vi isn’t good because it “doesn’t use the mouse” or because its “modal” or because its “a command line program” These attributes are all helpful (especially to prevent Repetitive Stress Injury), but they miss the point. vi is good because it has a broad, composable text editing vocabulary, supports macros, and sed commands.
In this tutorial, we’re going to discuss how to use vi to type faster. We’ll discuss everything you need to know in 15 minutes or less.
First, install vi everywhere
Whether you’re learning vi or you’ve used vi for a while, the first thing you should do is enable yourself to use vi in as many programs as possible. You should install vi mode for your IDE, shell, git, and web browser.
IDE
Notepad++ used to have ViSimulator, but it’s been abandoned. Try it at your own risk.
Bash
Add this to your .bashrc to get vi mode in your bash terminal.
set -o vi
The one difference between this and normal vi is that bash starts in insert mode, press escape to get to normal mode.
Set the visual editor variable to vim. Some bash programs use this to determine which editor to open.
VISUAL=vim
EDITOR=$VISUAL
Alternatively if you prefer an editor like sublime and you’re using it in vi mode, that works too.
VISUAL="subl -w"
EDITOR=$VISUAL
Git
Git uses its own config to decide which editor. Type the following into a shell to use vim directly:
git config --global core.editor "vim"
or your own editor like sublime with a vi plugin
git config --global core.editor "subl -n -w"
Web Browser
Vimium for the web browser is not about using vim for text fields in the browser, it’s a vim-inspired way to browse the internet. It uses the same navigation commands (g, GG, h, j, k, l) but otherwise uses letters differently. However, it is a must have because it makes web browsing an almost mouseless experience which is a huge Repetitive Stress Injury (RSI) deterrent and a huge productivity improvement given how mouse intensive clicking links is.
There are plugins for all major browsers:
I recommend disabling vimium on gmail and any other sensitive pages like bank accounts. Vimium makes it very easy to click a link, which is great except when that link is “send $10,000.”
Don’t jump into vim right away
One of the counterintuitive tricks to learning vi is not to learn it by using the vim editor. Vim is substantially different from most other editors. Jumping straight into it is like landing in Russia without a dictionary and trying to read the signs. It’s much better to learn russian by going to a russian neighborhood in the US where signs are both in Russian and English. The equivalent here is to learn vi within an editor you already know how to use. The editor provides everything you need for things like managing files and tabs, so you can focus on learning the vi editing convention. If you’re comfortable with sublime, use sublime with vi mode. If you’re happy with VS Code, keep using VS Code with vi mode. Learn a few navigational commands, and incorporate them into your sublime workflow. If you never leave sublime for vim, as long as you’re leveraging the vi plugin, you’re typing faster. And if do switch to vim, you’ll be familiar with the basics of editing and it’s not so intense to learn editor specifics like managing files and tabs. It’s like going to Russia, but with a preliminary understanding of how to read and order food, it’s still tough but at least you won’t starve.
vi’s Broad Vocabulary
vi supports a rich vocabulary of text navigation, text selection, and text editing.
Text Navigation
In the default text editing convention, you can only express navigating to the beginning/end of a line or file, and moving forward/back one character, word, line, or page at a time.
Vi supports these and:
Moving N characters, words, lines, pages, sentences, and paragraphs
Move to beginning or end of a word (in the default convention, moving right one word at a time forces your cursor to the end of words and moving left one word at a time forces your cursor to the beginning of words)
Go to line N.
Find matching block {} [] ()
Go back to beginning of block started by { [ (
Text selection
In the default text editing convention, text selection is done in the same way as text navigation.
In vi, text selection can be done this way as well (using “V”), with vi’s more expressive navigational commands, as well as selecting N lines. When working with code, lines of code are often copied wholesale making this quite useful. For example, copying a few lines of code and performing a find and replace to change the variable is quite fast in vim. To copy 3 lines and replace the name first with second in all 3 in all locations, just type.
3yyp3js:/first/second/g3
Pretty slick.
Text deletion
In the default text editing convention, text can either be deleted one character, word, or selection at a time.
In vi, text can be deleted in the same way it can be navigated as well as N lines at a time. Deleting whole lines comes in handy quite often.
Text Insertion
To write text, vi supports insert mode in which text is inserted just like any other. It also supports indenting lines in normal mode which is extremely useful:
N>>: indents N lines once to the right.
N<<: outdents N lines once to the left.
To indent thrice, type N>>.. What does . do? Read on.
Macros
.
The first type of macro is ".", pronounced dot. Typing . will redo the last vi command. Need to keep indenting? Use .
q and @
The second type of macro is recorded with q and unleashed with @. Unlike ., it can redo an arbitrary number of vi commands. For example, if you’re refactoring 10 consecutive lines of code all in the same way. Type qw<refactor the first line>j0q 9@w (or replace w with the character of your choice) and vim refactor the next 9 lines instantly. As an aside, vi plugins are slower than vim but faster than you and save you from RSI.
What makes vi macros really great is that since they repeat vi commands, you can write a sequence of vi commands which will work the same on code of varying string length, word length, and letter composition. The default convention makes you stuck with only describing one word and one character at a time. Your lines might have varying numbers of words, which isn’t a problem for vi, but in the default convention, you can’t automate your refactor. And even when you can automate it, you’ll spend forever trying to get the cursor to get it at the beginning of a word after moving to the right. No thanks.
(s)ed commands
Remember sed? Well, vi supports all sed commands (vi and sed have a common ancestor, ed). Any tricks you learn in sed automatically transfer to vi, just type a colon first. Want to find and replace on a line? Use the sed command, prepended with a colon.
:s/find/replace/
One difference between sed and vi: sed by default operates on all lines, vi only operates on the current line. To operate on all lines, use %
:%s/find/replace/
One caveat is that sed commands other than find and replace are often not supported in vi plugins for other editors.
Learning vi
vi doesn’t have a steep curve because of the memorization. vi has a steep curve because you need to learn a new way of thinking. You no longer think in “move cursor to before the code on the 10th line, and type tab, then go down a line and type tab and go down a line and type tab.” You think, “goto line 10, indent 3 lines.” Thinking like this cannot be learned with a cheat sheet. You need to learn from others. Invest $25 and time in Vim Adventures (no affiliation) and watch other people using vi. If you’re at a tech company, ask a senior engineer if you can “shadow” him while he uses vim (don’t be shy, he’ll be flattered). You’ll learn a lot and build your professional network.
.vimrc (vim configuration)
The vim editor is incredibly configurable and has tons of plugins. It shouldn’t. Vim is a text editor and should be used for one thing only: text editing. Vim is not a development enviromment. That would be unix. Don’t use a git plugin in vim. Use a git program in unix.
For reference, here’s my .vimrc (the vim configuration file) with added comments:
" double quote starts a comment in vimrc
" show line numbers on the left side of vim
set nu
" add the showbreak character when line wrapping long lines
set showbreak=↪\
" Explicitly render different types of whitespace differently
" and render trailing spaces.
set list listchars=tab:→\ ,nbsp:␣,trail:•
" Use the delek colo(r scheme).
colo delek
" The tab button creates 4 spaces, backspace deletes 4 spaces " vi indents and outdents are 4 spaces
set expandtab
set tabstop=4 softtabstop=4 shiftwidth=4
" Language dependent indentation.
autocmd FileType html setlocal ts=2 sts=2 sw=2
autocmd FileType javascript setlocal ts=2 sts=2 sw=2
autocmd FileType css setlocal ts=2 sts=2 sw=2
autocmd FileType ruby setlocal ts=2 sts=2 sw=2
The most important thing is what’s not here. There are no plugins. No Vundle. No integrations. Just a few simple options.
Statically Typed Languages
Not using plugins makes it hard to write in a statically typed language like Java. The solution is easy: don’t use vim. Use an IDE like IntelliJ that’s built for handling statically typed languages and install a vi plugin.
Conclusion
We’ve now seen some big ideas about how to use vi and vim to type faster. We saw especially how we can use vim to automate typing through macros. Next week, we’ll continue this idea and jump into the world of automating typing. If you don’t want to miss out, just click Subscribe now below.
I remember how painful was learning vi. But it was fun also although. I still have that beep sound in my head happening everytime I made a mistake with vi.