Posted by Jean-Baptiste Jung on Jun 30, 2008 | 100 comments100 Vim commands every programmer should know
Since the 70’s, Vi is one of the programmer’s best friend. Nevermind you’re new to Vi or not, here’s a big list of 100 useful commands, organized by topic, which will make your coder life better.
Search
| /word | Search “word” from top to bottom |
| ?word | Search “word” from bottom to top |
| /jo[ha]n | Search “john” or “joan” |
| /\< the | Search “the”, “theatre” or “then” |
| /the\> | Search “the” or “breathe” |
| /\< the\> | Search “the” |
| /\< ….\> | Search all words of 4 letters |
| /\/ | Search “fred” but not “alfred” or “frederick” |
| /fred\|joe | Search “fred” or “joe” |
| /\<\d\d\d\d\> | Search exactly 4 digits |
| /^\n\{3} | Find 3 empty lines |
| :bufdo /searchstr/ | Search in all open files |
Replace
| :%s/old/new/g | Replace all occurences of “old” by “new” in file |
| :%s/old/new/gw | Replace all occurences with confirmation |
| :2,35s/old/new/g | Replace all occurences between lines 2 and 35 |
| :5,$s/old/new/g | Replace all occurences from line 5 to EOF |
| :%s/^/hello/g | Replace the begining of each line by “hello” |
| :%s/$/Harry/g | Replace the end of each line by “Harry” |
| :%s/onward/forward/gi | Replace “onward” by “forward” , case unsensitive |
| :%s/ *$//g | Delete all white spaces |
| :g/string/d | Delete all lines containing “string” |
| :v/string/d | Delete all lines containing which didn’t contain “string” |
| :s/Bill/Steve/ | Replace the first occurence of “Bill” by “Steve” in current line |
| :s/Bill/Steve/g | Replace “Bill” by “Steve” in current line |
| :%s/Bill/Steve/g | Replace “Bill” by “Steve” in all the file |
| :%s/\r//g | Delete DOS carriage returns (^M) |
| :%s/\r/\r/g | Transform DOS carriage returns in returns |
| :%s#<[^>]\+>##g | Delete HTML tags but keeps text |
| :%s/^\(.*\)\n\1$/\1/ | Delete lines which appears twice |
| Ctrl+a | Increment number under the cursor |
| Ctrl+x | Decrement number under cursor |
| ggVGg? | Change text to Rot13 |
Case
| Vu | Lowercase line |
| VU | Uppercase line |
| g~~ | Invert case |
| vEU | Switch word to uppercase |
| vE~ | Modify word case |
| ggguG | Set all text to lowercase |
| :set ignorecase | Ignore case in searches |
| :set smartcase | Ignore case in searches excepted if an uppercase letter is used |
| :%s/\<./\u&/g | Sets first letter of each word to uppercase |
| :%s/\<./\l&/g | Sets first letter of each word to lowercase |
| :%s/.*/\u& | Sets first letter of each line to uppercase |
| :%s/.*/\l& | Sets first letter of each line to lowercase |
Read/Write files
| :1,10 w outfile | Saves lines 1 to 10 in outfile |
| :1,10 w >> outfile | Appends lines 1 to 10 to outfile |
| :r infile | Insert the content of infile |
| :23r infile | Insert the content of infile under line 23 |
File explorer
| :e . | Open integrated file explorer |
| :Sex | Split window and open integrated file explorer |
| :browse e | Graphical file explorer |
| :ls | List buffers |
| :cd .. | Move to parent directory |
| :args | List files |
| :args *.php | Open file list |
| :grep expression *.php | Returns a list of .php files contening expression |
| gf | Open file name under cursor |
Interact with Unix
| :!pwd | Execute the “pwd” unix command, then returns to Vi |
| !!pwd | Execute the “pwd” unix command and insert output in file |
| :sh | Temporary returns to Unix |
| $exit | Retourns to Vi |
Alignment
| :%!fmt | Align all lines |
| !}fmt | Align all lines at the current position |
| 5!!fmt | Align the next 5 lines |
Tabs
| :tabnew | Creates a new tab |
| gt | Show next tab |
| :tabfirst | Show first tab |
| :tablast | Show last tab |
| :tabm n(position) | Rearrange tabs |
| :tabdo %s/foo/bar/g | Execute a command in all tabs |
| :tab ball | Puts all open files in tabs |
Window spliting
| :e filename | Edit filename in current window |
| :split filename | Split the window and open filename |
| ctrl-w up arrow | Puts cursor in top window |
| ctrl-w ctrl-w | Puts cursor in next window |
| ctrl-w_ | Maximise current window |
| ctrl-w= | Gives the same size to all windows |
| 10 ctrl-w+ | Add 10 lines to current window |
| :vsplit file | Split window vertically |
| :sview file | Same as :split in readonly mode |
| :hide | Close current window |
nly | Close all windows, excepted current |
| :b 2 | Open #2 in this window |
Auto-completion
| Ctrl+n Ctrl+p (in insert mode) | Complete word |
| Ctrl+x Ctrl+l | Complete line |
| :set dictionary=dict | Define dict as a dictionnary |
| Ctrl+x Ctrl+k | Complete with dictionnary |
Marks
| mk | Marks current position as k |
| ‘k | Moves cursor to mark k |
| d’k | Delete all until mark k |
Abbreviations
| :ab mail mail@provider.org | Define mail as abbreviation of mail@provider.org |
Text indent
| :set autoindent | Turn on auto-indent |
| :set smartindent | Turn on intelligent auto-indent |
| :set shiftwidth=4 | Defines 4 spaces as indent size |
| ctrl-t, ctrl-d | Indent/un-indent in insert mode |
| >> | Indent |
| << | Un-indent |
Syntax highlighting
| :syntax on | Turn on syntax highlighting |
| :syntax off | Turn off syntax highlighting |
| :set syntax=perl | Force syntax highlighting |
Related Posts
No related posts.
100 Comments + Trackbacks
6.30.2008
/\/ Search “fred” but not “alfred” or “frederick” — huh?
Search exactly 4 numbers — digits
6.30.2008
Thanks for correcting my mistakes!
6.30.2008
Excellent! A very useful list. Thanks a lot.
7.1.2008
I see a typo: Alignement >> Alignment
7.1.2008
Corrected. Thanks, Mark!
7.1.2008
In search…
/\/ Search “fred” but not “alfred” or “frederick”
There is no text here to indiciate a search for fred.
In replace…
:%s/old/new/gw Replace all occurences with confirmation
There is no w flag, I believe you mean c. See help :s_flags
In interact with unix…
I think CTRL-Z and the fg command work better than :sh and exit. At least they are much easier to type.
In text indenting…
<< Desindent … I believe “Un-indent” would be a better word. Or in fact, it is not indenting, it is shifting.
Didn’t check all, but those are what stood out.
Jon
7.1.2008
Most important command ever:
:q!
cd /usr/ports/editors/nano
make install
I really don’t understand why people still use VI. At least Emacs has its own operating system
7.1.2008
Stupid american
7.1.2008
@Jon Keating: Thanks

