Vim Commands Cheat Sheet

Photo of author

By admin

A Linux system user typically uses a text editor to program and script, edit configuration files and perform other tasks. A large number of Linux users use Vim, an open-source Linux text editor.

It takes time and practice to learn how to operate Vim commands. That is why having a handy reference document or command cheat sheet ready while mastering them is extremely beneficial. In this blog, you will find several commonly used Vim commands that every individual should be aware of.

What is Vim?

Vim is a Linux text editor that comes preinstalled on Linux, BSD, and macOS. It is popular for being quick and powerful, thanks to the fact that it’s a simple application that can run in a terminal. It’s widely used because it can be operated totally without the use of buttons or a mouse.

Vim is a derivative of the Vi editor. It’s a controlled, centered editor, just like Vi. Vim is available and used worldwide, which is one of the benefits of learning it. Vim is installed by default on all UNIX variants, including Linux, Mac, HP-UX, AIX, and many others. There was no graphical user interface with Vim, but it is now provided as part of a separate installer called gVim.

  • It has a very small memory footprint
  • It is a command-driven system. With just a few commands, you can complete difficult text-related activities.
  • It is very configurable and stores its settings in a simple text file.
  • There are numerous Vim plug-ins available. Its usefulness can be greatly enhanced by using such plugins.
  • Multiple windows are supported. This function allows you to split your screen into numerous windows for working parallelly without issues.
  • Multiple buffers are supported in the same way as multiple windows are.
  • It has many tabs that allow you to work on multiple files at the same time.
  • It has recording options that enable you to repeatedly record and play Vim commands.

Vim Commands Cheat Sheet

1. Editing Text

  • r – replacing a single character with another character (and returning to the command mode)
  • cc – replaces a line in its whole (deletes the line and moves into insert mode)
  • C / c$ – replace texts from the cursor and places it to the end of the line
  • cw – replaces a word from the cursor to the end
  • s – remove a character from the list (and shifts it into the insert mode)
  • J – By adding a space between them, merge the line below with the selected one.
  • gJ – combine the two lines with leaving no space between them
  • u – undo
  • Ctrl + r – redo
Cutting, Copying, And Pasting
  • yy – copy complete line
  • #yy – copy the desired number of lines
  • dd – remove all the lines
  • #dd – remove a selected number of lines only
  • p – Pasting the text after the cursor
  • P – pasting the text before the cursor

2. Moving Inside a File

Inside a file, users could move the cursor by single letters, words, tokens, or lines. A word can be any combination of letters, digits, and underscores. A token, on the other side, is something that is separated by whitespace, including punctuation. You can also use screen view to go to different areas of text.

Moving by Characters, Words, and Tokens

The following are the basic commands for moving the pointer by a single character:

  • h – drag the pointer to the left.
  • j – moving the cursor towards left
  • k – moving it towards up
  • l – moving it towards the right

These letters can also be prefixed with a number to move in a specific direction several times. When you type 5j, for example, the pointer moves left by 5 lines.

  • b – go to the beginning of a word
  • B – go to the beginning of a token
  • w – going to the beginning of the next word
  • W – go to the beginning of the following token
  • e – moving to the end of a word
  • E – moving towards the end of a token

For instance, you have the noun phrase “step-by-step” as part of a text and the cursor is placed at the end of it. The first time you press b, the cursor moves back to “step-by-step”. However, if you use B, the cursor moves all the way back to “step-by-step” since there is no whitespace between these characters.

Moving by Lines
  • 0 (zero) – jumping directly to the line’s start
  • $ – jumping directly to the line’s end
  • ^ – jumping directly to the line’s first (non-blank) character
  • #G / #gg / :# – moving towards a particular line number (change # with the line number)
Moving by Screens

The commands below are used to rapidly move through the text without needing to scroll.

  • Ctrl + b – go one fullscreen back
  • Ctrl + f – Skip one fullscreen
  • Ctrl + d – move half a screen in front
  • Ctrl + u – move half a screen back
  • Ctrl + e – scroll down one line on the screen without using the cursor
  • Ctrl + y – scrolling up by one line without using the cursor
  • Ctrl + o – go back with the help of history
  • Ctrl + i – moving forward through the history
  • H – skipping directly on the top of the screen
  • M – Skipping directly in the middle of the page
  • L – skipping directly on the bottom of the screen
Inserting Text
  • i – shift to insert mode
  • I – inserting texts at the start of the line
  • a – switch to insert mode after the cursor
  • A – inserting texts at the end of the lines
  • o – opening new lines before the selected one
  • O – Open a new line above the current one
  • ea – inserting texts at the end of a word
  • Esc – Exit insert mode by pressing Esc

Most of these commands are capable of switching between command and insert modes. Vim starts in the command prompt by default, enabling users to move and edit files. Use the Esc key to enter command mode.

3. Marking Text (Visual Mode)

Vim has a visual mode in addition to command and insert modes. This mode is mostly used to highlight text. You can pick between three different graphic modes depending on the amount of text you want to select: character mode, line mode, and block mode.

  • v – use character mode to pick texts
  • V – use line mode to choose lines
  • Ctrl+v – selecting texts with the help of block mode
Visual Commands

Once you’ve picked the appropriate text in visual mode, you can edit it with one of the visual commands. Some of them are mentioned below:

  • y – copying the highlighted text
  • d – deleting the highlighted text
  • p – pasting the highlighted text right after the cursor
  • u – adjust the capitalization of the selected text to lowercase
  • U – adjust the capitalization of the selected text

4. Saving and Exiting File

  • :w – saving files
  • :wq / 😡 / ZZ – saving and closing the file
  • :q – exit
  • :q!/ ZQ – exit without saving the modifications
  • :w new_file_name – rename the file and keep working on the original
  • :sav – saving the file with a new name and editing the newer version
  • :w !sudo tee % – writing the file via sudo and tee command

5. Working with Multiple Files

  • :e file_name – creates new buffers for opening files
  • :bn – jumping directly to the next buffer
  • :bp – return back to the previous buffer
  • :bd – closing a buffer
  • :b# – moving to a specific buffer
  • :b file_name – moving to a buffer based on a specific name
  • :ls – listing down all the buffers that are running
  • :sp file_name – split viewport horizontally and open a file in a new buffer
  • :vs file_name – split viewport vertically and open a file in a new buffer
  • :vert ba – Edit all files as vertical viewports
  • :tab ba – editing buffer as a tab
  • gt – jumping onto the next tab
  • gT – jumping into the previous tab
  • Ctrl+ws – splitting viewports
  • Ctrl+wv – splitting viewports vertically
  • Ctrl+ww – switching viewport
  • Ctrl+wq – Existing viewports
  • Ctrl+wx – exchanging selected viewport with another one
  • Ctrl+= – Adjusting all the viewports to have similar height and width

6. Marks and Jumps

  • m[a-z] – use character mode to mark text (from a to z)
  • M[a-z] – use line mode to mark lines (from a to z)
  • `a – jumping to the place indicated by the letter ‘a’
  • `y`a – yank text to >a> marked position
  • :marks – listing all the marks
  • :jumps – listing all the jumps
  • :changes – listing all the changes
  • Ctrl+i – Moving to the next occurrence in the jump list
  • Ctrl+o – Moving to the previous instance in the jump list
  • g, – Switching to the next instance within the change list
  • g; – Switching to the previous instance within the change list

Conclusion

Since most Linux distributions include Vim by default, knowing basic Vim commands is useful. You should have no problem mastering Vim once you get used to using its commands. Meanwhile, you can use the Vim cheat sheet to have all the important commands at your fingertips.

Leave a Comment