Skip to main content

Vim: Getting Started With Vim

A couple of readers asked me to do an overview of how to get started with Vim. I'm sure there are far better overviews than mine, but here are some personal notes:

Be sure you take the Vim tutorial. You can use "vimtutor" from the shell command line.

When you're taking the tutorial, learn how to use "hjkl" instead of the arrow keys. Not having to use the mouse or the arrow keys and rarely needing to use the control key is one of the great benefits of being a Vim power user.

It's best to get Vim out of compatibility mode. You can do this by creating a .vimrc file (even an empty one) in your $HOME directory.

Next, I personally prefer to use gvim. Using the gvim command starts Vim in GUI mode. I like the GUI menus, etc. Of course, you'll need a version of Vim with GUI support. There are GUI versions of Vim for Windows, Linux, Mac, etc.

Next, if you can get Vim 7, do that. It has nice, native tabs and a bunch of other useful features.

In response to a reader's question, I think the auto folding comes builtin. I've never used it, so I can't say more. I have seen it though.

There's a Vim cheat sheet.

Here's a list of my favorite Vim commands copied from my notes:
  Format something: <highlight>gq<or motion>
Set a map:
:map <key> <several keystrokes>
Don't be afraid to use C-V to type special characters.
Visual mode (so you can visually highlight text):
v: highlight by characters
V: highlight by lines
C-v: highlight vertical blocks
<highlight><cmd><or motion>
cmd may be either "<" or ">"
You often follow this up with one or more .'s
Named buffers: "<letter><some cut or paste like command>
Create: q<letter><go about your business>q
Use: @<letter>
If you're on a search and replace mission, end your macro with a search for
the next thing you need to search for. Otherwise, your "search next"
will probably have been blown away during the macro.
@@ will repeat the last macro you executed.
Where was I?
^o: go backwards
^i: go fowards
This will even change your file!
m<letter>: Set a marker named <letter>
'<letter>: Jump back to that marker
cmdline mode:
"q:": Enter into cmdline mode.
^f: Enter into cmdline mode in the middle of typing a command.
omni complete:
^x^o: autocomplete code
I have a pretty simple, self-explanatory .vimrc file. About the only thing complex is that I specify how to indent various programming languages based on my psychotic understanding of the various style guides for those languages. To learn more about any of these settings, use "help somesetting" withing Vim:
" These are general settings.
set guifont=Monospace\ 9
syntax on
colorscheme torte

" autoindent works better if you fix the backspace key.
set backspace=indent,eol,start

" "T" toggles the taglist for ctags.
map T :TlistToggle

" These are settings for various file types.
au BufRead,BufNewFile *.c,*.h,*.cpp set ai sw=4
au BufRead,BufNewFile *.css set ai sw=4 sts=4 et
au BufRead,BufNewFile *.html set ai sw=2 sts=2 et
au BufRead,BufNewFile *.java set ai sw=4 sts=4 et
au BufRead,BufNewFile *.js set ai sw=4 sts=4 et
au BufRead,BufNewFile *.mhtml,*.mas set ai sw=2 sts=2 et ft=mason
au BufRead,BufNewFile *.ml,*.mll,*.mly set ai sw=2 sts=2 et
au BufRead,BufNewFile *.php,*.php3,*.php4 set ai sw=2 sts=2 et
au BufRead,BufNewFile *.pl,*.pm set ai sw=4 sts=4 et
au BufRead,BufNewFile *.py set ai sw=4 sts=4 et tw=72 " Doc strs
au BufRead,BufNewFile *.scm set ai sw=2 sts=2 et
au BufRead,BufNewFile *.sh set ai sw=4 sts=4
au BufRead,BufNewFile *.sql set ai et
au BufRead,BufNewFile *.tmpl set ai sw=2 sts=2 et
au BufRead,BufNewFile *.txt set ai sw=2 sts=2 et tw=79
You can download plugins for Vim from the Vim Web site. The taglist plugin is a must have for using ctags. I use this to see all of the functions and classes in the Python module I'm currently editing. The minibufexpl plugin is useful if you're not using Vim 7. It shows all of your open buffers in fake tabs. Be sure to add "set hidden" to your .vimrc if you use it so that you don't have to save the current buffer before switching to another one.

Vim 7 has some neat features such as an improved ability to edit and manage files over scp. I'm just getting started with it, but "gvim scp://" is already useful.

Also, I should mention something about workflow. IDE users and Emacs users rarely have more than one copy of their editor running at any one time. Since startup in Vim is so fast, I must admit to opening up multiple gvim windows all the time. I might have 5 of them spread across my screen at the same time. However, it's now possible in Vim 7 to open up a document from the command line in a new tab within the currently running editor: "gvim --remote-tab file.txt".

Furthermore, I should state that I don't use Vim as an IDE replacement. If you want a full IDE for Python, I can recommend Wing IDE. I don't use an IDE. Rather, I use Vim as an important part of my "development environment". I'm still very shell oriented. I use the shell for all sorts of things such as file management, Subversion, grep, find, wc, etc. Recently, I switched to zsh, but that's a topic for another post ;)