@Russia: I may be stupid, but a sure thing is that I’m not American: I live in Belgium and come from France
7.1.2008
Great list. Here is one of my favorite tricks:
map j
map k
map gt
map gT
This will let you switch between tabs and windows by using a combination of the control key and h,j,k, and l. For example, to move to the top window, you can press ctrl-k. To move to the tab on the right, you can press ctrl-l.
7.1.2008
Ignore my previous comment, since it seems like the comment system mangled up my text because it had less-than and greater-than characters in it.
7.1.2008
@Naveed: Yeah, sadly Wordpress comments system always break code and/or commands. I didn’t knew the tips you submitted, so I’m going to check it out.
7.1.2008
Interesting list but how can you not mention the awesome * or some of the tips on http://www.vim.org/tips/tip.php?tip_id=305 ? : )
7.1.2008
@Anon: The vim.org tips are indeed very helpful, but for this list, I didn’t wanted to re-use 75% of these tips. Though, I should have gave the link just as you did. Thanks!
7.1.2008
Here’s the list I created when learning vim:
http://www.pixelbeat.org/vim.tips.html
7.1.2008
Nice list Pádraig, thanks for sharing!
7.1.2008
@Bill – Nano over VIM?
I love VIM, mostly because of the syntax highlighting on the myriad of different file types as they are being edited, I can quickly spot any syntax typos I may have fat-fingered in.
Thanks for sharing this. =]
7.1.2008
I liked the :Sex part. You’ll want if you’re trying to peek at some other files.
7.1.2008
s/Delete \zsall\ze white spaces/trailing/
7.1.2008
what no mention of dot? (repeats the last edit you made)
It’s actually one of my favourite commands.
7.1.2008
I love it. > :Sex Split window and open integrated file explorer
7.1.2008
@carl: I didn’t know that one! thanks for sharing.
7.1.2008
Really love the t + char pattern
For instance
“hi I am foo line” with the cursor on the first “, c+t+” gives you
“” and leaves you in insert mode between the “”
7.1.2008
I would probably add next dozen of my favorites, but at least some esp. useful for programmers:
% – jump to matching parenthesis
=% – indent the code between parenthesis
[[ – jump to function start
[{ – jump to block start
* – search the word under cursor
:new abc.txt – edit abc.txt in new window
7.1.2008
@Jan: Great ones! I should think about creating a second list with all commands submitted by readers
7.1.2008
I have some more things that you might find useful on my VIM site.
7.1.2008
And what does:
:emacs
do?
7.1.2008
@Raffy: Excellent website! I haven’t the time to check it all, but I really liked what I saw.
7.1.2008
ggguG is wrong, that makes the file lowercase. gggUG makes the file uppercase. However, why you should know this command combo over the separate commands gg, gu/U, and G, I don’t know.
7.1.2008
Great list. Thanks. [:wq]
7.1.2008
@Reid: You’re right. Sorry for the mistake! I just corrected it, thanks for telling me.
7.2.2008
gg=G — indent all lines in the file
shift-v curlybracket control-v shift i # escape — adds #’s in front of all of the lines in that chunk of code
let @a=@* — copies the contents of register a to the systemwide paste bin
bufdo %s/something/somethingelse/g — changes “something” to “somethingelse” in all the open buffers
:tabnew — opens a new tab
– Vim rocks.
7.2.2008
I find :Hex more useful than :Sex, even though I like sex :p
7.2.2008
:%s/\r/\r/g Transform DOS carriage returns in returns
doesn’t miss anything here? It simply replaces all x0d with x0d …
7.3.2008
Excellent list.. I have used vi.. but never realised it had so many commands.
7.3.2008
Thanks, shoban. Vi have much more commands than only theses ones
7.3.2008
The pattern to get rid of d part of DOS line-feeds in an entire document is
:%s/^M//g
Where you must input the “^M” pattern by holding down Ctrl while typing “vm”
If you often exchange text files with Macs then this replacement comes in handy
:%s/^M/\r/g
7.3.2008
@Bernhard: Thank you for theses useful contributions!
7.5.2008
Very nice
I would add “set wrap” and “set nowrap” commands. With those commands you can enable and disable text wrapping on vi editor
Great work man
7.5.2008
and what about copy/paste?
7.5.2008
Kiril, I’m glad you liked the list! And thanks for submiting more commands!
7.6.2008
@barf: You’re right, I should have inclued at least basic copy/paste functions (y and p)
7.15.2008
Hi,
That is really a very useful list.
Thanks for the list.
7.30.2008
Thanks, that’s pretty usefull.
But actually:
ctrl-w_ maximize vertically and
ctrl-w| maximize horizontally.
8.1.2008
Wonderful list I must say specially the Explorer commands.
I have been using vi for a long time but didn’t know.
Thanks a lot.
8.13.2008
I have posted a customize vim guide which can be found here Customizing Vim . It goes the next step beyond usage, which is customizing VIM for a better experience.
8.26.2008
vim is the best editor I’ve ever used. Thanks for this really usefull list.
8.30.2008
i can’t stand vi.. but one of the servers i manage doesn’t have anything else on it so it’s all i can use. Thanks for the list, would you mind if i re-blogged this over at my blog? (linking to the source natural)
8.30.2008
@Jamie: No problem!
9.1.2008
Great BLog. I am taking a print of this. Thanks 4 sharing it on web.
9.5.2008
Very useful. It will come handy when searching for Commands
10.8.2008
Thanks for my new bookmark!
One of the best commands is certainly ggVGg?
11.2.2008
Good list. BTW, you can rot13 whole buffer also with “ggg?G”. No need for V, one step less
11.2.2008
Digraphs are a great feature in Vim. To type a special character, hit ctrl-k in insert mode and type two-character code. Check out the list of availale characters with :digraphs.
12.23.2008
Thank you very much for sharing this information. very interesting article for programmers specially for beginners.
1.5.2009
This was very useful. I searched for command to convert text to lower case and I found it here. You can also set all text to the upper case with gggUG.
Cheers!
1.23.2009
Great list, gotta bookmark this. I often forget some of these commands
1.28.2009
Very helpful. Thanks!
2.1.2009
Let me be the next to say: this is a very nice list. I love vi (and vim, too) and there were several tricks that I didn’t know about on the list.
Thanks for posting.
(I came in via Hacker News)
2.1.2009
every once in a while I see a cheat sheet of vi(m) commands. unfortunately most of them forget to teach the basics: motion.
for example:
ggguG is not a random key combination (as begginers tend to believe), its gg (goes to begin of file), gu changes to lowercase, G goes to end of file.
so long,
alexandru
2.1.2009
I’m almost newbie in the church of Vim and I’m enjoining it by learning new things every day.
Nice post. Thanks.
2.1.2009
Thank you so much for this command list. It’s very helpful!
Regards,
Pascal
2.6.2009
This will let you switch between tabs and windows by using a combination of the control key and h,j,k, and l. For example, to move to the top window, you can press ctrl-k. To move to the tab on the right, you can press ctrl-l.
2.10.2009
Since the 70’s, Vi is one of the programmer’s best friends, not friend.
2.14.2009
Hmmm…. I’m learning these tags and the whole article as well as the comments associated with it were very informative. Thanks!
2.19.2009
Vim is sh it.
How many command u MUST know for ONLY editing?
Commands like: “%$#%$#% 4354 543543543543 54rt5^%$^%$^$ ^%$” for change some words?!
BULLSHIT!
2.28.2009
I’m almost newbie in the church of Vim and I’m enjoining it by learning new things every day.
3.2.2009
I graduated with a B.S degree in computer science. Used to do all my programming from telnet at home instead of sitting all day sweating it in the labs. These commands are all very useful and eliminate the need for a GUI interface. Thanks for the great list.
3.5.2009
Great list. i print it. Really useful for programmers.
Thanks for sharing this information
3.8.2009
:%s/old/new/gw Replace all occurences with confirmation
I think the right way is :%s/old/new/gc
Great list. Thanks Jean-Baptiste.
Danilo De Martin
12.29.2009
Hi. your blog was very useful to me. I’ve just started to learn linux and Vi. I found it much enjoyable and interesting. Your list can help me so much.
truly yours,
Thank you.