Vim
10 Vim Commands for a Better Workflow
Vim is an essential tool at Hashrocket. It lets us share a common workspace with anybody, onsite or remote. Despite its unassuming veneer, Vim is a very powerful text editor. Small investments in learning slowly compound into a fast and joyful programming experience.
Here are ten Vim commands worth exploring.
The Setup
To follow along, you'll need Vim installed. Head over to the Vim download page for more information. Most Unix-based systems come with Vim; check to see if you have it by running:
vim -u NONE
This loads Vim without any plugins or custom settings. We don't need them, because we'll be looking at standard Vim commands only. Embrace the minimalism!
:file
One useful command is :file
. This sends the file name, cursor position, and file status to the Vim status bar:
"vim_blog_post.md" 200 lines --0%--
Aliases include CTRL-G
, :f
, and :fi
.
:buffers
Mastering buffers is an essential part of a good Vim workflow. The :buffers
command is your starting point. As I type this, mine looks like this:
:buffers
1 %a + "vim_blog_post.md" line 40
5 #h "show_tell.txt" line 5
Aliases include :files
and :ls
.
CTRL-^
One area Vim seems to lag behind more fully-featured editors is file navigation. The command CTRL-^
is one of many tools that can help.
CTRL-^
switches to the alternate file in your buffer. This is useful when you want to toggle between two related files, such as a unit test and its related model.
:saveas
If you want to save your file as a new file in Vim, you might try writing it with :w {filename}
. This sort of works, but your buffer is still the original file. You'll need to open the new file to proceed.
There is a better way: the :saveas
command. :saveas {file}
saves the current buffer as {file}
and sets the current buffer to {file}
. It's the 'save as' you've always wanted. Add a bang (!
) to overwrite an existing file.
Aliased as :sav
.
:wall
The day is ending and you want to save a work-in-progress commit. Wouldn't it be nice to write every buffer with one command?
The :wall
('write all') command writes all changed buffers. The output will only show your current buffer as having saved, but it writes them all. Add a bang (!
) to also execute on readonly files.
Aliases include :wa
and :wa!
.
:ball
The :ball
('buffer all') command opens a window for each buffer. This is helpful if you are switching back and forth between a handful of files and need to see them all at once.
If you prefer tabs, try :tab ball
. Aliases include :ba
, :sba
, and :sball
.
ZZ
ZZ
and ZQ
are the same as :wq
and :q!
, but require fewer keystrokes. Again, small improvements that compound over time.
:.!
The :!
command is very useful in Vim, allowing you to shell out to programs like psql or rake.
A variant of this is :.!
, which forwards the output of your external command to the current window and line (hence the .
).
Here are a few examples:
# Paste my current directory
:.! pwd
/Users/jwworth/Documents
# Paste a calendar
:.! cal
February 2015
Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
# Share what else I'm working on
:.! ls | grep blog
postgres_blog_post.md
vim_blog_post.md
The practical applications are limited only by your imagination.
K
Another awesome command is K
. Type this while the cursor is over any Unix-based command, and you will be taken straight to that command's manual page!
g CTRL-g
Once you get past the austerity, Vim is a great platform for writing prose.
One tool you'll need when writing prose is a word count. Vim has many possible solutions; perhaps the simplest is g CTRL-g
. Here's the output when I ran that command on this blog post:
Col 1 of 0; Line 111 of 155; Word 748 of 993; Byte 4218 of 5501
Tons of useful information. Step aside, Microsoft Word!
Conclusion
As you can see, Vim is powerful without any customization. Explore its depths, and Vim will repay you with a faster and more joyful programming experience.