Vim for Developers: Part 2 — Advanced Basics

David Ondrich
The Startup
Published in
7 min readJul 1, 2020

--

Did You?

Check out the previous posts in this series if you haven’t already!
Vim for Developers: Part 0 — Why Vim?
Vim for Developers: Part 1 — The Basics

TL;DR — If you’re somewhat familiar with NeoVim/Vim and you’re just looking for a badass config, you can check out my dotfiles here.

Advanced Basics

An oxymoron as a title?? These are un-chartered waters. Hopefully by now you’ve checked out Part 1 of this series and learned the very basic Vim commands.

The reason I’m calling this post advanced basics is everything I’m going to teach you is plain-old vanilla Vim. No extensions or nothing. But these are slightly more complicated commands and use-cases, and rely on a working knowledge of the material outlined in Part 1.

So without further ado, let’s get this Vim-party started.

Pairing Commands

To be frank with you, I’m not really sure if the real Vim-term for this is called pairing. You could probably call it combining, and I wouldn’t be surprised if there was an official word for it. The bottom line is we’re putting multiple different commands together to perform some enhanced functionality.

The Whole is Greater than the Sum of its Parts
— Aristotle
—Myself (on Vim)

So, let’s say that your cursor is on the start of a word and you want to delete it, but remain in Normal Mode. Before you answer, remember the mnemonics. Speak the Vim-grammar.

d — delete we learned d will delete and remain in Normal Mode.

w — word we learned w will jump the cursor to the next word.

You guessed it…

dw — delete word it really works just that simple. Isn’t Vim freakin’ epic.

You might have noticed that dw deletes the word and the space after it. I’ve found this is usually what I want to do. However, let’s say you just want to delete until the end of the word, and keep the space. (See what I did there in italics)

de — delete to end of word We learned e jumps the cursor to the end of the current word, so de deletes to the end of the word!

What about if you’re in the middle of a line and want to delete everything backwards, to the first character in the line?

d^ — deletes everything to the first character in the line

You see how there’s real logic to everything? The mysticism of Vim is starting to unravel. And guess what? If you want to do all these these commands, but enter Insert Mode after… just substitute c for d since c — change enters into Insert Mode.

There’s endless combinations like this, and even to this day I find myself discovering new ones. Let’s see some slightly more exotic, and more magical ones.

// Example JS Code
const printHello = () => {
console.log("Hello!");
};

Let’s say you have the Javascript function above and you want to change what’s inside of the “”. The command would be:

ci" — change inside " Yup. I know.

You can perform this if your cursor is on either " or anywhere between the "" . And guess what? It doesn’t even stop at "" . Imagine you decide you want to change everything inside the function:

ci{ — change inside {
ci} — change inside }

Both will do the same thing if your cursor is on either {} or anywhere between them. This is when things start going from 0 to 100 in your coding efficiency.

The possibilities with these pairings, I presume, are nearly endless. When you’re going about your Vim-business and wonder “hmmm I wonder if I can delete everything to the s in this line?”

dts — delete to/towards s

The answer is yes, yes you can. Just keep using Vim and these will become second-nature soon enough. It always brightens my day when I discover a new pairing I never knew about.

Marks

Now that we’ve covered pairing commands, it seems like a good time to introduce marks. Remember that marks allow you to place a marker in a specific location in a file.

The example I gave in the previous post was, if you’re working in a massive file and find yourself constantly returning to the same place, you can set your mark to help you get back there easily.

You set a mark by typing m<some letter> . You can set up to 26 marks because you can use all 26 letters as a different mark. I usually find myself using mm because it’s easy to type the same letter twice, and I rarely set more than one mark per file.

But how do you get back to the mark you just set? Another pairing of course! If you set mm then from anywhere in the file you’d type:

j'm — jump to m — another perfect mnemonic command pairing!

The fun doesn’t stop here. Just like all the other pairings we saw you can use just about any command with 'm . Let’s say you have a big block of code you want to delete. You could just set you m mark at the end of the block and hit:

d'm — delete to mark m

Amazing and amazingly useful. I generally use those two examples the most, but the possibilities are endless.

Multiplying Commands

Again, there might be a more correct Vim-term for this, but you’ll get the point nonetheless.

Let’s say you want to yank (copy) the line you’re on, plus the two underneath it, then put (paste) it somewhere. Well it’s time for some Vim-math.

yy — yanks entire line that your cursor is currently on.

So really you just want to yy three times. You guessed it.

3yy — yanks entire line x 3

Then wherever you want to put the lines simply p .

This type of command multiplying is especially useful when using the hjkl navigation keys. If you want to edit a line 10 down from your current position: 10j and boom you’re ready to rock and roll.

Dot Operator

Okay you just learned a bunch of super cools commands. Let’s say you nailed an awesome command, but realize you’ll need to do it a few times.

// Example JS Class
class Example {
static printHello() {
console.log("Hello");
}
static printBye() {
console.log("Bye");
}
}

For example you have the class above and you want to delete everything inside the printHello method. You learned that you can quickly do this with di{ or di} . But let’s say you want to do the same with the printBye method too. It’s easy enough to jump down and hit di{ again, but there’s got to be a better way because this is Vim and there’s always a better way!

. — remembers you last command and repeats it

So if you hop down to printBye and hit . presto! You’ve saved two keystrokes, and trust me those keystrokes add up.

The dot operator makes mince-meat of repetitive tasks and will come to be one of your closest friends.

Visual Mode

We talked previously about Visual Mode. It’s mostly used to highlight blocks of text, and then perform a command on the entire block. For example, you highlight a block and delete the entire thing. To enter Visual Mode:

v — Visual Mode

You’ll notice after you hit v if you move your cursor with hjkl you’ll start highlighting text. You can then perform commands on this highlighted text. For example if you typed:

vjjjd — you would see that you’ve entered Visual Mode, highlighted down three lines, and deleted them. And, just like all the other commands we’ve seen you can pair v with almost anything. Example:

ve — visual mode hightlights to end of word

%

% — jump to closing pair i.e. (), {}, []

I didn’t include this command in the previous piece because I don’t think it’s necessarily a critical command, but it kind of is for development work. This command will jump from one opening bracket or parentheses , to its closing counterpart, and vice versa. I use it constantly, especially if you’re working with large JSONs, or much-too-long classes. It’s the best.

Miscellaneous Commands

Here are some miscellaneous commands you might find useful as you start getting more proficient with Vim.

ctrl + o — this is a magical commands that remembers where your cursor has been, and jumps you back to those place. Trust me this is SUPER USEFUL.

ctrl — i — this undoes ctrl+o and is also SUPER USEFUL.

ctrl + f — full — jumps your cursor down a full “page”

ctrl + b — back-up — jumps you cursor back up a full “page”

ctrl + d — down — jumps cursor down a half “page”

ctrl + u — up — jumps cursor up a half “page”

H — high— jumps cursor to highest (top) line of the “page”

M — middle — jumps cursor to middle line of the “page”

L — last— jumps cursor to last line of the “page”

zz — ??? — moves line that cursor is on to the middle of the “page”. No mnemonic, but you’ll remember it because it’s so useful.

> — indent line — will indent a line while in Visual Mode. Will work on blocks on highlighted text too.

< — dedent line — will dedent a line while in Visual Mode. Will work on blocks on highlighted text too. Will also work in Normal Mode if you press it twice.

Coming Up

Continue on your journey to Vim-stardom with the next post in this series:

--

--