Vim For Programming, Sometimes In C

Most of the time, when I write code, I write it in Ruby. I also write code in languages like Perl and even Scheme. For them, I tend to like my indentation depth at two columns (character widths). Once in a while, I write code in C or even C++. For them, I like my indentation depth around four columns. For all of the above, I use Vim -- not because I love everything that Vim does, but because there are just a few things it does that I really want, above and beyond what nvi provides. What I would most prefer is actually nvi plus just a handful of extra features.

I use a custom color scheme for syntax highlighting. I don't really feel like I need syntax highlighting, and in fact without this customized color scheme I would prefer no syntax highlighting at all -- because any other color schemes I have seen (including Vim's default) are all worse than no syntax highlighting at all, at least for my tastes. I did finally go to the effort of tweaking and fine-tuning a color scheme that makes syntax highlighting a better option for my purposes than no syntax highlighting, so I use it these days.

I occasionally use folding. This is where a block of code gets collapsed into a highlighted line that tells me how many lines have been "folded" away, so I can see the basic structure of the code without having all the code within those blocks taking up space on the screen. It is difficult to explain; I recommend just doing some searches online for screenshots of folding in Vim to get an idea what I mean.

What follows is a selection of some of the configuration options I use in my ~/.vimrc file. I specifically chose the options that pertain to the above described preferences.

filetype plugin indent on

syntax enable

set autoindent
set shiftwidth=2
set tabstop=4
set expandtab

set nofoldenable
set fdm=indent

autocmd BufEnter *.txt syntax off
autocmd BufEnter *.c set shiftwidth=4
autocmd BufEnter *.cpp set shiftwidth=4
autocmd BufEnter *.h set shiftwidth=4

The filetype line basically says that Vim should use settings derived from plugins that are sensitive to the type of file currently opened in the editor.

The syntax line turns on syntax highlighting.

The autoindent line tells Vim that I want it to automatically indent and unindent lines based on the syntax of the code I write.

The shiftwidth line tells Vim how many columns of indentation it should default to starting or ending when doing its autoindent thing. I choose a value of 2 for this because that is the level of indentation I want for the high-level dynamic languages I use most often.

The tabstop line tells Vim that it should default to moving the cursor to the nearest-next multiple of four columns when I press the Tab key.

The expandtab line ensures that all tabs are actually expanded into the correct number of spaces rather than inserted into the file as the \t (tab) character.

The nofoldenable turns off folding by default, because a lot of the time I do not actually want to use folding. I tend to use za in Vim's command mode to fold something because it does some automagical stuff, including turning on foldenable the first time I use it -- and za also unfolds when I use it where there is already a fold. Check the :help foldenable and :help za for more details.

The fdm line tells Vim that I want it to use indentation levels as indicators of where a level of folding should occur. This is a good choice if your indentation discipline is good. If it is not so good, you probably should not be programming much anyway.

The autocmd BufEnter lines execute Vim commands automatically when opening a file with the specified filename format (e.g. *.txt indicating a filename ending in .txt):

I may add to this file or edit it in the future.