Hacker News new | past | comments | ask | show | jobs | submit login
Vim Recipes (runpaint.org)
174 points by telemachos on Jan 7, 2011 | hide | past | web | favorite | 26 comments

This is pretty nice! My wish for these types of things - can you just put out a big list key combos to browse through? I know the basic concepts of vim but I just want to see how the commands can be strung together. Something like:

- ggVG: highlight the whole file

- jj: escape (this is in my .vimrc, not standard)

- xp: swap letters

- vab: highlight an s-exp (inluding the parens)

- gt: go to the next tab to the right

- gT: go to the next tab to the left

- :tabnew: open a new tab (you can also add a file after that)

- daw: delete a word

- das: deletes the sentence under the cursor

- dap: deletes the paragraph under the cursor

- vas: selects the sentence under the cursor

- ci(: deletes the inner content of a parens, puts you in insert mode

- q[key][whatever]q: records a macro

- @[key]: plays back the macro

- ZZ: save and close the current window

Before I made a brief foray in the land emacs, I used tabs in VIM rather than leveraging the buffer system (I came from a Windows world, so I was used to tabs in other editors). Then emacs opened my eyes to magic of buffers, and when I returned to VIM (for various reasons), I found buffers to be better than tabs, especially once I first learned the commands, and then updated my .vimrc with a few useful shortcuts, particularly for split views.

For any newbies who are curious what all this talk of buffers and tabs in vim is, check out:


And, in vim you can type:

  :help buffers

  :help tab-page

Please tell me the difference. All I see is that it's more difficult to switch when using buffers instead of tabs (have to press :, have to add ! if it's not saved).

There are multiple plugins to cosmetically improve interaction with buffers, my favourite is minibufexplorer. Using tabs like you might in other editors is fine for a beginner, but once you begin to learn and use more advanced vim features you'll very quickly realize they don't scale.

Also, you should always make sure 'set hidden' is in your '~/.vimrc'. It's a crime that it's not default in my opinion. Takes care of the dumb "have to add ! if it's not saved)." thing.

Two words: FuzzyFinder.

Bind it to a key and switching between buffers (or on-disk files that are not even loaded) becomes as simple as typing a few letters of the filename.

No more scanning of abbreviated tab-labels, no more trial and error cycling through buffers. Highly recommended.

I checked out buffers - it's not clear to me why they are better than tabs. It seems like a different UI treatment for basically the same thing. Can you clue me in?

Tabs are all about layout. Look into the windows (and how they fit in with buffers) and you'll start to notice the difference.

Basically, the only case where you should be using tabs is when you have several different arrangements of windows that you want to switch back and forth between.

Right. In Django I tend to use one tab per application, with seeral split buffers inside each.

Read this: http://stackoverflow.com/questions/1218390/what-is-your-most...

Then go to your nearest vim and do an

:he navigation


:he change.txt

to get a fantastic overview.

Honestly, just reading through the help system in general is fascinating.

Yeah I totally agree -- I find no end of amusement how a large portion of answers in #vim are simple :he $ANSWER. For a couple reasons actually:

1. Amazingly the documentation covers that odd topic!

2. Just knowing where to search is a big enough task a whole chatroom has essentially devoted itself to it.

3. No one thinks it odd that vim is a big enough, esoteric enough space that being a fancy search bot doesn't bother people.


sometimes its hard to know what you need to look up in help to reach your solution though (I mean before you know the general vim subsystem you need).

True. People often underestimate the comprehensiveness of vim's :help. Maybe (and this is conjecture) it's because other utilities lack thorough help documentation.

I think it's because most help system emebedded inside applications are useless. Vim is the only internal help I find useful.

This is pretty comprehensive:


this is how i learnt vim by using this site. i think doing vimtutor and this is all you need.

I've collected some of my favorites at http://vimtweets.com, pushed through http://twitter.com/vimtips Monday through Friday. I would judge most of them as "more advanced", for lack of a less judgmental-sounding term, but I hope you'll find them useful, anyway.

thanks a lot. i've just subscribed via RSS. I've been using vi/vim for 21 years, but good tips for intermediate/advanced users are _always_ helpful. Please keep updating your tips.

You only need to know a few basic pure motions, mostly for moving around: h/j/k/l, gg, G, <count>G, 0/^, $, <C-o>/<C-i>, <c-d>/<c-u>

The more important motions are %, f/t and the text-objects (:help for a full description). These are the vim power tools and honestly what I use ~80% of the time.

