|
|
|||||||||||||
All the settings below are included in the sample .vimrc configuration file.
set nowrapIt’s nice when typing paragraphs of text (e-mail messages, news postings, etc) for line breaks to be inserted automatically in appropriate places, so that <Enter> only needs pressing at the end of paragraphs not the end of lines. This is also convenient when typing comments in scripts and programs (where the comment character is also automatically inserted after the line break). However, such behaviour is definitely not wanted when working on computer-readable code such as programs. So have Vim generally not formatting text; it will still format comments, which here are set to have a maximum of 79 characters on each line:
set formatoptions-=t set textwidth=79Indents are often more readable if they consist of two spaces rather than a tab character; this affects the >/< keys in normal mode, and <Ctrl>+T/<Ctrl>+D in insert mode:
set shiftwidth=2 set shiftround set expandtabIt can also be handy to have indentation ‘copied down’ lines as you type, so that once the first line of something has been indented, the indent will apply to all subsequent lines typed, until cancelled (EG with <Ctrl>+U just after <Enter>). This can be achieved with:
set autoindentThe rest of this page deals with settings useful for specific file formats. These assume that the above general settings are in place, and that filetype detection has be enabled with:
filetype on
/* ... */)
comment, which can be irritating if you ever use “*” for
bullet lists in plain text. To remove the default definition use:
set comments-=s1:/*,mb:*,ex:*/An alternative (which I also thinks looks nicer) is to use two stars on the middle lines of C comments, so that they look like this:
/* ** This is the start of the comment. ** It continues like this. ** And finishes here. */This can be achieved with:
set comments+=s:/*,mb:**,ex:*/Then bullet lists using stars can be defined like they already are for hyphens, without fear of being confused for a C comment:
set comments+=fb:*Somewhat bizarrely, Vim doesn’t treat lines beginning with quote marks as comments, despite this being the character used to denote comments in scripts and configuration files for, er, Vim. This can be remedied with:
set comments+=b:\"It can also use be useful to have colon-started lines treated as comments, because many Tin users use them to denote quotations.
set comments+=n::
.txt should be assumed to be
‘human’ format. Mail and news articles also fall into this
category; they already have a filetype, though I also want anything in my
postponed directory to be considered a news article, so I have:
augroup filetype autocmd BufNewFile,BufRead */.Postponed/* set filetype=mail autocmd BufNewFile,BufRead *.txt set filetype=human augroup END(This clobbers the
help filetype, but this doesn’t
seem to prevent help from working properly.)
Then Vim can be set up so that line breaks are automatically inserted in all human text. I have these at 72 characters so that even if my mail or news article is quoted by somebody else, it still fits into an 80-column terminal:
autocmd FileType mail,human set formatoptions+=t textwidth=72
autocmd FileType c,cpp,slang set cindentFor actual C (not C++) programming where comments have explicit end characters, if a new line is started after
/* but before
*/ then it is obviously still part of the comment so have
the relevant leader characters inserted:
autocmd FileType c set formatoptions+=roFor Perl programming, have things in braces indent themselves:
autocmd FileType perl set smartindentMakefiles need to have genuine tab character codes in them; an equivalent number of spaces will not do, so ensure that tabs are definitely used and that indentations are to tabstops:
autocmd FileType make set noexpandtab shiftwidth=8