My most-used Vim commands

I like Vim.* Without further ado, here are some commands that I use regularly:

gq | Painless line breaking

gq (and gw) will line break your code neatly so that each line is at most 80 characters long. I usually use it in combination with Visual mode by highlighting the lines I want to break. It’s somewhat finicky with /* ... */ style comments, but I especially love it because it (tries to) automatically comment each line. It definitely works well with Python comments:

It's quite convenient to write a long comment without worrying about line breaks.

It’s quite convenient to write a long comment without worrying about line breaks.

shift-V...

shift-V

gq! Yay!

gq! Yay!

{Visual}I | Multi-line insertion

When I first started using Vim, I found it incredibly tedious to fix indentation if I changed the scope of a block of code. This was a game-changer.

I need to scoot this code four spaces to the right in order for it to be properly indented inside of main.

I need to scoot this code four spaces to the right in order for it to be properly indented inside of main.

CTRL-v, jjjjjjjj...

CTRL-v, jjjjjjjj…

i (insert) [whatever I want to insert on each line]

i (insert) [whatever I want to insert on each line]

ESC!

ESC!

Oh no, now the lines are all messed up. If only there were some sort of command that would automatically fix them!

Oh no, now the lines are all messed up. If only there were some sort of command that would automatically fix them!

You can also do deletions (just highlight and press X) and other neat stuff. Experiment!

Note: Since posting this, my friend Andy Moreland messaged me and told me that in Vim, you normally indent / unindent using < and >. This command is still appropriate for when you want to insert text on multiple lines (for example, if you want to comment out a bunch of code), but maybe not the best way to do indentation. The dangers of learning by googling!

zf, zo, zm, zd | Folding / Collapsing

I want to collapse the main function so I can just look at the code that I'm working on.

I want to collapse the main function so I can just look at the code that I’m working on.

Our best friend, visual block mode comes to the rescue! Highlight everything you want to collapse. I usually leave the comments; the reason will be clear in a moment.

Our best friend, visual block mode comes to the rescue! Highlight everything you want to collapse. I usually leave the comments; the reason will be clear in a moment.

zf - The highlighted code has been collapsed! As you can see, the first line of the block is the preview, which is why I like to leave the comments unfolded and see which function I folded.

zf – The highlighted code has been collapsed! As you can see, the first line of the block is the preview, which is why I like to leave the comments unfolded and see which function I folded.

Use zo to open a code fold, zm to toggle, and zd to delete permanently.

:split, :vsplit | Editing multiple files

This didn’t really matter to me until I took CS142 and realized I would be simultaneously editing HTML and CSS files at the same time on a regular basis. But now, I absolutely love being able to edit multiple files without messing around with multiple terminal sessions.

Here's my HTML file and a window showing the files in this directory. Totally going to open source Legit.js at some point.

Here’s my HTML file and a window showing the files in this directory. Totally going to open source Legit.js at some point.

To split vertically, type :vs. You'll then have two side-by-side windows of the same code. To navigate, press CTRL-w, and then use HJKL to move your cursor in the direction of the window you want to switch to. For example, here, I would type CTRL-w l (that's a lowercase L) in order to switch to the right-hand window.

To split vertically, type :vs. You’ll then have two side-by-side windows of the same code. To navigate, press CTRL-w, and then use HJKL to move your cursor in the direction of the window you want to switch to. For example, here, I would type CTRL-w l (that’s a lowercase L) in order to switch to the right-hand window.

I switched over to the right-hand window. Now, :E to "explore" the files in this directory. Use J/K to move up and down and hit enter on the file you want to open. Alternatively, I could have skipped the filesystem altogether by writing ":o styles.css" -- however, most of the time I don't feel like typing out whole filenames, so I just get there from the directory.

I switched over to the right-hand window. Now, :E to “explore” the files in this directory. Use J/K to move up and down and hit enter on the file you want to open. Alternatively, I could have skipped the filesystem altogether by writing “:o styles.css” — however, most of the time I don’t feel like typing out whole filenames, so I just get there from the directory.

Yay, HTML and CSS side-by-side! Just use CTRL-w to move back and forth!

Yay, HTML and CSS side-by-side! Just use CTRL-w to move back and forth!

Sometimes, though, you're me and you want to have a window open on the side (maybe a blog post or a Rails cheat sheet?) -- in which case, horizontal split (:split) might be relevant. For future reference, :sp and :vs are the short forms of horizontal split and vertical split, respectively. Yes, I'm annoyed that it's not :hs, as well.

Sometimes, though, you’re me and you want to have a window open on the side (maybe a blog post or a Rails cheat sheet?) — in which case, horizontal split (:split) might be relevant. For future reference, :sp and :vs are the short forms of horizontal split and vertical split, respectively. Yes, I’m annoyed that it’s not :hs, as well.

Just don't get crazy.

Just don’t get crazy.

If you want to save and close every window at once, use :xa.

Miscellaneous

    • vimtutor: If you’re just starting out with Vim, this post might have been pretty nonsensical. For people completely new to Vim, I would strong recommend using vimtutor — just open your Terminal and, assuming you have Vim installed already, type in “vimtutor” to get started. It’s a great way to learn the basics of Vim!
    • :colorscheme [name]: In the HTML example, I’m using elflord, which is probably my favorite default colorscheme. In every other example, I’m using an edited version of zenburn, which I think is a beautiful colorscheme. zenburn normally comes with a solid gray background, but I enjoy having a transparent terminal (I put coding resources or YouTube videos in the background because I need more screen space…), so I’ve been trying to make the zenburn colorscheme transparent rather than opaque. I forgot to change back to zenburn when I switched to the HTML example above, hence the inconsistency in the pictures.
    • ~/.vimrc: If you have default settings, such as a colorscheme, to be used every time you open a file, edit your vimrc file (“vim ~/.vimrc“). Any commands in here will be run immediately on startup. You can do some pretty ridiculous things with vimrc, but I don’t know very much about it yet. My file is pretty bleak:
Note that you don't need to prepend commands with a colon.

Note that you don’t need to prepend commands with a colon.

Closing

In general, I refuse to memorize Vim shortcuts (or any syntax, for that matter) before I have to use it. It just doesn’t stick in my mind until I have a use case for it. I didn’t need these commands when I started because they just weren’t necessary to get the basic functionality Vim. I learned them gradually–I’ve been using Vim for less than a year–and didn’t make it a goal to learn as much about Vim as possible. The point is, don’t memorize. If you’re working with Vim and you think “I wish there were a convenient way to do X,” just google it and see what you find! Incorporate Vim commands into your workflow gradually, rather than reading about all the commands and forgetting about them later on. Honestly, I don’t think Vim is fun at first, when you’re still constantly in insert mode and navigating with the arrow keys rather than HJKL. But as you get more comfortable, Vim starts to reveal more of itself to you and it becomes a lot of fun to learn new commands!

* I wasn’t sure whether to stylize Vim as “vim” or “Vim” and it was really frustrating trying to get a consensus on this. The title bar of the Vim website writes it in lowercase, but on the website itself, they capitalize it. Wikipedia also capitalizes, so I went with Vim. Up until now, I had been exclusively writing it as “vim,” though, so “Vim” just looks wrong right now.

3 thoughts on “My most-used Vim commands

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>