Updated: April 23rd, 2008

[WORK IN PROGRESS] Here is a list of commands that I use every day with vim, in no particular order. Out of a billion possible key combinations, I found these to be irreplaceable and simple enough to remember.

     

    *

    search for the word under cursor (to the end of the file)

    #

    search for the word under cursor (to the top of the file)

    ctrl-p,ctrl-n

    suggest (p)revious or (n)ext autocomplete from the list of existing keywords in the file or included files (!).

    :go NNN

    go to byte NNN

    .

    redo last command

    /SEARCH TERM

    search document for SEARCH TERM

    :%s/FOO/BAR/gci

    replace FOO with BAR (g)lobally, case (i)insensitively, and asking for (c)onfirmation

    n (N)

    next (previous) search result

    %

    find and jump to a matching brace or parenthesis

    u

    undo

    ctrl-r

    redo

    r CHAR

    replace character under curson with CHAR

    i

    start editing before current character

    I

    start editing in the beginning of current line

    a

    start editing after current character

    A

    start editing at the end of current line

    o

    start editing on the next line

    O

    start editing on the previous line

    :wq or ZZ

    write file and exit

    ctrl-v

    visual block select (rectangular)

    shift-v

    visual line select

    ctrl(or shift)-v y or d

    copy or delete selected text

    yy

    yank (copy) current line

    yNNN arrow up/down

    yank NNN lines above or below

    p

    paste the yanked buffer

    cw

    change word (delete word under cursor and go into edit mode)

    cNw

    change N words

    e!

    reload the file (revert)

Today I was asked a question about defining custom extensions for vim syntax highlighting such that, for example, vim would know that example.lmx is actually of type xml and apply xml syntax highlighting to it. I know vim already automatically does it not just based on extension but by looking for certain strings inside the text, like

image

After digging around I found the solution. Add the following to ~/.vimrc (the vim configuration file):

1
2
3
syntax on
filetype on
au BufNewFile,BufRead *.lmx set filetype=xml

After applying it, my .lmx file is highlighted:

image

Same principle works, for instance, for mysql dumps that I have to do from time to time. If they don't have a .sql extension, you'll get something like:

image

After

1
2
3
syntax on
filetype on
au BufNewFile,BufRead *.dump set filetype=sql

everything is fine:

image

But why and how does it work, you ask?

:help au :au[tocmd] [group] {event} {pat} [nested] {cmd}
Add {cmd} to the list of commands that Vim will execute automatically on {event} for a file matching {pat}.
:help BufNewFile When starting to edit a file that doesn't exist.
:help BufRead When starting to edit a new buffer, after reading the file into the buffer.
:help filetype will actually tell this whole story in part B.

And that's how you do it, folks.