Dear LazyWeb,

vim Though I've used GNU Emacs as my primary code/email/todo list editor for many years now, I'd like to give Vim a fair shake.

Now I know the most basic of vi editing but never really got past the 8-12 keystrokes that I learned back in college. So, loyal readers and geeks, I ask you to bestow upon me your favorite on-line Vim references, tips, tricks, tutorials, references, and so on.

This isn't a direct result of my hand injury... I've been thinking that I should do this for a couple years now. But it sure did help get me motivated finally.

I'll be using vim on both Linux and Windows, not that it matters much for most things (I hope).

:wq

Posted by jzawodn at April 02, 2008 12:54 PM

Reader Comments
# Matt Cutts said:

To select text visually:
V, then move with cursor keys, then y to yank that text into a buffer.

When you copy and paste text with line breaks, sometimes your paragraph gets ragged. To re-format a paragraph:
gqap

Also, this Vim book is surprisingly readable:
http://www.amazon.com/iMproved-VIM-Landmark-Steve-Oualline/dp/0735710015

on April 2, 2008 01:12 PM
# Mohan said:

I liked this one a lot - got it from slashdot
http://www.eandem.co.uk/mrw/vim/usr_doc/index.html

on April 2, 2008 01:18 PM
# Michael Stoppelman said:

Ah, Jeremy, you must know the powers of the split screening if you don't already.

:new -- this creates a new screen so you can edit multiple files, then you can use ctrl-w + j OR ctrl-w + k which allows one to hop up and down between those splits, respectively.

on April 2, 2008 01:39 PM
# Nick Berry said:

FYI, you can use "ZZ" instead of ":wq" (one-hand friendly)

on April 2, 2008 01:52 PM
# A. Reader said:

To fix transposition (like in "tyep") put your cursor on "e" then type x then p

on April 2, 2008 02:00 PM
# Dave said:

I refer to this list quite frequently:

http://www.rayninfo.co.uk/vimtips.html

on April 2, 2008 02:09 PM
# Charles Eicher said:

If you want a really left hand friendly editor, I recommend...


...Wordstar!

on April 2, 2008 02:26 PM
# Nigel said:

Mmmm, you've sparked my interest in vim again, I really only use emacs to manage multiple window handling, the rest is in viper, I generally have 2-3 full editing windows open, ie. not split window, but 2-3 seperate windows, each one is usually split in 1/2, but going back to a single window with splits would be a deal breaker for me & I'd stick with xmemacs.

on April 2, 2008 03:06 PM
# Geoff Moller said:

* (when over a word);
`` (To the position before the latest jump, or where the last "m'" or "m`" command was given);
marks (-> ':help mark(s)');


Window management:
sp, vsp, et al (-> ':help sp');
all tab commands (-> ':help tabe');
all buffer commands (-> ':help buffer');
mks (becomes very handy when combined with 'split' and 'tab'; open session file with 'vim -S sessionfilename');

on April 2, 2008 03:43 PM
# said:

You can probably do fine with the basic stuff you learned way back when. I've been happily working with vi and vim for ::coughcough:: years and I mostly just use the basics.

My killer vim command? . (Repeats last edit. Priceless.)

on April 2, 2008 03:53 PM
# John Herren said:

Steal some .vimrc:

http://dotfiles.org/.vimrc

on April 2, 2008 04:03 PM
# cole said:

When in insert mode:
ctrl + p
ctrl + n

These will match the word your currently typing to any words listed before (p) or after (n) and will virtually eliminate all typos (or cause your typo to be repeated continually throughout your file) and should improve typing speed (especially for long function names you have to continually call).

To fix the issue of your typo being repeated throughout your file, when in command mode:
:%s/old value/new value/g
or to verify each change
:%s/old value/new value/gc

on April 2, 2008 04:04 PM
# Ricardo Dias Marques said:

Hi Jeremy,

Besides the good tips and links that other readers have already pointed out, I like a lot one presentation, authored by the author of Vim himself - Bram Moolenaar.

The presentation is called "Seven habits of effective text editing 2.0" and it is available in PDF format (with notes) at:
http://www.moolenaar.net/habits.pdf

The presentation is also available as a 1H20m video in Google Video at:
http://video.google.com/videoplay?docid=2538831956647446078

Both links are listed in Bram Moolenaar's page about vim at:
http://www.moolenaar.net/vim.html

BTW: Can't you get fired by Yahoo if you start using a text editor, namely vim, that's developed by someone currently employed by Google? ;-)

http://en.wikipedia.org/wiki/Bram_Moolenaar

on April 2, 2008 04:08 PM
# Ryan Grimm said:

Here are some bits from my vimrc:

:set ruler (shows your line/column position in the document)
:set ts=4 (sets your tabstop to be 4 characters)
:set sw=4 (sets the shift width to 4 characters, check out :help >> for more info on shifting)
:set bg=dark if you use a black terminal
:set backspace=indent,eol,start (allows delete to work over multiple lines)
:set nu (throws in line numbers)
:set ai (automatic indentation)
:set hls (highlights the search matches)

When using vim I can't go a day without using:

When over a word and not in insert mode, * will copy that word into your search buffer and take you to the next match.

When in insert mode, ctrl-n will auto-complete the word based off of other words in the files you have open, very handy for function names and whatnot.

When you aren't in insert mode, >> will indent a line of text one tabstop, << will do the opposite.

Also as someone else mentioned, visual mode is very handy. It allows you to perform tasks over a selection of text. That task my be copying the text, deleting it, executing a search and replace on it, reformatting it, indenting, etc. There are three ways to get into insert mode, v, V and ctrl-v. Lower case v allows you to select on a character by character basis, upper case V allows you to select lines and ctrl-v allows you to select blocks (eg: the columns 5-25 on lines 10-50). It's well worth playing around with visual mode and all that you can do with it.

on April 2, 2008 04:33 PM
# Jon Keating said:

I second the link that was mentioned above (http://www.rayninfo.co.uk/vimtips.html)

Also, check out the tips at www.vim.org: http://tinyurl.com/2zatfl

on April 2, 2008 06:36 PM
# Sisir Koppaka said:

@Nick Berry: Ah, but, for a QWERTY touch typist, both wq and ZZ use only one hand..

on April 2, 2008 08:13 PM
# brad said:

Seriously, if your still using emacs or vim or any variation of them in 2008, you are riding the failboat.

on April 2, 2008 08:15 PM
# Hanan Cohen said:

Whenever in need, I search for something + "cheat sheet".

Works every time.

on April 2, 2008 09:23 PM
# Anonymous said:

@Sisir Koppaka: it's :wq, not just wq. The ':' character requires use of the right hand for a QWERTY touch typist.

on April 2, 2008 10:42 PM
# Eric Wagner said:

the o'reilly vi book is old, but pretty solid. the problem with the cheatsheet approach to learning vim is that it doesn't teach the overriding metaphor of how the editor works. for example, arrow keys must be banished. moving around while in insert mode ruins the whole purpose of using vi. and then, once you get that, the :h help is exhaustive and generally well written.

on the down side, you will type into every text field you see.

on April 2, 2008 11:05 PM
# said:

that was <esc> into every text field you see.

on April 2, 2008 11:08 PM
# Pavan yara said:

The wikia page on vim is a good reference page.
Url: http://vim.wikia.com/wiki/Main_Page.

Seems there are about 1604 article pages in the wiki.

on April 2, 2008 11:51 PM
# Henrik said:

use ctrl+v to select text vertically, very cool feature!

on April 3, 2008 03:08 AM
# Doug said:

Hanging on my cube wall is a very handy cheat sheet:
http://www.viemu.com/a_vi_vim_graphical_cheat_sheet_tutorial.html
Helps me quickly get around and discover new features.

on April 3, 2008 04:27 AM
# Scott Y. said:

Got a messy unindented Perl code from the previous maintainer?

1. Go to the beginning of code block '{'
2. Press 'v' to start visual mode
3. Press '%' to move to the end of code block
4. Press '=' to get Vim to re-indent everything

on April 3, 2008 04:52 AM
# Fledsbo said:

Best tip: Sit next to someone who knows vim *really* well.

Random tip:
Ctrl-A and Ctrl-X to increment and decrement nearest number.

on April 3, 2008 05:50 AM
# Michael B. Trausch said:

I have to say, I never really learned a great deal about vi(m) myself, even though I used it for a very long time for text editing. It has (some) features, but the pure extensibility of Emacs finally got me to switch to it in the past two years or so.

That having been said, I think it's probably best to learn the "lowest common denominator" of vi so that your knowledge with it is portable to the varied UNIX platforms you're still likely to encounter in the world. Not all of them have featureful implementations of vi such as vim. I don't have any tips for you, though, other than to say this: I think vi is great for editing basic text, but I think that if you require the power of a full development environment, you should probably use Emacs. I am not sure how extensible modern vim is, but I can't imagine it'd make quite as good an IDE as Emacs. :-)

on April 3, 2008 06:26 AM
# Glen said:

http://tnerual.eriogerg.free.fr/vim.html

Good quick-reference card. I've printed it out, and had it hanging on my wall for nearly 5 years. Just last week I found some *cool* new stuff in vim :)

on April 3, 2008 06:51 AM
# Casey said:

Here's a great cheat sheet I found years ago:

http://simpletutorials.com/tutorials/vim/vimquick.pdf

on April 3, 2008 07:22 AM
# Mike Schilli said:

You should definitely subscribe to the Yahoo-internal devel-vim mailing list which sends out a "vimaday" tip every day.

on April 3, 2008 07:58 AM
# Dave said:

Say you have something like ":set textwidth=70" to wrap your typing at 70 chars. You type a few paragraphs, but do some editing and end up with a bunch of lines of varying lengths, some longer than 70. Just move to some start point and type "gwG"; it'll reformat the rest of the file to respect your textwidth setting.

on April 3, 2008 08:20 AM
# said:

If you are going to use vim for the long term it is worth reading the built in help docs. I read them all about 6 year ago and it was worth it. Stuff I couldn't live without:

ctrl+p - search for similar words (variable names) and take a guess at what you are trying to type.
ctrl+shift+v - create a visual vertical+horizontal range. search and replace, delete, etc.
EnhancedCommentify.vim - plugin script for commenting code
setlocal spell spelllang=en_us - spell check
mutt - advanced text editing speeds up email (abbreviations for urls multiple sigs, etc) and piping messages through external scripts can really help if you are supporting a product
subversion - edit commit notes with vim put everything edited with vim in subversion

Next project is to figure out LaTeX and use vim for all word processing.

on April 3, 2008 09:29 AM
# ch said:

db_ext.vim - very useful for database access from vim
tvo.vim - the vim outliner. todo lists, notes, bookmarks, everything that needs to be remembered in an outline

on April 3, 2008 10:23 AM
# Greg Whitescarver said:

I'm very surprised no one has mentioned this. Go to the command line on virtually any Mac or Linux machine, and type 'vimtutor'. It's a great hands-on tutorial for vim, and the fastest way I know of to get into the vim groove.

Also, you need to come up with a ~/.vimrc file that gives vim the default behavior you need. Here's some of mine; try it out:

set autoindent
set nocompatible
set autoindent
set smartindent
set incsearch
set backspace=indent,eol,start
set showmode

if &t_Co > 2 || has("gui_running")
syntax on
set nohlsearch
set hl=l:Visual
endif
set tabstop=4
set shiftwidth=4

"add to list of matchpairs
set matchpairs+=
" show me my bracket match
set showmatch
" show me my matched pair FAST
set matchtime=2

on April 3, 2008 11:22 AM
# Greg Whitescarver said:

Beware... my posted .vimrc lost a lot of characters in the comment submission. Sorry! Feel free to IM me with quick vim questions.

on April 3, 2008 11:28 AM
# Joe 'Zonker' Brockmeier said:

I have some Vim resources on my blog, including links to a series that I wrote on using Vim. Maybe you'll find it useful. http://www.dissociatedpress.net/vim-resources/

on April 5, 2008 11:27 AM
# Peter Lawless said:

Maybe I am just a plain marketeer and non-techie, but what the heck does a VIM do when it is at home, what will it do for me, and why would I really, really want one!

Peter

on April 5, 2008 12:22 PM
# Richard Toohey said:

So all we need to do to convert all the poor lost souls using Emacs is break one of their fingers, and then they'll use the One True Editor? 8-)

@brad - if you still cannot get your/you're right in 2008 then you are on the "failboat."

@Peter Lawless ... http://en.wikipedia.org/wiki/Vim_%28text_editor%29 and why would you need one? http://www.pragprog.com/the-pragmatic-programmer/extracts/tips ... "Use a Single Editor Well
The editor should be an extension of your hand; make sure your editor is configurable, extensible, and programmable."

@jeremy - get well soon, can't really add much more to the tips that you have got already

on April 6, 2008 03:22 AM
# Tony Pearson (IBM) said:
on April 8, 2008 01:47 PM
# Adrian said:

http://www.vi-improved.org/tutorial.php best tutorial I've seen.

on April 9, 2008 05:27 AM
# Jamie said:

Let me answer my own question. Python and vim setup tips:
http://blog.sontek.net/2008/05/11/python-with-a-modular-ide-vim/

on May 11, 2008 11:01 PM
# Steve said:

http://spf13.com/content/my-custom-vimrc-file-vim

Apparently the comment parser stripped my link. My Customizing vim guide is located at the link at the top of this comment.

on August 13, 2008 07:12 AM
Disclaimer: The opinions expressed here are mine and mine alone. My current, past, or previous employers are not responsible for what I write here, the comments left by others, or the photos I may share. If you have questions, please contact me. Also, I am not a journalist or reporter. Don't "pitch" me.

 

Privacy: I do not share or publish the email addresses or IP addresses of anyone posting a comment here without consent. However, I do reserve the right to remove comments that are spammy, off-topic, or otherwise unsuitable based on my comment policy. In a few cases, I may leave spammy comments but remove any URLs they contain.