Vim keybindings for efficient text editing
Tags: productivity, programming, neovim, vim
Published: 2022-07-17 04:30:54

Hello, I am Taras. I am working as a backend developer. 

If you’re a software engineer, you probably heard about Vim, a free, open-source, screen-based text editor program.

In this article, I’ll show you how you can radically speed up your development workflow by leveraging one of the most potent things about Vim — its keybindings — without even having to use Vim.

You might be thinking, “ugh, keybindings, too nerdy and difficult, I don’t need that”… Hold on! It’s not as difficult as you might think, and you don’t need to memorize all of them. Once you understand the core logic behind keybindings, the following experience should come naturally. Let me show you!

{% embed https://youtu.be/uGkYDRbhHo0 %}

## What is so special in Vim keybinding?

Vim has different editing modes and it lets you use easy-to-remember keybinding without needing to add a couple of key modifications (ctrl+shift+…, alt+shift+…). It has it's language for editing text. And once you get the basics you can combine them to do different actions. Everything you can think about editing text there is probably a keybinding for it, if not you can easily create it. Let's start with Vim modes.

### Normal Mode

You can’t write text in this mode. By default, Vim starts in this mode. In Normal mode, key presses don’t work as one would expect. This mode is responsible for editing and moving around. Remember, this is what makes Vim different.

### Basic movements that I use on a daily basis

Cursor movement:

-   **`h`** move one character left
-   **`j`** move one row down
-   **`k`** move one row up
-   **`l`** move one character right

If you are a touch typist like me you will like these movements because you don’t have to leave a ‘home row’. Keys ‘h’ and ‘l’ were easy to remember for me but ‘jk’ took me some time to used to.

Word movements:

-   **`w`** move to the beginning of the next word
-   **`b`** move to the previous beginning of the word
-   **`e`** move to the end of the word
-   **`ge`** move to the end of the previous word
-   **`W`** move to the beginning of the next word after a whitespace
-   **`B`** move to the beginning of the previous word before a whitespace
-   **`E`** move to the end of a word before a whitespace

Beginning/End of line movement:

-   **`0`** move to the beginning of the line
-   **`$`** move to the end of the line

### targeted movements:

-   move to the next occurrence of the current word under cursor: **``**
-   move to the previous occurrence of the current word: **`#`**
-   move (un)til character: **`t<char>`**
-   move back (un)til character: **`T<char>`**
-   move to found character: **`f<char>`**
-   move back to found character: **`F<char>`**
-   move to match: **`/<regex>`**
-   move back to match: **`?<regex>`**
-   move to the next occurrence of a match: **`n`**
-   move to the previous occurrence of a match: **`N`**

Most keybindings are mnemonic (w - word, e - end) and some of them have regex expression philosophy beyond.

### Insert Mode

This mode works the way text editing works throughout the OS. Same shortcuts (`cmd+c`, `cmd+p`, etc), same movements (arrow keys, page up/down, `ctrl+a`, `ctrl+e`, etc). To exit this mode you need to press escape

There are many movement commands that can result in switching to Insert Mode:

-   insert before letter under cursor: **`i`**
-   insert after letter under cursor: **`a`**
-   insert at the start of the line: **`I`**
-   insert at the end of the line: **`A`**
-   insert on a new line: **`o`**
-   insert on a new line above: **`O`**
-   substitute: **`s`**
-   change: **`c`**

### Visual Mode

A mode for creating selections.  This mode can be extremely useful for identifying chunks of text to be manipulated (e.g. apply reg expression for selected text).

To enter Visual Mode (starting from Normal Mode):

-   `v` to select by character
-   `V` to select by line

## A Language of Text Editing

I really enjoy mnemonic keybindings, because they are easy to remember. Vim has them! Many keybindings can be conveniently thought of as the "verbs", "nouns" and "prepositions" that make up the "language".

### Examples of "verbs":

-   change (**`c`**)
-   replace (**`r`**)
-   substitute (**`s`**)
-   delete (**`d`**)
-   yank (**`y`**)
-   put (**`p`**)

NOTE: 'change', 'replace', AND 'substitute' aren't the same thing!

-   Change deletes everything in the next movement and puts you in Insert Mode (though with a selection, it behaves like a substitute).
-   Substitute immediately deletes the current character (or selection) and puts you in Insert Mode.
-   Replace never enters Insert Mode at all, but just replaces the current character (or every character in a selection) with the provided character.

### Examples of "nouns":

-   word (**`w`**)
-   end (**`e`**)
-   beginning (**`b`**)

### Examples of "prepositions":

-   inside (**`i`**)
-   around (**`a`**)
-   (un)til (**`t`**) You can combine these parts of the "language" to perform editing tasks:
-   delete to the end of a word: **`dw`** (or **`de`**)
-   delete the current word: **`diw`**
-   yank to the end of word: **`ye`**
-   change inside parentheses: **`ci)`** (or **`ci(`**)
-   delete around brackets: **`da]`** (or **`da[`**)

Of course, Vim has more editing and movement commands and I want you to give it a try. I am sure that it can be a life-changer for your workflow!

Afterword

### Awesome personal remaps that I think are really helpful

1.  Remap **`CAPSLOCK`** to **`ESC`** if pressed alone, and to **`CTRL/CMD`** if pressed with the key. For me personally, it's easier to reach out.
2.  Remap **`WIN/OPTION`** + **`hjkl`** to arrow keys
3.  Remap **`WIN/OPTION + N`** go to the next word and **`WIN/OPTION + P`** go to the previous word

### Where I use vim keybindings

-   **Neovim** - my main text editor.
-   **Terminal** I am using zsh-vi-mode plugin for zsh.
-   **Obsidian** for writing notes
-   **Google Chrome** I am using Surfingkeys plugin for browsing