DEV Community

Igor Irianto
Igor Irianto

Posted on • Edited on • Originally published at irian.to

Mastering Vim grammar

Follow @learnvim for more Vim tips and tricks!

Using Vim text editor can be overwhelming for many. If you're like me, your first exposure with Vim is when you accidentally opened it and couldn't get out. There is even a saying:

"Vim is hard to master, but harder to quit"

Well, that was 4 years ago. Vim is now my primary text editor. I would like to share some tips how to become more productive with Vim without feeling overwhelmed by its many key combinations.

To effectively use Vim, we must learn to speak its language (not to be confused with Vim Script). When we understand how to speak Vim language - I will refer it as Vimish in this article - it becomes more intuitive. Give is a try, and who knows, maybe you will end up switching over to Vim!

At the end of the day, use whatever editor that makes you most productive, because that is what matters most.

Learning Vimish

I am not a native English speaker. When I first moved to the US, it was hard for me to communicate. There were three things I did to build fluency:

  1. Increase vocabulary
  2. Learn grammar rules
  3. Practice, practice, practice.

It takes initial work, but I think anyone can do it.

To learn Vimish, we need to do the same. I will share some vocabularies and a grammar rule. However, just like any language acquisition, repeated practice is required for mastery.

Let's start with grammar:

verb + noun
Enter fullscreen mode Exit fullscreen mode

That's it. Next we will need to learn some nouns and verbs. Here I will cover:

  1. Basic nouns (motions)
  2. Verbs
  3. More nouns (text objects)
  4. Putting them together

Let's get started.

Vim nouns (motions)

Some of you might already know Vim notions because they are what you use to navigate in normal mode. If you need refresher, you can check out this article. To read more about motion, check :h motion.txt.

For now, these should be more than enough:

  • h/j/k/l - left/up/down/right
  • w,W - word/ Word
  • b,B - back a word/ Word
  • e,E - end of word, Word
  • {/} - paragraph forward/ backward
  • 0/$ - beginning/ end of line

You can add quantifier to motions. For example, j is to go down one line. Typing 10j lets us go down 10 lines instead of jjjjjjjjjj. Try to use quantifier instead of repeating same motion.

I mentioned motions here because they also work as nouns in Vimish when combined with verbs. Let's learn some verbs.

Vim verbs

Vim has many operators (16 at the time of the writing. Check :h operator for more. You can also add an additional operators using plugins). However, in my experience, learning these 3 operators is enough to do 95% of editing.

  • y - yank (copy)
  • d - delete
  • c - change

Now that we know our motions and operators, let's construct our sentence with verb + noun grammar in mind. Let's say you have the following:

