VIM cheat sheet for common actions
Tags: vim
Published: 2020-08-09 06:17:37

---
title: VIM cheat sheet for common actions
published: true
date: 2020-08-08 20:48:19 UTC
tags: vim
canonical_url: https://dico.duba.dev/vim-cheat-sheet-for-common-actions/
license: cc-by-4.0
---

When editing files over ssh, I prefer to use VIM over editors like nano. While it is certainly easier to find the save button in nano, most other actions in nano are just clunky. And while I never really understood the obsession with home row navigation, since my brain is usually the limiting factor rather than how fast I can type or navigate through an editor, I do prefer how VIM just gets the job done.

One thing I would have benefited from when learning to use this editor is just a cheat sheet with the most common actions you would want to do in an editor like VIM. So I made one.

## Understanding VIM

![](https://dico.duba.dev/wp-content/uploads/2020/08/image-1.png)<figcaption>VIM over SSH. Annotated: (1) Cursor location, (2) Highlighted bracket matching bracket under cursor, (3) Filename, 18 lines, 288 characters, (4) line number 1, column 1</figcaption>

When opening a file with VIM, you start in a sort-of command mode. You can navigate the file, but you can not edit. To enter edit-mode, press <kbd>i</kbd> or <kbd>insert</kbd>. If you press <kbd>insert</kbd> more than once, you toggle between insert-mode (add text under the cursor) and replace-mode (replace text under the cursor). To get back into command-mode, press <kbd>Esc</kbd>.

If you are stuck, you can usually get out of whatever you just did by mashing <kbd>Esc</kbd> and sometimes typing <kbd>:</kbd> <kbd>q</kbd> <kbd>Enter</kbd>. When you are back in command mode, and there is no weird prompt anymore in the bottom you can return to edit mode via <kbd>i</kbd> or <kbd>insert</kbd>.

When in command mode, pressing keys like <kbd>:</kbd>, <kbd>/</kbd> and <kbd>?</kbd> will move the cursor to the bottom. These are the beginning of commands. If you don’t want to do a command, just mash <kbd>Esc</kbd> to get back to what you were doing.

![](https://dico.duba.dev/wp-content/uploads/2020/08/image-2.png)<figcaption>VIM over ssh with an arrow pointing at the bottom command line</figcaption>

## Cheat sheet

All of these commands happen in command mode (see above). Get in the habit of pressing <kbd>Esc</kbd> before doing any of these.

### Critical commands

- <kbd>Esc</kbd> <kbd>Esc</kbd> <kbd>Esc</kbd>: Get back into command mode
- <kbd>i</kbd> or <kbd>insert</kbd>: Enter edit mode
- <kbd>:</kbd> <kbd>w</kbd> <kbd>enter</kbd>: Save the current file
- `:w new_file_name.ext` <kbd>enter</kbd>: Save to a (new) file new\_file\_name.ext
- <kbd>:</kbd> <kbd>q</kbd> <kbd>enter</kbd>: Quit VIM
- <kbd>:</kbd> <kbd>q</kbd> <kbd>!</kbd> <kbd>enter</kbd>: Force quit VIM (discarding changes)
- <kbd>:</kbd> <kbd>w</kbd> <kbd>q</kbd> <kbd>enter</kbd>: Save changes and quit VIM

That’s all you really need to get going. You can just navigate using the arrow keys and keys like home, end, page up and page down do exactly what you expect them to do.

## Cut / Copy / Paste

VIM has it’s own “clipboard”. If you paste something that is **not** copied through one of VIM’s commands, just enter insert mode and (if you are using ssh for example) press the right mouse button to paste your own clipboard.

- <kbd>insert</kbd> <kbd>right mouse button</kbd>: Paste from own clipboard (in ssh)
- <kbd>y</kbd> <kbd>y</kbd>: Copy the current line
- <kbd>d</kbd> <kbd>d</kbd>: Delete the current line (works like the cut action, as you can paste this line elsewhere)
- <kbd>p</kbd>: Paste what you have copied or cut
- <kbd>v</kbd>: Enter visual mode. In visual mode you select text between the character where you entered visual mode and where your cursor is now. If you press <kbd>y</kbd> you copy that entire area and if you press <kbd>d</kbd> you delete/cut that entire area. To exit out without doing anything, use <kbd>Esc</kbd>.

![](https://dico.duba.dev/wp-content/uploads/2020/08/image-3.png)<figcaption>VIM over ssh. Annotated: (1) Position where visual mode was activated with “v”, (2) current position of the cursor (e.g. reached by “down”, “down”, “end”), (3) the bottom bar shows that visual mode has been activated.</figcaption>

When combined, you can do many things. For example

- <kbd>y</kbd> <kbd>y</kbd> <kbd>p</kbd>: Duplicates the current line by copying the current line and pasting it immediately
- <kbd>d</kbd> <kbd>d</kbd> <kbd>p</kbd>: Move the current line below the following line
- <kbd>v</kbd>, some navigation, <kbd>d</kbd> to cut a section, then navigate to the new position and <kbd>p</kbd> to put it back in that position.

## Undo / redo

- <kbd>u</kbd>: Undo the last action (and you can keep pressing it to undo more)
- <kbd>ctrl</kbd> + <kbd>r</kbd>: Redo whatever you did just undo

## Navigation

In VIM you can search from the beginning of the file to the end of the file with <kbd>/</kbd> and from the end of the file to the beginning of the file with <kbd>?</kbd>. This only affects the order of the search results you get.

- <kbd>/</kbd> `your search term` <kbd>enter</kbd>: Finds the first occurence of `your search term` in the file
- <kbd>?</kbd> `your search term` <kbd>enter</kbd>: Finds the last occurence of `your search term` in the file
- <kbd>n</kbd>: When searching, finds the next result. For `/` this is later in the file, while for `?` this is earlier in the file. If no more results can be found, it will go back to the first result.
- <kbd>shift</kbd> + <kbd>n</kbd>: When searching, goes to the previous result.
- <kbd>:</kbd> `some line number` <kbd>enter</kbd>: Goes to the specified line number. (e.g. `:18` goes to line 18)

![](https://dico.duba.dev/wp-content/uploads/2020/08/image-4.png)<figcaption>When pressing “n”, if no more search results can be found, the bottom row will tell you that it continued back at the edge of the file you started searching at.</figcaption>

* * *

The post appeared first at [VIM cheat sheet for common actions](https://dico.duba.dev/vim-cheat-sheet-for-common-actions/)