šŸ§  Second Brain


Search IconIcon to open search

The Vim Language (and Motions)

Last updated Feb 9, 2024

Why are there many users (and many new ones) still using Vim, you might ask?

It’s mainly for vim language/motions. Many believe, me included, that you need to know 1000 shortcuts. But when I realized it’s vim has a language and things are more like a grammar. I noticed that I only have to remember a couple of modes (Copying (or yanking) in Vim with y, deleting with d, pasting with p, changing with c) and shortcuts, which I can then combine. This made me want to try it, and I didn’t regret it.

It is surprising to me; I use it even more for writing text than coding, which I think I will use it for. However, the efficiency of editing text with the precision of a surgeon is even more helpful when I edit and rearrange my blogs or books.

# Vim Language

Vim has a terrific language or grammar behind its shortcuts. Instead, remember a thousand shortcuts; you can learn a couple and combine them. These are sometimes called the Vim language or Vim motions for moving around. This has nothing to do with the editor yet; they are universal and available in other editors as well.

For example, VSVim for VSCode, IdeaVim for the JetBrains products, Vintage Mode for Sublime and so on. But there are also Browser extensions like Vimium or Firenvim, even Gmail adapted some of its shortcuts for navigation (j, k for moving, g for jumping).

Everyone who types on a computer eight hours a day should learn the Vim language. Yes, it’s hard in the beginning, but that’s everything new and different, but getting better every day and having more fun coding or writing should motivate enough. The excuse of being too busy to learn while doing what you always did does not count.

Are you too busy to improve | Image from steenschledermann

# The Grammar

As grammar has verbs, subjects, and objects, so does the Vim language. The grammar has different verbs to begin with. Copying (or yanking) in Vim with y, deleting with d, pasting with p, changing with c.

For example, the easiest shortcut is copying a line with yy. In this case, yank is the verb and the second y is a synonym for y_. TheĀ yĀ is doubled up to make it easier to type since a joint operation.

Next, we can add movements. Each verb takes a subject to their movements. There are lots of movements (more in the next chapter); the easiest is with numbers. E.g. to copy three lines, you add three in front, such as 3yy. You can do that with all verbs, e.g., deleting three lines 3dd. Another would be {Ā andĀ }Ā to move to the beginning or end of the paragraph, respectively.

In addition to verbs and subjects, the Vim languageĀ also has objects. E.g., we can save text into different clipboards (called a register in Vim) with "ay. Here, we copy it into register a, which would be the object. We can paste it again by doing the same but using the verb paste instead of yank "ap.

If you will, there are even adjectives and adverbs with prefixes. Usually, you use a verb and an object. But instead of going down three lines with 3J, which joins the following three lines. You could add d5}, which means “delete from the current line through the end of the fifth paragraph down from here.”,

For me, the most considerable magic and myth about Vim. All of the vim language is how you navigate end edit text, and it still has nothing to do with the editor. Sure Vim was the one that introduced and perfectionized them, but as listed above, you can get them anywhere else. This goes deep into the Vim language, yet we still need to touch the editor. This is important to know.

I hope you start seeing the power of such a pattern. With a couple of verbs and objects, you can already know hundreds of combinations without memorizing them. You can see a video on Mastering the Vim Language on the Vim language or read a full plea for the Vim language on a terrific StackOverflow comment.

# Vim Motions

As touched in the Vim language. Vim motions are how you navigate, whether you navigate to the end of the word or back to the start of the document; these are all motions.

These are the first thing you start learning (and hating) when you start learning. It’s extra hard to know initially, but something you want everywhere when you get used to it. Instead of using arrow keys, Vim uses jk to move down and up and hl to move left and right. The main idea is to use the keys that your right hand naturally rests on. You do not need to move any of your hands or even fingers for navigation. Again, this seems like a small thing, but once learned, you know why everyone is telling you that.

Some common ones are:

h,j,k,l - left, down, up, right
w,W - to start of next word or WORD
b,B - to start of previous word or WORD
e,E - to end of word or WORD
$   - to end of line
^   - to start of line

You find the most important ones to start in this cheatsheet:

Vim Command Cheat Sheet from Cloud Guru

My own Cheatsheet
My own Vim Cheatsheet

# Modes (normal, insert, visual, command)

Modes are what get you confused at the beginning. When you launch Vim, you are not typing what you click on your keyboard as you are not in the “insert” mode that you know. Instead, the normal mode you are in lets you do the commands explained in the above Vim language and motions.

Vim is the only editor that optimizes editing text instead of writing from a blank page.

Three modes illustrated (escape mode being the command mode) | Image from Geekforgeeks

That’s another reason Vim makes you so efficient: you have different modes for each phase of your taskā€”normal mode for reading code and navigating quickly. Insert mode when you want to add some code or text. Visual mode is unique, the same as highlighting text with the mouse, but with the above Vim motions. The command mode is the powerhouse, where you can type Linux commands such as formatting a JSON file with :%!jq (whereas jq is a command line tool installed on your machine) and execute them within Vim. As well as Vim commands such as :sort for sorting your file.

I could go on here, but I want to dive into the editor itself now and explore why I learned it initially and how to get started if you’re going to.

Origin: Why I’m using (Neo)vim as a Data Engineer and Writer in 2023 |
Created 2023-01-16