DEV Community

Dico
Dico

Posted on • Originally published at dico.duba.dev on

VIM cheat sheet for common actions

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

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

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 i or insert. If you press insert 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 Esc.

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

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

VIM over ssh with an arrow pointing at the bottom command line

Cheat sheet

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

Critical commands

  • Esc Esc Esc: Get back into command mode
  • i or insert: Enter edit mode
  • : w enter: Save the current file
  • :w new_file_name.ext enter: Save to a (new) file new_file_name.ext
  • : q enter: Quit VIM
  • : q ! enter: Force quit VIM (discarding changes)
  • : w q enter: 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.

  • insert right mouse button: Paste from own clipboard (in ssh)
  • y y: Copy the current line
  • d d: Delete the current line (works like the cut action, as you can paste this line elsewhere)
  • p: Paste what you have copied or cut
  • v: 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 y you copy that entire area and if you press d you delete/cut that entire area. To exit out without doing anything, use Esc.

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.

When combined, you can do many things. For example

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

Undo / redo

  • u: Undo the last action (and you can keep pressing it to undo more)
  • ctrl + r: 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 / and from the end of the file to the beginning of the file with ?. This only affects the order of the search results you get.

  • / your search term enter: Finds the first occurence of your search term in the file
  • ? your search term enter: Finds the last occurence of your search term in the file
  • n: 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.
  • shift + n: When searching, goes to the previous result.
  • : some line number enter: Goes to the specified line number. (e.g. :18 goes to line 18)

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.


The post appeared first at VIM cheat sheet for common actions

Top comments (0)