Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions ch04_vim_grammar.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

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).
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 (Vim's built-in programming language, you will learn that in later chapters).

## How to Learn a Language

Expand All @@ -12,7 +12,7 @@ I am not a native English speaker. I learned English when I was 13 when I moved
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.
Likewise, to speak Vim, you need to learn the grammar rules, increase your vocabulary, and practice until you can run the commands without thinking.

## Grammar Rule

Expand Down Expand Up @@ -50,19 +50,19 @@ You will learn more about motions in the next chapter, so don't worry too much i

## 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:
According to `:h operator`, Vim has 16 operators. However, in my experience, learning these 3 operators is enough for 80% of all 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).
By the way, 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:
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";
Expand All @@ -79,15 +79,15 @@ Motions also accept count number as arguments (I will discuss this in the next c

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!
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.
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.

Expand All @@ -113,7 +113,7 @@ const hello = function() {
- 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.
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 of 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:

Expand Down Expand Up @@ -154,7 +154,7 @@ To learn more, check out `:h text-objects`.

## Composability and Grammar

Vim grammar is subset of Vim's composability feature. Let's discuss composability in Vim and why this is a great feature to have in a text editor.
Vim grammar is a subset of Vim's composability features. Let's discuss composability in Vim and why this is a great feature to have in a text editor.

Composability means having a set of general commands that can be combined (composed) to perform more complex commands. Just like in programming where you can create more complex abstractions from simpler abstractions, in Vim you can execute complex commands from simpler commands. Vim grammar is the manifestation of Vim's composable nature.

Expand All @@ -167,7 +167,7 @@ Id|Name|Cuteness
03|Bunny|Ok
```

This cannot be easily done with Vim commands, but you can get it done quickly with `column` terminal command (assuming your terminal has `column` command). With your cursor on "Id", run `!}column -t -s "|"`. Voila! Now you have this pretty tabular data with just one quick command.
This cannot be easily done with Vim commands, but you can get it done quickly with the `column` terminal command (assuming your terminal has the `column` command). With your cursor on "Id", run `!}column -t -s "|"`. Voila! Now you have this pretty tabular data with just one quick command.

```
Id Name Cuteness
Expand All @@ -193,13 +193,13 @@ Result:

Great! The external command operator can also use pipe (`|`).

This is the power of Vim's composability. The more you know your operators, motions, and terminal commands, your ability to compose complex actions is *multiplied*.
This is the power of Vim's composability. The more you know your operators, motions, and terminal commands, the more you will be able to compose complex actions.

Suppose you only know four motions, `w, $, }, G` and only one operator, `d`. You can do 8 actions: *move* 4 different ways (`w, $, }, G`) and *delete* 4 different targets (`dw, d$, d}, dG`). Then one day you learn about the uppercase (`gU`) operator. You have added not just one new ability to your Vim tool belt, but *four*: `gUw, gU$, gU}, gUG`. This makes at 12 tools in your Vim tool belt. Each new knowledge is a multiplier to your current abilities. If you know 10 motions and 5 operators, you have 60 moves (50 operations + 10 motions) in your arsenal. Vim has a line-number motion (`nG`) that gives you `n` motions, where `n` is how many lines you have in your file (to go to line 5, run `5G`). The search motion (`/`) practically gives you near unlimited number motions because you can search for anything. External command operator (`!`) gives you as many filtering tools as the number of terminal commands you know. Using a composable tool like Vim, everything you know can be linked together to do operations with increasing complexity. The more you know, the more powerful you become.
Suppose you only know four motions, `w, $, }, G` and only one operator, `d`. You can do 8 actions: *move* 4 different ways (`w, $, }, G`) and *delete* 4 different targets (`dw, d$, d}, dG`). Then one day you learn about the uppercase (`gU`) operator. You have added not just one new ability to your Vim tool belt, but *four*: `gUw, gU$, gU}, gUG`. This makes at 12 tools in your Vim tool belt. Each new motion multiplies your current capabilities. If you know 10 motions and 5 operators, you have 60 moves (50 operations + 10 motions) in your arsenal. Vim has a line-number motion (`nG`) that gives you `n` motions, where `n` is how many lines you have in your file (to go to line 5, run `5G`). The search motion (`/`) practically gives you an unlimited number of motions, because you can search for anything. External command operator (`!`) gives you as many filtering tools as the number of terminal commands you know. Using a composable tool like Vim, everything you know can be linked together to do operations with increasing complexity. The more you know, the more powerful you become.

This composable behavior echoes Unix philosophy: *do one thing well*. An operator has one job: do Y. A motion has one job: go to X. By combining an operator with a motion, you predictably get YX: do Y on X.

Motions and operators are extendable. You can create custom motions and operators to add to your Vim toolbelt. The [`vim-textobj-user`](https://github.com/kana/vim-textobj-user) plugin allows you to create your own text objects. It also contains a [list](https://github.com/kana/vim-textobj-user/wiki) of user-made custom text objects.
Motions and operators are extensible. You can create custom motions and operators to add to your Vim toolbelt. The [`vim-textobj-user`](https://github.com/kana/vim-textobj-user) plugin allows you to create your own text objects. It also contains a [list](https://github.com/kana/vim-textobj-user/wiki) of user-made custom text objects.

## Learn Vim Grammar the Smart Way

Expand Down