Difference between revisions of "Vim"

From Noah.org
Jump to navigationJump to search
Line 8: Line 8:
  
 
This is my super-terrific .vimrc file.
 
This is my super-terrific .vimrc file.
     [http://www.noah.org/engineering/dotfiles/vimrc .vimrc]
+
     [http://www.noah.org/engineering/dotfiles/.vimrc .vimrc]
  
 
I tried to balance vi compatability and familiarity with  
 
I tried to balance vi compatability and familiarity with  

Revision as of 18:51, 9 April 2007

Vi is an archaic text editor from the early dawn of the computer age. How it survived to this day is amazing. It continues to evolve and adapt in the form of Vim. Learning Vim is like learning short-hand. It seems crazy and overly complex at first, but given time it shows its value.

My .vimrc

This is my super-terrific .vimrc file.

   .vimrc

I tried to balance vi compatability and familiarity with new time-saving features. I didn't want to make my vim so different that I would go crazy when I had to go back to a machine without my custom Vim.

The power of :g

:g

Run a macro on matching lines

After you record a macro it's easy to play it back on lines that match a pattern: (example assuming a macro recorded as 'q'):

    :g/<pattern>/normal @q

Or run the macro on lines that don't match:

    :g!/<pattern>/normal @q

Folding

Most folding is complicated and bothersome. These notes describe easy Vim folding.

My favorite trick is to fold on the current /search/ pattern. This way I can see only the lines that contain what I'm searching for. This is handy even for regular text documents, not just code.

   set foldexpr=getline(v:lnum)!~@/
   set foldmethod=expr foldlevel=0 foldcolumn=1

To make life easy, I map that to a key sequence \z (The <leader> character is the \ key on most systems).

   map <silent><leader>z :set foldexpr=getline(v:lnum)!~@/ foldlevel=0 foldcolumn=0 foldmethod=expr<CR>

When I fold code, I just want show class, method, and function names. I just want an index view of my code. I don't want to fold on every nested statement, so I only need one level of folding. I add a map so that I can quickly set the search pattern to find all classes and functions. The following search pattern works for both PHP and Python:

   /^\s*class\s\\|^\s*function\s\\|^\s*def\s/

I combine this with the search folding trick so that I can quickly fold code without the bother of syntax files or nested folding regions.

Finally, I add a map so that the SPACE key will toggle the hide/show state of the fold under the cursor.

To use these tips together, in normal mode type "zff" to highlight all classes and functions. Then type "\z" to fold. Now the document looks like an index. Press SPACE to view a class or function.

Put the following in your .vimrc file:

" folding using /search/ pattern
" \z
" This folds every line that does not contain the search pattern.
" see vimtip #282 and vimtip #108
map <silent><leader>z :set foldexpr=getline(v:lnum)!~@/ foldlevel=0 foldcolumn=0 foldmethod=expr<CR>
" this folds all classes and function to create a code index.
" mnemonic: think "function fold"
map zff :/^\s*class\s\\|^\s*function\s\\|^\s*def\s/<CR>:set foldmethod=expr foldlevel=0 foldcolumn=1<CR><CR>
" space toggles the fold state under the cursor.
nnoremap <silent><space> :exe 'silent! normal! za'.(foldlevel('.')?'':'l')<cr>

Intermediate Vim

help

Vim has very complete help, but it can be difficult to search because of the ambiguous matches on many commands. Usually it helps to specify the context (normal mode, insert mode, command-line command, etc.)

        WHAT                  PREPEND    EXAMPLE
    Normal mode command      (nothing)   :help x
    Visual mode command         v_       :help v_u
    Insert mode command         i_       :help i_<Esc>
    Command-line command        :        :help :quit
    Command-line editing        c_       :help c_<Del>
    Vim command argument        -        :help -r
    Option                      '        :help 'textwidth'

Dumb Vim commandd I actually use

CTRL-A

This increments a number to the right of the cursor. I thought this was totally pointless when I first saw it; yet, somehow I seem to use it a lot.

 :h i_CTRL-A

CTRL-O

This executes one command then returns to Insert mode. For example, if you want to reformat the paragraph you are editing type:

 CTRL-Ogwap

I tend to forget about the insert mode commands.

 :h i_CTRL-O

CTRL-W

This backspaces an entire word in insert mode.

Recording keystrokes

When you record a sequence of keystrokes using q these go into a register. You can paste the register and edit the recording. Yank the lines back into a register and play it back. This is handy if you have a long recording that you want to fix.

 :h i_CTRL-W