Jeremy Osterhouse's home page


Vim tips

My collection of vim tips. Good things to know, or just interesting. I should use these as sigs for my email or something.

Add an introduction...

Add conventions...

Bram Moolenaar, the main guy behind vim, has some good things to say about using vim in his Seven habits of effective text editing
There's always the main vim website. If you don't have vim, you can download it there, for unix, windows, mac, and others.
I just found this very nice article on efficient editing with vim.

If you know nothing else

Vim help is useful! (:help OR :h) Example: :h tabs will pull up help on tabstop. :h z will pull up help for the z command and commands starting with z. :h v_{char} will pull up help on {char} when in visual mode. :h :c will pull up help on commands starting with c. And so on. I used to almost always have a window up with the online vim documentation. Then I discovered that it's all write there just a few keystrokes away.


Little Movements

I - go to the beginning of the line and go into insert
A - go to the end of the line and go into insert

0, ^ -- Move to the beginning of the line, and first non-blank character of the line, respectively
$ -- Move to the end of the line

f{char} - go forward to {char} on the current line
F{char} - go backwards to {char} on the current line

t{char} - go forward to {char} on the current line, but stop before it
T{char} - go backwards to {char} on the current line, but stop after it

; - repeat last f,F,t,T
, - repeat last f,F,t,T backwards

gj,gk -- Move down or up one display line. Helpful for navigating in long lines that wrap around.

Big Movements

gg -- Move to the very top of the file
G -- move to the very bottom of the file
if either command is proceeded by [count], moves to the [count] line from the top

H,M,L -- (think, High, Middle, Low) Put the cursor on the first line, middle, and bottom of the window. If H or L are proceeded by [count], move to the [count] line from the top of bottom

zt,zz,zb -- (think Top, z?, Bottom) Move the window such that the current line is on the top of the window, the middle, and the bottom. Great for seeing context.

'' OR `` -- Go to the last jump point. Useful for jumping between two places in a file.
m{a-z} -- create a mark {a-z}
'{a-z}, `{a-z} -- jump to a mark {a-z} ` takes you to mark, ' takes you to first character on the mark's line

* -- Search for the word under the cursor

/ - search forward for whatever text you type in
? - search backwards for whatever text you type in


If you edit text files with vim, setting linebreak (:set linebreak) will make sure that vim breaks lines on word barriers, improving readability significantly. Note: it only breaks the line on the screen, not in the file

If you have wrap on and would like to see when a line is wrapped or not, you can set "showbreak" to a character which will be appended to each wrapped line. (i.e., :set showbreak=>)
To change the width of tabs to n characters - :set tabstop=n
To change the width of indent to n characters - :set shiftwidth=n
Lots of helpful info on tabs in vim - :help tabstop

Advanced. Or, bet you didn't know...

If you want to search for something you select in visual mode: 1) yank 2) / 3) CTRL-R" 4) <enter> That is: y/^R"<enter> (note: " is the default yank buffer. You could use a named buffer as well if you want.)

~ -- switch case of character under cursor

i_CTRL-A -- In insert mode, insert the previously inserted text.

{visual}g? -- Rot13 encode highlighted text
g?? -- Rot13 encode the current line

CTRL-A -- Add [count] to the number at or after the cursor
CTRL-X -- Subtract [count] from the number at or after the cursor

J - join the current line with the line below it.

. -- repeat the last change

If you want to change a quotation: "This is a quotation"
And want to change it to: "blablabla"
Start on the first letter of the quotation and press ct" (Change Till ")

ga - with your cursor over a character, it will show you the decimal, hex, and octal codes of the character. Great for non printable characters.

To insert special characters, you'll be interested in digraphs. :digraphs will show you a complete table of special characters, plus their two character code, plus their ascii number. You can insert digraphs using ctrl-k plus the two character code. For example ctrl-k SX results in ^B being inserted