To finish this post, as well as to give a flavor for how I use the shell and Vim together, I'll show you how I do global search and replace across my entire project. Start at the top of the project in your shell. Then do: "gvim $(grep -rl sometext .)". This will open up all the files of interest. Then, "/sometext". This will jump to the text you're looking for. Use the "c" command to change the text in one shot. Now, "n" to jump to the next instance and "." to change that instance too. At the end of the file ":wn" to write and jump to the next file. Following this "work idiom" you can make each change by hand, but still do so really fast: "n.n.n.n.:wnn.n.n.n:wn". To make more complicated edits, I follow the same pattern, but I use a macro to change the text.

Happy Hacking!


Anonymous said…
Actually gvim has had the ability to open up new files in an existing gvim instance for a while:

% which gv
gv: aliased to gvim --remote-silent

I alias gv and use it for that purpose, when I want to. One annoyance which I've not looked into is that gv will cause my gvim instance to move into my current "desktop". That's no doubt an X window manager issue.
jjinux said…
I should have known ;)
Batok said…
you can use vim inside wing, that is with the "personality" of vim instead of vanilla editor.
jjinux said…
> you can use vim inside wing, that is with the "personality" of vim instead
> of vanilla editor.

You mean you can use Vim keybindings. Yeah, I know. I spent a lot of time trying it out. I even submitted multiple bugs. It's good, but not quite right. It caused me a lot of pain until I finally gave up. For instance, shift-V (vertical highlight) jjj (down, down, down) > (shift right) ... (again again again). It should shift the whole block to the right multiple times, but it doesn't.

Hopefully it'll continue to improve. Those guys were super responsive to all of my bugs. I have high hopes.

Thanks for the comment!
Anonymous said…
Thanks, although I am not yet able to make vim able to work with scp and shell command, but tabbing is what I most want
Anonymous said…
Thanks, although I am not yet able to make vim able to work with scp and shell command, but tabbing is what I most want
Anonymous said…
This is cool. I've started using cream (cause I'm not quite sophisticated enough for real vim), but I have it in expert mode so it's just like a vim with pretty icons. I just went through the tutor one bored afternoon and now I'm using the key bindings all the time (apart from to change colour scheme). I'm quite glad as I was having trouble finding an editor that I'm happy with for all platforms. I've bought a book on vim because even if you can't grep them, you can read them in bed. Anyway, my advice to anyone going through the tutor is, don't think this is insane, esoteric and antiquated, just do the tasks until you're quick at them. You'll fit them to the code writing/text editing when you actually start doing it.
jjinux said…
> This is cool...


I know Vim has a terrible learning curve, but once you have it baked into your mental firmware, it really does make you ultra fast at editing text.

Popular posts from this blog

Drawing Sierpinski's Triangle in Minecraft Using Python

In his keynote at PyCon, Eben Upton, the Executive Director of the Rasberry Pi Foundation, mentioned that not only has Minecraft been ported to the Rasberry Pi, but you can even control it with Python. Since four of my kids are avid Minecraft fans, I figured this might be a good time to teach them to program using Python. So I started yesterday with the goal of programming something cool for Minecraft and then showing it off at the San Francisco Python Meetup in the evening.

The first problem that I faced was that I didn't have a Rasberry Pi. You can't hack Minecraft by just installing the Minecraft client. Speaking of which, I didn't have the Minecraft client installed either ;) My kids always play it on their Nexus 7s. I found an open source Minecraft server called Bukkit that "provides the means to extend the popular Minecraft multiplayer server." Then I found a plugin called RaspberryJuice that implements a subset of the Minecraft Pi modding API for Bukkit s…

Apple: iPad and Emacs

Someone asked my boss's buddy Art Medlar if he was going to buy an iPad. He said, "I figure as soon as it runs Emacs, that will be the sign to buy." I think he was just trying to be funny, but his statement is actually fairly profound.

It's well known that submitting iPhone and iPad applications for sale on Apple's store is a huge pain--even if they're free and open source. Apple is acting as a gatekeeper for what is and isn't allowed on your device. I heard that Apple would never allow a scripting language to be installed on your iPad because it would allow end users to run code that they hadn't verified. (I don't have a reference for this, but if you do, please post it below.) Emacs is mostly written in Emacs Lisp. Per Apple's policy, I don't think it'll ever be possible to run Emacs on the iPad.

Emacs was written by Richard Stallman, and it practically defines the Free Software movement (in a manner of speaking at least). Stal…

ERNOS: Erlang Networked Operating System

I've been reading Dreaming in Code lately, and I really like it. If you're not a dreamer, you may safely skip the rest of this post ;)

In Chapter 10, "Engineers and Artists", Alan Kay, John Backus, and Jaron Lanier really got me thinking. I've also been thinking a lot about Minix 3, Erlang, and the original Lisp machine. The ideas are beginning to synthesize into something cohesive--more than just the sum of their parts.

Now, I'm sure that many of these ideas have already been envisioned within, LLVM, Microsoft's Singularity project, or in some other place that I haven't managed to discover or fully read, but I'm going to blog them anyway.

Rather than wax philosophical, let me just dump out some ideas:Start with Minix 3. It's a new microkernel, and it's meant for real use, unlike the original Minix. "This new OS is extremely small, with the part that runs in kernel mode under 4000 lines of executable code." I bet it&…