Automatic Indenting With Vim
Vim has an extremely powerful and flexible automatic indent option, indentexpr. This makes indenting happen according to a particular expression. It's particularly useful when used together with automatic filetype detection. To turn this on, add this line to your ~/.vimrc:
|Tip of the Trade: Indentation may seem an insignificant step when coding, but Vim makes it automatic with its indent option, indentexpr. This powerful yet flexible tool indents based on expression.
filetype indent on
You can also use :filetype indent on in command mode once Vim has started, but this won't persist between sessions.
Now, find out the detected type of your file with the command :set filetype. In the case of this file, it's HTML. Check out /usr/share/vim/vimXX/indent/filetype.vim (where XX is the Vim version you're running, and filetype is the filetype name). If the file is not there, try /usr/local/share. This file has the indent rules for your filetype, and you can edit them or add more rules if you don't like them. Similarly, you can create a set of indent rules for any filetype that isn't already listed. Be sure to check out the Vim web site first in case someone's done it already.
smartindent and autoindent offer a less sophisticated alternative to indentexpr. It's worth also setting these in your ~/.vimrc, in case you have a file of a type that Vim doesn't know. Use these lines:
set ai set si
You can also temporarily turn off automatic indenting by typing :set paste in command mode. (:unset paste to turn it back on again.) This is useful -- as you might guess from the command name! -- if you're pasting in chunks of text or code to avoid getting annoying extraneous indents.
Juliet Kemp has been messing around with Linux systems, for financial reward and otherwise, for about a decade. She is also the author of "Linux System Administration Recipes: A Problem-Solution Approach" (Apress, 2009).
IT Solutions Builder TOP IT RESOURCES TO MOVE YOUR BUSINESS FORWARD
Which topic are you interested in?
What is your company size?
What is your job title?
What is your job function?
Searching our resource database to find your matches...