The f<char> motion moves forward on the same line up through <char>. The t<char> motion (mnemonic: 'til) works the same but doesn't include <char>. Capitalize to go backwards. You can string this together to get all sorts of useful behavior: change startFoo to stopFoo (ctF), work with TSV (ct<tab>), not have to remember W (ct<space>), etc. I tend to overuse it for things like deleting everything but the semicolon on a line so I don't have to retype the semicolon...but you get the point.

The % motion jumps between match pairs (parens, braces, etc), but the important trick with it is that if you're not on a pair, it searches forward till it finds one. This is mostly useful for manipulating function calls and email addresses, e.g. d% deletes a call if you're at the beginning of the function name. Pretty specific use but common use case.

Then there's text objects. You mentioned a couple use cases, but they're more general. There's two transitions, i (diw "delete inner word") and a (daw "delete a word"). Most text objects are marked by delimiters (e.g. parens, xml tags) a includes these i excludes them. There's a lot of ways to put these together, a few:

- ci[ : replace contents of an array access

- da[ : delete the array access

- ci" : replace contents of a string

- dat : delete an XML tag

- =i{ : realign the current block

- zfab : fold sexpr

- gqap : wrap paragraph

What makes the motions I mention here particularly great is that they tend to generalize well when you repeat them with period and don't rely on you pre-counting a certain number of words, lines, or whatnot.

If you're consistently using these motions, you're most of the way there to being a vim expert. The rest is all tricks. You can find your own tricks by noticing when you're repeating yourself or using too many keystrokes. In general, if you're using more than 5-6 keystrokes to do something, there's a shorter way to do it. The shorter way isn't necessarily worth the conceptual overhead, but you'll learn a bit more. You never really stop learning them, but it gets hard to remember once you've been using Vim daily for 5+ years (I'm 10 years in).

The most important plugin for tricks is surround.vim. You should immediately install it and add the following mappings:

nmap s ysi

nmap S ysa

nmap s$ ys$

nmap sv gvs

This lets you operate in 'surround' motions around text objects. E.g. sw( will surround the current word with parens, sp" surrounds the paragraph with quotes. You can change single quotes to double quotes: cs'" and so on. The sv map here surrounds the previous visual selection, I use it when I'm doing multiple wraps: sw'sv[sv(

Random useful tricks (there are thousands of these):

gv selects the previous visual selection. You can V select a number of lines, type colon and replace only in the selected area, then gv to do a < or :retab! or :g/^$/d or something.

Check out :help iskeyword, doing a :set iskeyword+=- is useful in CSS, XML, lisp.

The vim regex engine is screwy. Pretty much everybody knows this (sigh \+), but it's screwy in useful ways too. When you're doing a replacement, \zs and \ze mark the start/end of the match. If, for example you want to insert something at the end of all strings, :%s/"[^"]\+?\zs\ze"/ foo/g. Saves you from having to do capture groups/backrefs and whatnot. Also, the \k character class matches "keywords" (identifiers) for your current language and it's smart \<\k\+\> will not match numbers, for example.

If you have incsearch and hls turned on and are working on a tricky regex replace, it's useful to drop out and start oa normal search with / to see your matches highlighted. When you get your match, :%s//replace/g will reuse your search.

Know asterisk and # if you don't already.

You can save a location using m<letter> and jump back to it using `<letter>. Lowercase is in-file and uppercase is global. I tend to mA when I go looking for something elsewhere so I can get back to what I'm working on easily with `A. You can bounce back to where you just jumped from with ``, but I think <c-o> and <c-i> work better for that.

Random vimrc tricks:

"make Y consistent with C and D

nnoremap Y y$

" I use splits (tabs came later). `vim -o *.c'

nmap - :Sexplore<CR>

map <C-j> <C-W>j<C-W>_

map <C-k> <C-W>k<C-W>_

"repeats the last command for the entire visual selection

vnoremap . :normal .<CR>

"executes the macro in buffer a

nnoremap ' @a

vnoremap ' :normal @a<CR>

"Look up vim ToggleFold on google

nnoremap <silent> <space> :call ToggleFold()<CR>

"mapping for paste toggle

noremap <silent> <Leader>p :se invpaste<CR>:echo &paste ? "Paste ON!" : "Paste off"<CR>

" I think the default gp is stupid. This puts the cursor BEFORE the start of

" the paste and is primarily used for useful repeating: gpj.j.j.

nnoremap <silent> gp :<C-U>call repeat#exec("p`[h",1)<CR>

My favorite motion is search. As in "c/foo" to change until the next occurrence of foo.

gg=G: Autoformat the entire file. Useful to apply changes after changing tab options.

I just ran across the following, today:

Vim offers strong file encryption with Blowfish


Haven't looked into it much, yet. On a Windows machine, today, where I did notice that it seems to bork tab-based autocompletion when specifying a filename while saving (^I doesn't trigger autocompletion).

EDIT: Credit due to:


Blowfish encryption (and persistent undo) came in 7.3:


In case anyone else was wondering, the PDF appears to have been generated from HTML using "Prince" http://www.princexml.com/.

I tried journeying into Vim, but failed, sticking to Netbeans for mainly python-development.

Highlights that made me uneasy:

- Often I'd find myself with an accidentally closed gvim, having to open up working projects all over again.

- Overview of source changes with more control: Changes in n files pops up n vimdiff windows.

Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact