Vi - readline library cheat sheet Oct 22 2023

Bash Vi Editing Mode Cheat Sheet

In Bash, you can switch to Vi editing mode with the command set -o vi. Here's a collection of the most useful shortcuts in Vi mode.

Modes

In Vi mode, you start in "command mode" where you can navigate and manipulate text but cannot insert new text. To insert or append text, you'd enter "insert mode."

1
2
3
4
5
6
7
8
9
10
|---------------|-----------------------------------------|
| Command       | Description                             |
|---------------|-----------------------------------------|
| `Esc`         | Enter command mode                      |
| `i`           | Enter insert mode (before cursor)       |
| `I`           | Enter insert mode (beginning of line)   |
| `a`           | Enter insert mode (after cursor)        |
| `A`           | Enter insert mode (end of line)         |
| `R`           | Enter replace mode                      |
|---------------|-----------------------------------------|

Cursor Movement

1
2
3
4
5
6
7
8
9
10
11
12
|---------------|-----------------------------------------|
| Command       | Description                             |
|---------------|-----------------------------------------|
| `h`           | Move cursor left                        |
| `l`           | Move cursor right                       |
| `j`           | Move to previous command in history     |
| `k`           | Move to next command in history         |
| `w`           | Move forward one word                   |
| `b`           | Move backward one word                  |
| `0` or `^`    | Move to beginning of line               |
| `$`           | Move to end of line                     |
|---------------|-----------------------------------------|

Editing Text

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|---------------|-----------------------------------------|
| Command       | Description                             |
|---------------|-----------------------------------------|
| `x`           | Delete the character under the cursor   |
| `X`           | Delete the character before the cursor  |
| `dw`          | Delete word from cursor to word end     |
| `db`          | Delete word from cursor to word start   |
| `D` or `d$`   | Delete to end of line                   |
| `d^`          | Delete to beginning of line             |
| `dd`          | Delete entire line                      |
| `u`           | Undo the last change                    |
| `Ctrl-r`      | Redo the last undo                      |
| `p`           | Paste text after cursor                 |
| `P`           | Paste text before cursor                |
|---------------|-----------------------------------------|

Command History

1
2
3
4
5
6
7
|---------------|-----------------------------------------|
| Command       | Description                             |
|---------------|-----------------------------------------|
| `/`           | Search backward in command history      |
| `?`           | Search forward in command history       |
| `n`           | Repeat the last `/` or `?` command      |
|---------------|-----------------------------------------|

Miscellaneous

1
2
3
4
5
6
|---------------------------|-----------------------------------------------------------------------|
| Command                   | Description                                                           |
|---------------------------|-----------------------------------------------------------------------|
| `:` followed by a command | Execute a command (e.g., `:ls` will run the `ls` command)             |
| `v`                       | Edit the current line in an external editor (usually `vi` or `vim`)   |
|---------------------------|-----------------------------------------------------------------------|

Note: Always remember that you start in command mode in Vi mode. Use i, I, a, A, or R to enter different variations of insert or replace mode.


** If you want to check what else I'm currently doing, be sure to follow me on twitter @rderik or subscribe to the newsletter. If you want to send me a direct message, you can send it to derik@rderik.com.