const learn = "vim"; // cursor on c
Enter fullscreen mode Exit fullscreen mode
  • To delete a word, do dw. Verb (d) + noun (w).

  • To yank everything from your current location to the end of the line, do y$ (paste with p or P). Verb (y) + noun ($).

  • To change the entire paragraph ahead, do c}. Verb (c) + noun ({).

By the way, we can add quantifiers to our grammar. For example:

  • To delete the next two words, do d2w.

  • To change the next two lines, do c2j.

  • To yank the last two letters, do y2h.

A side note: although the result of 2dw is equivalent to d2w, they are technically different. 2dw means perform "deletion of a word" two times d2w means *delete the next two words one time. The difference is what Vim considers as "a change” and this matters if you are using dot command. So use your quantifiers wisely. You can read about Vim’s dot command here.

One more side note about Vim operators, is that you can type them twice to perform the operation on the entire line. dd,yy, and cc perform deletion, yank, and change on the entire line.

Once you grasp the basic verbs and nouns, it becomes intuitive. The goal is to practice enough so you won't have to think anymore. Just like learning a new language, you want to be able to speak it with no effort at all.

I think this compositional nature is where Vim shines. By learning these 3 verbs and a handful of nouns, we can create powerful instructions.

We are not quite done yet - let's continue with Vim's most powerful nouns: text objects.

Vim Nouns (Text Objects)

What if I am in the middle of a word I want to delete? Do I have to go back to the start of that word (b) and delete the word dw?

Vim allows you to delete the "group" you’re in the middle of. It works with paragraph, bracket pairs, quote pairs, etc. Anything that surrounds you. These are called text objects. To use text objects:

  • Inner: i + object
  • Outer: a + object

Suppose I have this function:


const hello = function() {
  console.log("Hello World"); // your cursor is on letter H in Hello World
  return;
}
Enter fullscreen mode Exit fullscreen mode
  • To delete the entire "Hello World", do di(. Verb (d) + noun (i()
  • To delete the entire function content, do di{. Verb (d) + noun (i{)
  • To delete the entire "Hello" string but keep "World" string, do diw. Verb (d) + noun (iw)

Here are some common text objects:

  • w - word
  • p - paragraph
  • ( { [ < - brackets
  • t - XML tags
  • " ' - quotes

Bonus: even more nouns

Vim nouns are not limited to motions and text objects. We can use search too. For example:

  • f / F- inclusive forward/backward search, same line
  • t / T - exclusive forward/backward search, same line
  • / / ? - forward/ backward search

Applying this to our grammar:

  • To delete from current location to first instance of "z", do dfz. Verb (d) + noun (fz - find z).
  • To delete everything to the third "e", do d3fe.
  • To delete everything from your current location, backwards, to first "a", do dFa.
  • To delete everything up to, excluding "x", dtx
  • To delete the line you are on to the next instance of "const hello", d/hello.

Verbs also work with marks. Example of a mark:

  • ma (mark position on register "a")

Mark can be used with any letter of the alphabets (a-z), so you can make up to 26 marks (technically 52 if you count global marks, but that's a separate discussion). Recall to jump to mark, you can either:

  • 'a (jump to the start of line marked a)
  • `a(jump to the line and column of line marked a)

To use mark as noun, you can mark a spot, go before/ after the mark, then d'a to delete everything between your current position and a mark.

Putting it all together

We just learned Vim grammar basic:

Verb + noun
Enter fullscreen mode Exit fullscreen mode

We learned some Vim verbs and nouns: operators and motions. Operators (d,y,c) make up basic verbs. Motions, text-objects, searches, and marks make up most of our nouns. There are many more verbs and nouns out there. I have only mentioned a fraction of them. Similar to language acquisition, learning Vim takes time before it comes naturally. But once we learn how to speak Vimish, we can unleash powerful commands.

You will notice that you can apply this rule with many commands. One of my biggest Vim AHA moment was when I started applying this grammar rule with new operators on my own. For example, gu and gU are operators to lowercase and uppercase, respectively. Because they are operators, they can be used with nouns. guiw lowercases a word. gU$ uppercases from current location to end of line. I just intuitively did it and it worked - because they follow the same grammar rule. I hope you will have your AHA moment soon, if not already.

Turning this knowledge learned into muscle memory will not happen over a weekend. It will take longer. I have imparted what I know and now it is up to you to apply it. Thank you for reading this far, I appreciate it.

Happy coding!

Resources

Top comments (6)

Collapse
 
ghost profile image
Ghost

Good intro, what I like about Vim is that what you learn today will be usefull in 10 years.

Who knows if VS Code, InteliJ, Atom, etc will exist in a couple of years, if they will work the same of they will be available for the same platforms, what you can be sure is that Vim will survive all of us, that will keep working the same and you'll probably find it everywhere.

Is also worthwhile to take a look at Sed and Awk, they have save me a lot of work. Old doesn't mean obsolete nor new means better ;)

Collapse
 
iggredible profile image
Igor Irianto • Edited

Agreed. One thing I found appealing to invest time learning Vim is that it is (almost) 30 years old (1991) - 1976 if we're talking Vi. If it's been around that long, there is a really good chance it will be around for another 30 years. Most modern editors are relatively new: VSCode (2015), Atom (2015), Sublime (2008 - a bit older, but nowhere near Vim age).

It'll be interesting to see the editors ecology in 10 years.

Yup, I am actually looking for sed, awk, and Ex resources. One of the principal devs at my work is super pro at awk, and I have been inspired to learn it.

Collapse
 
jvanbruegge profile image
Jan van Brügge

Vim is also my favourite text editor because in addition to the powerful movement commands, you can record any combination of them as macro. So useful for dumb repetitive tasks: dev.to/jvanbruegge/why-vim-macros-...

Collapse
 
iggredible profile image
Igor Irianto • Edited

Good article! Macros are powerful.

Collapse
 
jannikwempe profile image
Jannik Wempe

Very well explained. Exactly what I understand: structure and examples. Thanks!

I can‘t imagine to code in vim just yet but I always use it in SSH environments.

Collapse
 
waylonwalker profile image
Waylon Walker

The vim language is timeless and will continue to live in any major text editor