Thinking in Vim...notes from our dev learning lunch

We’re into our second week of lunch-n-learning about Vim, a text editor that you can use on pretty much any server, laptop or desktop to modify files using the command line. This week we learned how to delete text in files, and took a deep dive into how to ‘think in Vim’, by exploring common patterns and approaches that are used to build up commands.

Apr 30, 2020

Read in 3 minutes

Thinking in Vim...notes from our dev learning lunch

Deleting single characters

After learning how to navigate through files and how to insert text (the stuff of our first learning lunch), the next most common type of thing you’d want to do using Vim is to delete text. The essentials for this are pretty easy to remember:

Lower case x will delete the character at current position

Capital X (i.e. SHIFT+X) will delete the character at the previous position

Avoiding shortcuts

So far, so good. But it turns out that using single characters (like lower and uppercase x above) are actually just shortcuts that make it faster to use Vim. All well and good, but to embed our understanding it was time to learn how to properly think ‘in Vim’ by exploring a few common patterns.

operation{motion}

The first pattern we explored was operation{motion}. This is where you combine two Vim commands (one for the ‘operation’ and one to describe the ‘motion’).

For example dw is short for ‘[delete] [word]’. The operation here is obvious, but the key to understand this pattern is how that ‘delete’ operation is used.

From our previous learning lunch, we knew that w moves you forward to the next word, so here this pattern deletes the next word after the one in the current position. The way to think of it is ‘I’m going to delete where this motion would take me’.

Other examples of this pattern are…

d0 deletes from the current position all the way back to the beginning of the line

d$ deletes from the current position to the end of line

dd deletes the entire line regardless of where the cursor is based

See, we’re getting the hang of this whole operation thing. Sade would be proud.


[count]operation{motion}

Knowing this pattern, we can build on it by adding a ‘count’ to specify how many times to repeat the action of an operation. For instance, 3dd will delete the entire next 3 lines.

[count]operation{motion}[count]

The final pattern we explored took this same principle of adding a ‘count’, but this time at the end of the statement. This count specifies how many times to repeat a motion. For example 2dw3 will delete the next 3 words, twice (i.e. it will delete the next 6 words)

Of course, this could be achieved using 6dw (from the above pattern), but there may be times when you want to be more specific (and besides, everyone will have their own personal preference).


Getting to grips with the count operation motion commands on Vim

Dot command (.)

A final handy Vim command to be aware of is the full stop . command, which repeats the last command you entered into the terminal. A really useful one to remember!

Speed running

We ended the session with another speed run through the first level of the Vim adventures game, to reinforce what we’ve learned about Vim so far. We all managed to get the treasure, but our server-side lead Rhymes got there first. Well done Rhymes!

Putting it all together

So, in this week’s learning lunch, we learned how to delete text inside files using Vim, as well as some fundamental patterns involving ‘count’, ‘operation’ and ‘motion’, and how the vim syntax for these works. Using these strong foundations we’ll be able to combine and compose more sophisticated commands in the future.

It was great fun learning as a group and next week we will be doing the same again (or just . if you speak Vim!)

Sign up to our newsletter

Stay in the loop with latest releases, articles and updates