Chapter 4.Table of Contents

A Taste of Vim

A crow familiar getting a taste for their master's cake

// something something
  - Someone, 
    Some cool sounding title,
    Some even more cool sounding book or treatise

The best way to get a feel for Vim and its wondrous capabilities for text incantation is to jump straight into Vim. This chapter will give you a small taste of what it feels like to be a seasoned Vim practitioner. A creature of heightened senses and sharpened abilities, ready to spring into action in a heartbeat and ambush the word and its evil siblings, the sentence and the paragraph.

You don’t need to fire up your computer yet (although I won’t stop you), you are welcome to follow along reading. Don’t worry if you don’t understand everything right away or if some things seem weird. In fact, you can expect that things will get weird. But don’t fret! The remainder of the book will focus on explaining everything in detail and practicing over and over, so that by the end of it, you’ll be a confident Vim user able to do all this and much more.

Excellent! Through the use of dark eldritch powers this chapter will allow you to experience what it feels like to be a trained Vim alchemist. Close your eyes and imagine… (reality blurries and fades to a dreamlike alternate reality)

A portal to an alternative reality and you walking through it

You open Vim and you’re embraced by Vim’s core mode: normal mode. “Hello, old friend”, you think and smile. Inside this mode you don’t actually write code. Remarkable and strange, isn’t it? Instead, normal mode is designed for navigation and precise text manipulation and makes you an all powerful text editing powerhouse. Unlike other traditional editors, when using Vim you’ll spend most of your time in normal mode.

Your right hand rests firmly on the core motion keys hjkl (motion as in movement). These keys move the cursor around to left, down, up, and right respectively. You can also move left to right word by word using w (go to the beginning of next word) or e (go to the end of the next word), or use b/ge to do the same but from right to left.

This can feel intimidating for an initiate, so let’s open a scroll with some fancy diagrams:

The hjkl motions are one of the most foundational motions in Vim.

The word motions let you move quickly between words

Taking advantage of these keys you can navigate a file with fine granularity and strike with great vengeance and spite: Type daw and bang! You delete a word! das and you remove a sentence! dap and you obliterate a paragraph!

Or you can be more nurturing and fix stuff instead. Using caw, cas, cap to change a word, a sentence or a paragraph.

But it doesn’t end there. You can ctx to change until the first x in the current line, or c$ change everything until the end of the line, or event better ci( to change the content inside parentheses or ci" for content inside quotes.

On Notes, Melodies And Chords

Vim is quite special. If you’ve used other editors, you’re likely familiar with the habit of typing chords of keys. That is, typing a combination of keys at the same time. You may, for example, type CTRL-C to copy some text and CTRL-V to paste it. Vim can use chords as well but relies primarily on melodies of keys.

If you think of keys as musical notes, a melody is a series of notes one after the other. That is is how you normally interact with Vim. So, when you read that you need to type f{char} to find a character in a line, it means that first you type f and then you type the character {char} in rapid succession (f.i. fa to find the first a).

Using melodies of keys, although unfamiliar and kind of strange at first, is very convenient. Some would even call it ingenious. Controlling the editor will suddenly feel like you’re just typing text. I’m sure you are very accustomed to typing text. And as a bonus, your wrists and fingers will thank you. You’ll no longer need to contort your hands in wild and unnatural joogha positions.

Imagine a simple string:

const msg = 'Hither came Conan, the Cimmerian, black-haired, sullen-eyed, sword in hand, a thief, a reaver, a slayer, with gigantic melancholies and gigantic mirth, to tread the jeweled thrones of the Earth under his sandaled feet.'

You can change the string by typing: f'ci'WAT<ESC>

const msg = 'WAT'

Which means:

  1. Find the next single quote ' (f')
  2. Change everything inside ' for WAT (ci' removes everything inside ' and drops you into insert mode where you can type WAT)
  3. Then <ESC> to leave insert mode back to the cozy normal mode

More Productivity Plz!

Did you know you can achieve the same results in the previous example by just typing ci'WAT<ESC>. There is no need for f' because the ci' command seeks forward for a pair of single quoted text within a line.

As you become a more proficient Vim wizard, you’ll find that there are many ways to edit text in Vim. Oftentimes your goal will be to learn how to achieve a task with the least number of keystrokes and burn that into your muscle memory. But this is something that you’ll learn gradually so don’t worry about it for now.

If you have an obsessive character and feel like you must optimize everything, then follow the path of the vimgolf sorcerers.

Ok, so we now have this string:

const msg = 'WAT'

You can yank this line with yy (which is Vim’s extremely evocative jargon for copy) and put it below with p (again Vim jargon for paste):

const msg = 'WAT'
const msg = 'WAT'

Yes, you got it! yank is another operator like delete and change. You can use y just like d or c to yank a word yaw or yas yank a sentence.

Moreover doubling a command like so yy makes the command operate on the entire line. Nifty! It follows that cc changes a line and dd deletes one. Gosh, look at how much you’re learning!

Now try ci'MAN<ESC> which results in:

const msg = 'WAT'
const msg = 'MAN'

Then go crazy and join the lines with kJ (k to go up and J to join lines):

const msg = 'WAT' const msg = 'MAN'

Rinse with c3w+<ESC> (as in change the next 3 words for a +) and we’ve got ourselves:

const msg = 'WAT' + 'MAN'

Which is a completely ridiculous exercise of Vim but which stills manages to show you part of the magic of Vim.

For longer motions you can use counts in combination with motions (I actually sneaked on of those earlier). For instance, you can go down 5 lines by typing 5j (as in {count}{motion}). Likewise you can use these together with the operators you saw above (d, c, etc) and d2w detele two words, or c2s change 2 sentences. There’s longer motions too, you can H to move to the top of the visible area in the editor, or gg to go to the top of the file, L and G to achieve the same but downwards. Use { to move up a whole paragraph or } to do the same but down. While % helps you find matching parentheses.

You can start a new line below with o (drops into insert mode so you can start typing) or above with O. You can find patterns (of text) forward within a file using /{pattern} and navigate between patterns using n (next) and N (previous). You can do the same thing backwards using ?{pattern}.

You can repeat previous changes using the . command. Just type . and Vim will repeat your last change. Likewise you can repeat motions. Type ; and you’ll repeat a motion started with t,f,T,F or type n to repeat a search. You can record a collection of commands using macros and replay them over and over at your will. And there’s so. Much. More…

So much power at your fingertips and we have barely left normal mode or the confines of a single file. There’s splits, there’s tabs, there’s regex, there’s a seamless symbiosis with external tools, there’s spell checking, word count, there’s 6 basic modes more with 6 additional variant modes and infinite extensibility and customization possibilities!!!

Who’s excited!?

Then proceed apprentice.

Jaime González García

Written by Jaime González García , dad, husband, software engineer, ux designer, amateur pixel artist, tinkerer and master of the arcane arts. You can also find him on Twitter jabbering about random stuff.Jaime González García