Difference between revisions of "Vim"

From Noah.org
Jump to navigationJump to search
Line 22: Line 22:
 
It's very tempting to remap lots of keys. I would like a little bit of emacs compatibility (such as CTRL-A and CTRL-E for beginning and end of line in insert mode). An inverted T for cursor control would be better than HJKL cursor control. I also <b>HATE</b> the   
 
It's very tempting to remap lots of keys. I would like a little bit of emacs compatibility (such as CTRL-A and CTRL-E for beginning and end of line in insert mode). An inverted T for cursor control would be better than HJKL cursor control. I also <b>HATE</b> the   
 
fact that q: splits a new window with the command-line history. I often instinctively hit q or q: when I was to quit. Of course, I meant to hit q: to quit. Hitting q puts you in record mode. Hitting q: shows command-line history. Since I hit :q about 5000 times a day I will almost certain accidentally hit q or q: at least a few times.
 
fact that q: splits a new window with the command-line history. I often instinctively hit q or q: when I was to quit. Of course, I meant to hit q: to quit. Hitting q puts you in record mode. Hitting q: shows command-line history. Since I hit :q about 5000 times a day I will almost certain accidentally hit q or q: at least a few times.
 +
 +
But I avoid most of these temptations. It's better to stick with a stock Vim.
 +
Every once in a while I try out a few customizations such as mapping q: to :q.
 +
I have never used q: and I don't think I ever will, so mapping it to q: seems harmless.
  
 
== The power of :g ==
 
== The power of :g ==

Revision as of 16:54, 27 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

To make full use of this you also want the .vim directory and everything below it (plugin). Most of these are vim scripts that support .vimrc.

I tried to balance vi compatability and vim features. I like features, but I didn't want to make my vim so custom and unique that I would go crazy when I had to go back to a machine without my custom Vim. I tried to keep it familiar.

Temptations

It's very tempting to remap lots of keys. I would like a little bit of emacs compatibility (such as CTRL-A and CTRL-E for beginning and end of line in insert mode). An inverted T for cursor control would be better than HJKL cursor control. I also HATE the fact that q: splits a new window with the command-line history. I often instinctively hit q or q: when I was to quit. Of course, I meant to hit q: to quit. Hitting q puts you in record mode. Hitting q: shows command-line history. Since I hit :q about 5000 times a day I will almost certain accidentally hit q or q: at least a few times.

But I avoid most of these temptations. It's better to stick with a stock Vim. Every once in a while I try out a few customizations such as mapping q: to :q. I have never used q: and I don't think I ever will, so mapping it to q: seems harmless.

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 sometimes

These are commands that I learned at one time and thought that they seemed pretty lame. I thought that I would forget about quickly, yet for some reason I find that I use them enough to remember them.

CTRL-A

This increments a number to the right of the cursor. I thought this was totally pointless when I first saw it; yet, 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.

 :h i_CTRL-W

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.

Vim Cheat Sheet

VIM Cheat SheetClick to view this handy VIM cheat sheet. This was taken from ViEmu -- home of vi/vim emulation for Microsoft Visual Sutdio.