# Ch04. Vim Grammar It is easy to get intimidated by the complexity of Vim commands. If you see a Vim user doing `gUfV` or `1GdG`, you may not immediately know what these commands do. In this chapter, I will break down the general structure of Vim commands into a simple grammar rule. This is the most important chapter in the entire guide. Once you understand the underlying grammatical structure, you will be able to "speak" to Vim. By the way, when I say *Vim language* in this chapter, I am not talking about Vimscript language (Vim's built-in programming language, you will learn that in later chapters). ## How to Learn a Language I am not a native English speaker. I learned English when I was 13 when I moved to the US. There are three things you need to do to learn to speak a new language: 1. Learn grammar rules. 2. Increase vocabulary. 3. Practice, practice, practice. Likewise, to speak Vim language, you need to learn the grammar rules, increase vocabulary, and practice until you can run the commands without thinking. ## Grammar Rule There is only one grammar rule in Vim language: ``` verb + noun ``` That's it! This is like saying these English phrases: - *"Eat (verb) a donut (noun)"* - *"Kick (verb) a ball (noun)"* - *"Learn (verb) the Vim editor (noun)"* Now you need to build up your vocabulary with basic Vim verbs and nouns. ## Nouns (Motions) Nouns are Vim motions. Motions are used to move around in Vim. Below is a list of some of Vim motions: ``` h Left j Down k Up l Right w Move forward to the beginning of the next word } Jump to the next paragraph $ Go to the end of the line ``` You will learn more about motions in the next chapter, so don't worry too much if you don't understand some of them. ## Verbs (Operators) According to `:h operator`, Vim has 16 operators. However, in my experience, learning these 3 operators is enough for 80% of my editing needs: ``` y Yank text (copy) d Delete text and save to register c Delete text, save to register, and start insert mode ``` Btw, after you yank a text, you can paste it with `p` (after the cursor) or `P` (before the cursor). ## Verb and Noun Now that you know basic nouns and verbs, let's apply the grammar rule, verb + noun! Suppose you have this expression: ```javascript const learn = "vim"; ``` - To yank everything from your current location to the end of the line: `y$`. - To delete from your current location to the beginning of the next word: `dw`. - To change from your current location to the end of the current paragraph, say `c}`. Motions also accept count number as arguments (I will discuss this in the next chapter). If you need to go up 3 lines, instead of pressing `k` 3 times, you can do `3k`. Count works with Vim grammar. - To yank two characters to the left: `y2h`. - To delete the next two words: `d2w`. - To change the next two lines: `c2j`. Right now, you may have to think long and hard to execute even a simple command. You're not alone. When I first started, I had similar struggles but I got faster in time. So will you. Repetition, repetition, repetition. As a side note, linewise operations (operations affecting the entire line) are common operations in text editing. In general, by typing an operator command twice, Vim performs a linewise operation for that action. For example, `dd`, `yy`, and `cc` perform **deletion**, **yank**, and **change** on the entire line. Try this with other operators! This is really cool. I am seeing a pattern here. But I am not quite done yet. Vim has one more type of noun: text objects. ## More Nouns (Text Objects) Imagine you are somewhere inside a pair of parentheses like `(hello Vim)` and you need to delete the entire phrase inside the parentheses. How can you quickly do it? Is there a way to delete the "group" you are inside of? The answer is yes. Texts often come structured. They often contain parentheses, quotes, brackets, braces, and more. Vim has a way to capture this structure with text objects. Text objects are used with operators. There are two types of text objects: inner and outer text objects. ``` i + object Inner text object a + object Outer text object ``` Inner text object selects the object inside *without* the white space or the surrounding objects. Outer text object selects the object inside *including* the white space or the surrounding objects. Generally, an outer text object always selects more text than an inner text object. If your cursor is somewhere inside the parentheses in the expression `(hello Vim)`: - To delete the text inside the parentheses without deleting the parentheses: `di(`. - To delete the parentheses and the text inside: `da(`. Let's look at a different example. Suppose you have this Javascript function and your cursor is on the "H" in "Hello": ```javascript const hello = function() { console.log("Hello Vim"); return true; } ``` - To delete the entire "Hello Vim": `di(`. - To delete the content of function (surrounded by `{}`): `di{`. - To delete the "Hello" string: `diw`. Text objects are powerful because you can target different objects from one location. You can delete the objects inside the parentheses, the function block, or the current word. Mnemonically, when you see `di(`, `di{`, and `diw`, you get a pretty good idea which text objects they represent: a pair of parentheses, a pair of braces, and a word. Let's look at one last example. Suppose you have these HTML tags: ```html
Paragraph1
Paragraph2