The code standard that I use indicates that lines should have less than 80 chars. I want to indicate this automatically while I write to avoid raising the error later.
What is a good way of showing it?
Vi and Vim Stack Exchange is a question and answer site for people using the vi and Vim families of text editors. It only takes a minute to sign up.
Sign up to join this communityIf you have Vim 7.3 or later, you can set colorcolumn
, or cc
.
:set colorcolumn=80
From :help colorcolumn
:
'colorcolumn' is a comma separated list of screen columns that are highlighted with ColorColumn hl-ColorColumn. Useful to align text.
You can also set it relative to your textwidth
variable, which is maybe more
useful than setting it to an absolute value:
:set colorcolumn=+1
Or set multiple columns:
:set colorcolumn=-1,+30
To change the color of the colorcolumn
use :highlight ColorColumn
, for
example:
:highlight ColorColumn ctermbg=lightgrey guibg=lightgrey
In addition to the colorcolumn option, you can tell vim to automatically push you down to the next line by putting:
set textwidth=80
into your .vimrc file.
From the docs (:h textwidth
)
Maximum width of text that is being inserted. A longer line will be broken after white space to get this width. A zero value disables this. 'textwidth' is set to 0 when the 'paste' option is set. When 'textwidth' is zero, 'wrapmargin' may be used. See also 'formatoptions' and ins-textwidth.
When 'formatexpr' is set it will be used to break the line.
NOTE: This option is set to 0 when 'compatible' is set.
Instead of showing a line showing the maximum size, the extra characters can also been highlighted through
:let w:m1=matchadd('ErrorMsg', '\%>80v.\+', -1)
This will set the extra characters to be marked as error (normally, red background, but it depends on the color scheme).
This can be added to .vimrc
to do it automatically on each file
:au BufWinEnter * let w:m2=matchadd('ErrorMsg', '\%>80v.\+', -1)
gitcommit
syntax, for example, which uses syntax highlighting.)