Vim Commands for C Progammers --------------------------------------------------------------------- Aimed at those taking Erin's C Class (http://erinb.freeshell.org/c/), this brief guide will be useful for any Vim user who is taking first steps into C programming. This mostly summarises The Vim Tutorial Chapt. 7, which is available for free download from the Vim docs page (http://www.vim.org/docs.php) - Syntax Highlighting Syntax highlighting makes it easier to see if you have, say, typed a single quote rather than a double to close a quoted statement. Type the following to enable it... :syn on If it is not to your liking, ':syn off' disables it. - Abbreviations I like to comment functions when they are begun so that you know what their purpose is, like below.. /***************************** getYN() ********************************** Returns 1 if user enters 'y', 0 if user enters 'n' ************************************************************************/ getYN () { blah.. } To quickly make such frame is easy under Vim. First define two abbreviations in your .vimrc (_vimrc in Windows). :ab /** /************************************************************************ :ab **/ ************************************************************************/ This means whenever you type '/**', it expands to the above line of asterix. The same happens when you type '**/'. Once you have done this, move to the first opening line and type '30l' to move you to roughly halfway along the line, the type 'R' then enter you function name. In between your lines, add a brief description of your functions. - Autowarapping Comments Comments can be set so that they wrap automatically at a set chaarcter width. This is fantastic for languages for like Perl where each line must be prefixed with a #. In C this isn't so important but it can still be useful. To enable type the following in your .vimrc... set formatoptions=cq set textwidth=72 "Studies say that 72 characters is good for readability. However, say that you alter your comments later and there is no longer the nice formatting, simply select all the comment in visual mode (push 'v') and type 'gq'. This should reformat the text again. - Setting to Four Spaces In your .vimrc file add the following lines... set tabstop=4 set shiftwidth=4 Now tab away! - 'cindent' Command Vim can regognise patterns in C code and has a reasonably good idea of when to ident and when not to. To enable this type the following... :set cindent If it is not to your liking, type ':set nocindent' to disable it. - Bracket Matching Position the cursor of the opening bracket, then type '%' to jump to the closing bracket. This is useful as a moving command (i.e. to delete all code in a block type 'd%') or also to check if your brackets are properly matched. - Jumping to Variable Declaration Place your cursor over the variable and type 'gD' to jump to the global declaration, or type 'gd' to jump to the local declaration of a variable. - (Un)Indenting a Chunk of Code I use visual mode a lot. Go to the start of where you want indenting, type 'v' to go into visual mode, then move down to the end of the chunk. Once there type '<' once. All the code inbetween should be indented by one tab. Use marks ('ma' to set mark called 'a', ''a' (single quote, a) to jump to it) if you need to do this many times. Pushing < unindents code.