Vim Quick Reference
Wednesday, December 27, 2006
I often use vim as the editor when I’m connecting to my hosting
provider…. so I figured I’d put the most common tasks up
here, since I can never remember some of these.
| Open / Save / Exit |
|
| :w | Save File |
| :q | Quit |
| :q! | Really quit, don’t just talk about it. |
| :wq | Quit, but save first, cause who really wants to do something in vim more than once? |
| :x | Pretty much the same as the above, but it doesn’t save if you don’t need to. This makes more sense, and it’s one keystroke shorter. |
| ESC | Go back to command mode |
| Navigating / Edit |
So this is where the gmail keys came from! |
| j | Up one line |
| k | Down one line |
| l | right |
| h | left |
| e | end of the world. I mean word. |
| b | beginning of the word. |
| 0 | Beginning of a line |
| $ | End of a line |
| H | kinda like Home, takes you to the top left |
| L | also means loser, takes you to the bottom of the screen. |
| :88 | takes you to line #88. Don’t try this with any line other than 88. |
| i | insert text. This is one of the only keys you need to know. |
| x | delete the character you are standing on. This is the other character you really need to know. |
| cw | delete the current word and start inserting. means “Change Word”. Thanks Quinn! |
| r | overwrite a single character. I like this one. |
| R | replace lots of characters. |
| o | Make a new line below and start insert mode |
| O | Make a new line above and start insert mode |
| a | append right here. You’ll probably use i more. |
| A | Appends at the end of the line. I use this a lot. |
| dd | delete the entire line. |
| 9dd | delete 9 lines. This only works for 9 lines. You couldn’t say, use this to delete 8 lines by doing ‘8dd’. No way would that work. |
| yy | yank the current line to the clipboard, or whatever they call it in vim terms. |
| 5y | yank 5 lines to the clipboard. |
| p | paste the line you just yanked. |
| u | Undo. This app actually has undo? very cool. |
| /pattern | search for the pattern “pattern” Kinda redundant example. |
| n | Works like F3 does in windows, takes you to the next search result. |
| N | works like F3 doesn’t in windows, and takes you to the previous search result. |
| %s/stuff/toreplace/g | Replace stuff with toreplace everywhere in the file. |
| G | Go to the end of the file (thanks Q!) |
May 2nd, 2007 at 8:16 pm
Some more vim commands I use often:
- An alternate to ‘n’ is to just type ‘/’ again to move to the next search result.
- ‘?’ also takes you to the previous search result (or searches backwards if you prefer to think of it that way).
- CTRL-R is the command for redo (or undo undo).
- At the command prompt (the ‘:’ thingy), type ‘!’ and then a bash command to see the output in a shell window (there’s a command to insert the output of that shell command, but I can’t remember it right now).
- ‘J’ joins the current line with the next one (comes in more handy than you’d think).
- In addition to just doing a %s/…/…/ search and replace, you can do line ranges. ‘.’ means the first line of the program, ‘$’ means the last line, and a number is that line number. So you can do .,8s/…/…/, or 15,$s/…/…/, or even .,$s/…/…/ if you’re feeling silly.
- In addition to ‘g’ after a search and replace, there’s ‘c’ which will make vim ask your permission for each replacement, and ‘i’ which will do a case-insensitive search. There’s probably others I don’t know about.
June 28th, 2007 at 1:49 am
In addition to Pete:
just s/…/…/ replaces on the current line, adding a g (%s/…/…/g) replace multiple items on the same line
Extra:
- If you edit a lot of source code: ‘gf’ to open the file under the cursor
- When the cursor is on a (, ), [, ], { or }, press % to go to the matching opening or closing bracket
July 23rd, 2007 at 8:44 am
‘D’ is useful: delete to end of line.
December 5th, 2007 at 12:49 pm
kjlk kjlkj
April 12th, 2008 at 6:07 pm
This is one of the best vi guides I’ve seen. It made me smile and remember too!
May 11th, 2008 at 5:38 am
nice
I have found two interesting sources and would like to give the benefit of my experience to you.
I am tuning my pc by the best software for free, with the file search engine http://fileshunt.com and http://filesfinds.com May be you have your own experience and could give some useful sites too. Because this two social sites help me much.