The Stripey Site

Text Formatting

The way Vim formats text as it is typed can be improved. In particular, its behaviour can be tailored for different types of files.

All the settings below are included in the sample .vimrc configuration file.

General Settings

It can be confusing to have Vim wrap lines and make it look like there are line breaks where there aren’t, so turn this off:
set nowrap
It’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=79
Indents 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 expandtab
It 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 autoindent
The 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

Defining Comments

What Vim considers to be a comment affects how it formats text. By default it considers any line beginning with “* ” to be a middle-line of a C-style (/* ... */) 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::

Plain Text (Human Language) Files

Different formatting settings are designed for plain text files, those just containing ordinary English (or whatever) language. There isn’t a default filetype for these, so define that all files with the extension .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

Program Files

For C-like programming, Vim can sort out all the indentation — things in braces, loops, conditions, etc — automatically:
autocmd FileType c,cpp,slang set cindent
For 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+=ro
For Perl programming, have things in braces indent themselves:
autocmd FileType perl set smartindent
Makefiles 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

Webpages

See the HTML page for settings suitable for sensibly inserting line breaks in HTML and indenting HTML and CSS.


Feedback is welcome via vim-www@stripey.com. © Copyright 2000 — see copying information for details.