Vim Commands Cheat Sheet



Vim cheat sheet Allison McKnight (aemcknig@andrew.cmu.edu) Navigation h Move left H Top line on screen j Move down M Middle line on screen k Move up L Bottom line on screen l Move right 10j Move down 10 lines gg First line of the le e The end of the current word G Last line of the le b Beginning of current. Vim Commands Cheat Sheet. Invoking Vim Exiting Vim Switching Modes Editing a File Moving Around Inserting Text Inserting a File Word Completion Deleting Text Changing (or Replacing) Text Copying (Yanking) and Moving Text: Undo/Redo/Repeat Searching Substituting (Searching and Replacing) Selecting Text (Visual Mode) Marks. This cheat sheet is intended to be a quick reminder for the main concepts involved in using the command line program Vim (or vi) and assumes you already understand its usage. It does not cover every command in Vim, only the ones we consider to be useful for most people for. Basic vi commands (cheat sheet) By admin. Vi is one of the most commonly used editor program and included by default with every UNIX and linux system. Vi basically operates in 3 modes, namely: vi mode – the mode vi starts in. Command mode – you can be in command mode from the vi mode by pressing the key colon (“:”) input mode – in this mode user starts the actual editing of the text.

I’ve compiled a list of essential Vim commands that I use every day. I have then given a few instructions on how to make Vim as great as it should be, because it’s painful without configuration.

Essentials

Cursor movement (Normal/Visual Mode)

  • hjkl - Arrow keys
  • w / b - Next/previous word
  • W / B - Next/previous word (space seperated)
  • e / ge - Next/previous end of word
  • 0 / $ - Start/End of line
  • ^ - First non-blank character of line (same as 0w)

Command-line-cheat-sheet A place to quickly lookup commands (bash, Vim, git, AWS, Docker, Terraform, Ansible, kubectl).

Editing text

  • i / a - Start insert mode at/after cursor
  • I / A - Start insert mode at the beginning/end of the line
  • o / O - Add blank line below/above current line
  • Esc or Ctrl+[ - Exit insert mode
  • d - Delete
  • dd - Delete line
  • c - Delete, then start insert mode
  • cc - Delete line, then start insert mode

Operators

  • Operators also work in Visual Mode
  • d - Deletes from the cursor to the movement location
  • c - Deletes from the cursor to the movement location, then starts insert mode
  • y - Copy from the cursor to the movement location
  • > - Indent one level
  • < - Unindent one level
  • You can also combine operators with motions. Ex: d$ deletes from the cursor to the end of the line.

Marking text (visual mode)

  • v - Start visual mode
  • V - Start linewise visual mode
  • Ctrl+v - Start visual block mode
  • Esc or Ctrl+[ - Exit visual mode

Clipboard

  • yy - Yank (copy) a line
  • p - Paste after cursor
  • P - Paste before cursor
  • dd - Delete (cut) a line
  • x - Delete (cut) current character
  • X - Delete (cut) previous character
  • d / c - By default, these copy the deleted text

Exiting

  • :w - Write (save) the file, but don’t quit
  • :wq - Write (save) and quit
  • :q - Quit (fails if anything has changed)
  • :q! - Quit and throw away changes

Search/Replace

  • /pattern - Search for pattern
  • ?pattern - Search backward for pattern
  • n - Repeat search in same direction
  • N - Repeat search in opposite direction
  • :%s/old/new/g - Replace all old with new throughout file (gn is better though)
  • :%s/old/new/gc - Replace all old with new throughout file with confirmations

General

  • u - Undo
  • Ctrl+r - Redo

Vim Commands Mac Cheat Sheet

Advanced

Cursor movement

  • Ctrl+d - Move down half a page
  • Ctrl+u - Move up half a page
  • } - Go forward by paragraph (the next blank line)
  • { - Go backward by paragraph (the next blank line)
  • gg - Go to the top of the page
  • G - Go the bottom of the page
  • : [num] [enter] - Go to that line in the document
  • ctrl+e / ctrl+y - Scroll down/up one line

Character search

  • f [char] - Move forward to the given char
  • F [char] - Move backward to the given char
  • t [char] - Move forward to before the given char
  • T [char] - Move backward to before the given char
  • ; / , - Repeat search forwards/backwards

Editing text

  • J - Join line below to the current one
  • r [char] - Replace a single character with the specified char (does not use Insert mode)

Visual mode

  • O - Move to other corner of block
  • o - Move to other end of marked area

File Tabs

  • :e filename - Edit a file
  • :tabe - Make a new tab
  • gt - Go to the next tab
  • gT - Go to the previous tab
  • :vsp - Vertically split windows
  • ctrl+ws - Split windows horizontally
  • ctrl+wv - Split windows vertically
  • ctrl+ww - Switch between windows
  • ctrl+wq - Quit a window

Marks

  • Marks allow you to jump to designated points in your code.
  • m{a-z} - Set mark {a-z} at cursor position
  • A capital mark {A-Z} sets a global mark and will work between files
  • '{a-z} - Move the cursor to the start of the line where the mark was set
  • ' - Go back to the previous jump location

Text Objects

  • Say you have def (arg1, arg2, arg3), where your cursor is somewhere in the middle of the parenthesis.
  • di( deletes everything between the parenthesis. That says “change everything inside the nearest parenthesis”. Without text objects, you would need to do T(dt).

General

  • . - Repeat last command
  • Ctrl+r + 0 in insert mode inserts the last yanked text (or in command mode)
  • gv - reselect (select last selected block of text, from visual mode)
  • % - jumps between matching () or {}

Vim is quite unpleasant out of the box. It’s an arcane experience:

  • Autocomplete is missing
  • System clipboard is not used
  • Act of typing :w to save is cumbersome
  • Mouse doesn’t work
  • Management of multiple files is tricky
  • Ability to indent multiple lines is missing

It does have a significant strength though: your fingers can stay on the main keyboard keys to do most editing actions. This is faster and more ergonomic. I find that the toughest part about VIM is guiding people towards getting the benefits of VIM without the drawbacks. Here are two ideas on how to go about this.

Switch caps lock and escape

  • I highly recommend you switch the mapping of your caps lock and escape keys. You’ll love it, promise! Switching the two keys is platform dependent.

Visual Studio Code

  • VSCode is the simplest way to give you a fantastic editor that also gives you the benefits of VIM. Just install the VIM extension.
  • I made a few slight changes which improved the experience for me.

Configure native VIM

For all the given limitations, you’ll need to find a solution. You can either solve the issues one by one, or you can use a reference .vimrc settings file that fix most of the issues out-of-the-box.

  • My .vimrc file could be a good starting point. Honestly, it’s a bit old and not the best. I now use VSCode mainly so I haven’t kept a great vimrc.

Using the system clipboard

  • '+y copy a selection to the system clipboard
  • '+p paste from the system clipboard
  • If this doesn’t work, it’s probably because Vim was not built with the system clipboard option. To check, run vim --version and see if +clipboard exists. If it says -clipboard, you will not be able to copy from outside of Vim.
    • For Mac users, homebrew install Vim with the clipboard option. Install homebrew and then run brew install vim.
      • then move the old Vim binary: $ mv /usr/bin/vim /usr/bin/vimold
      • restart your terminal and you should see vim --version now with +clipboard

Sublime Text

  • Another option is to use Vintageous in Sublime Text (version 3). This gives you Vim mode inside Sublime. I suggest this (or a similar setup with the Atom editor) if you aren’t a Vim master. Check out Advanced Vim if you are.
  • Vintageous is great, but I suggest you change a few settings to make it better.
    • Clone this repository to ~/.config/sublime-text-3/Packages/Vintageous, or similar. Then check out the “custom” branch.
      • Alternatively, you can get a more updated Vintageous version by cloning the official repository and then copying over this patch.
    • Change the user settings (User/Preferences.sublime-settings) to include:
      • 'caret_style': 'solid'
      • This will make the cursor not blink, like in Vim.
      • Sublime Text might freeze when you do this. It’s a bug; just restart Sublime Text after changing the file.
    • ctrl+r in Vim means “redo”. But there is a handy Ctrl + R shortcut in Sublime Text that gives an “outline” of a file. I remapped it to alt+r by putting this in the User keymap
      • { 'keys': ['alt+r'], 'command': 'show_overlay', 'args': {'overlay': 'goto', 'text': '@'} },
    • Mac users: you will not have the ability to hold down a navigation key (like holding j to go down). To fix this, run the commands specified here: https://gist.github.com/kconragan/2510186
  • Now you should be able to restart sublime and have a great Vim environment! Sweet Dude.

Other

I don’t personally use these yet, but I’ve heard other people do!

  • :wqa - Write and quit all open tabs (thanks Brian Zick)

Additional resources

  • Practical Vim is a fantastic resource on many of the useful hidden features of vim.

No matter if you are a sysadmin or a software developer, if you work in the Linux terminal, you would face the situation where you need to edit text files in the terminal.

When you are restricted to the terminal, you must use one of the command line editors like Vim. Most of us are accustomed to the GUI editors and this causes a problem because using a command line editor is not the same as using a graphical text editor like Atom.

There are several command lines text editors available and you are free to use any of them. Vim is one of the most popular command line text editors and you’ll find it installed on any standard Linux distribution. This is why learning the basics of Vim will help you a lot.

Now, this is not a comprehensive guide to make you a Vim expert. It’s a comprehensive Vim guide intended to provide you enough to survive Vim in the Linux terminal.

I hope you did install Vim on Ubuntu or whichever Linux distribution you are using.

Basic Vim Commands You Should Know

If you have worked with Vim previously, maybe you just want to refresh your memories by looking at this Vim cheat sheet:

Vim Commands Cheat Sheet
Vim CommandDescription
iEnter insert mode
EscEnter command mode
x or DelDelete a character
XDelete character is backspace mode
uUndo changes
Ctrl + rRedo changes
yyCopy a line
ddDelete a line
pPaste the content of the buffer
/<search_term>Search and then cycle through matches with n and N
[[ or ggMove to the beginning of a file
]] or GMove to the end of a file
:%s/foo/bar/gciSearch and replace all occurrences with confirmation
Esc + :wSave changes
Esc + :wq or Esc + ZZSave and quit Vim
Esc + :q!Force quit Vim discarding all changes

You can download the above commands as PDF as well:

Don’t worry, I am going to explain these basic Vim commands in detail as well.

Understand the Vim modes

Before we start, let’s know about the Vim modes. Vim has two modes:

  • Command Mode: When you start Vim, you are placed in Command mode. In this mode, you can move across the screen, delete text and copy text.
  • Insert Mode: You cannot write text in command mode. To write text (or let’s say insert text) into a file, there is a dedicated insert mode. When you want to write something on a file, you must enter the insert mode.

Once you know the Vim modes, let’s see some basic Vim commands for various purposes.

Entering insert mode in Vim

Always remember, i stands for insert mode. You press the ‘i’ key to enter the insert mode.

When you enter the insert mode, you’ll see — INSERT — at the bottom of the editor/terminal screen. This indicates that you are in insert mode.

But i is not the only way to enter the insert mode. There are several other commands to enter the insert mode in Vim. The cursor position is important while entering the insert mode.

  • i – New text will appear before the cursor
  • a – New text will appear after the cursor
  • I – New text will appear at the beginning of the current line
  • A – Next text will appear at the end of the current line
  • o – A new line is created after the current line
  • O – A new line is created before the current line

Going back to command mode in Vim

You start in command mode, you go in to insert mode. If you want to move back to the command mode, press Esc (escape) key.

When you have entered your text, I advise hitting Esc key to enter command mode. This way, you won’t enter any new text unknowingly, involuntarily. Tip: If you cannot remember anything else, just remember to press ‘i’ for insert mode and Esc key for command mode. This should be sufficient.

Moving around in Vim

Vim Commands Cheat Sheet

When you are in Vim, you cannot use your mouse to click at in point in the screen like normal text or code editors. This is why you need to know the movement commands to a certain line or word or position in Vim.

Vi/Vim purists will suggest using h, j, k and l keys for moving up, left, right and down respectively when you are in the command mode. While this is applicable to both Vi and Vim editors, I don’t prefer using these weird key-combinations.

Trivia: When the Vi editor was first developed, most keyboards didn’t have arrow keys. This is why h,j,k,l keys were used for the movement.

I use arrow keys for moving around in Vim. It works well even when you are in insert mode in Vim. That’s the standard way of moving around most of the newer generation is accustomed to.

If you are new to Linux or to Vim, you may opt for the arrow keys. No harm done.

However, if you want to be a bit more proficient with Vim, you may want to memorize a few Vim shortcuts for easily moving around the screen. Some of my favorite Vim movement commands are:

  • H – Move to the top of the screen. Note that it doesn’t always mean moving to the first line in the file. It’s just for moving to the top of the visible screen.
  • L – Move to the bottom of the screen. Note that it doesn’t always mean moving to the last line in the file. It’s just for moving to the bottom of the visible screen.
  • M – Move to the middle of the screen.
  • [[ – Move to the first line of the file. You can also ue gg here.
  • ]] – Move to the last line of the file. You can also use G here.
  • nG – Move to line number n. Note that you won’t see anything on the screen while typing the line numbers.

Tip: You can display line numbers in Vim by going into the command mode and typing :set number

As you can see, with these additional movement commands, it will be easier for you to move around in a big text file. You can learn more movement commands like { and } for moving back and forth paragraph, w for moving word by word etc.

You can also use ‘repeater modifiers’ like nG you just saw with most commands in Vim. For example, if you use 5w, it will move 5 words. If you use 6H, it will move to the 6th line from the top of the screen.

Repeater modifiers also work with ‘i’ and this is where many people make mistakes. If you have accidentally pressed a number like 7 before pressing ‘i’ to go into insert mode, whatever text you type will be added 7 times. You won’t see it on the screen immediately until you start saving the document.

So what to do when you make a silly mistake like this? Undo it.

Undo your changes in Vim

If you made a mistake in Vim, you don’t have to live with it forever. I have also seen people quitting the editor without saving in such cases which is sort of ridiculous.

Vim allows you to undo your changes. You can also redo your changes. It’s all applicable in the same session of course. Once you have saved your changes, you cannot undo it.

To undo a change, go to command mode and press ‘u’ key. You can press it several times for multiple undo actions.

If you want to redo a change, press Ctrl+r key combination in the command mode. You can press it several times for multiple redo actions.

Deleting in Vim

Apart from undoing your changes, you might also want to delete some text from the file. In Vim, you can always use the Delete key for deleting a character but there are a few key combinations for better handling the deletion in Vim.

  • x – Delete the character at current cursor position just like a delete key
  • X – Delete the character before the current cursor position like a backspace key. Note that backspace key itself doesn’t work in Vim.
  • dw – Delete word. Actually, it deletes from the current cursor position till the end of the current word plus the white space after it.
  • dd – Delete current line.
  • d$ – Delete from current cursor position till the end of the line.
  • dG – Delete from current cursor position till the end of the file.

Note: There are no cut commands in Vim because when you delete something, the deleted text is placed into the buffer. In other words, delete commands are the cut commands.

Copy-Pasting in Vim

You might wonder how to copy paste in Vim. This is a legitimate concern because you won’t always type in the texts.

There are two kind of copy and paste in Vim. One that deals with the buffer and one that deals with the ‘external’ text.

When you are in command mode, you can either use your Linux terminal shortcuts for copying the text or the following key combinations for copying text:

  • yw – Copy word. Actually, it copies from the current cursor position till the end of the current word plus the white space after it.
  • yy – Copy current line.
  • y$ – Copy from current cursor position till the end of the line.
  • yG – Copy from current cursor position till the end of the file.

Suppose you used one of the delete commands discussed above. The deleted text is placed into the buffer. You can paste this text from the buffer using these two paste commands:

  • p – Paste the contents of the buffer before the cursor position
  • P – Paste the contents of the buffer after the cursor position
Printable

Note that if you have deleted an entire line, the contents will be placed before or after the current line with the above-mentioned paste commands.

Vim Commands Cheat Sheet

The paste commands only work with the Vim buffer. What about the text you copied from some other file? In those cases, you can use the standard copy and paste key combinations of your Linux Terminal.

In Ubuntu and many other Linux distribution’s default terminal, Ctrl+Shift+C is used for copying and Ctrl+Shift+V is used for pasting. In some other terminals, selecting a text copies it and you can paste it using the right click.

Whatever may be the way to copy paste in your Linux terminal, you can also use it in Vim. Just make sure that you are in the insert mode for this type of copy pasting.

Searching for text in Vim

Finding a particular text is an important function of a text editor. You can search for text in the file in Vim using ‘/’.

Vim Commands Cheat Sheet Pdf

In the command mode, use / and then type your search text and press enter. You’ll see whatever you are typing in the bottom left of the screen.

It will do a forward searching for the searched term from your cursor position. You can also use ‘?’ and then type your search term and press enter to perform a backward search. Note that the search is case sensitive.

If there is more than one match for your search text, you can jump to the next position by pressing n key. If you want to go back to the previous match, press N. Basically, with n and N you can cycle through all the matches. Tip: You can use c option to run a case-insensitive search in Vim. Example: /cMy_Search

Search and Replace in Vim

Vim provides a substitute command (:s) for searching and replacing text. It relies heavily on regular expressions (regex) for search and replace.

You can use it in the following fashion:

The above command will replace all the ‘foo’ with ‘bar’ in the entire file. The ‘g’ at the end is responsible for extending the search and replace function on all the matches. Otherwise, only the first match is replaced.

The ‘:s’ command will do the exact same function as above but only in the current line instead of the entire file.

By default, the search is case sensitive. To make it case insensitive, you can use the flag ‘i’ along with g.

I added an additional ‘c’ flag here. Actually, you might not feel comfortable with the idea of your entire matching texts getting replaced in the entire document within seconds. This is where ‘c’ flag helps a lot. With this confirm flag, Vim will ask you to confirm if you want to perform a replace on each match.

In confirmation mode, you’ll be presented with the following option: replace with UU (y/n/a/q/l/^E/^Y)?

Let me explain the choices here:

  • y – YES, replace this match
  • n – NO, don’t replace this match and move to the next one
  • a – Replace ALL matches
  • q – QUIT without replacing any match
  • l – Replace this match and quit as if it was the LAST match
  • ^E – Scroll the screen up
  • ^Y – Scroll the screen down

Tip: If you can’t remember everything else, just remember :%s/foo/bar/gci will try to replace all the matches in entire file with your confirmation.

Saving and quitting Vim

You have just learned the basics of Vim commands. It’s time to save your work and exit Vim. To save or exit Vim, you must enter the command mode first by press Esc key. And then you can use the following options:

Printable Vim Cheat Sheet Pdf

  • :w – Save the changes but don’t exit
  • :wq – Save and quit
  • :q – Just Quit (if you have unsaved changes, you’ll see this warning: E37: No write since last change (add ! to override))
  • :q! – Force quit (it will discard any unsaved changes)

Download Vim Command Cheat Sheet in PDF format

I can understand that if you are new to Vim, remembering what you just learned could be difficult. This is why I created a list of basic vim commands and their quick explanation.

You can download this Vim cheat sheet in PDF format, print it and keep at your desk for a quick reference.

Going further with Vim

Well, you just learned all the Vim basics you need to survive this dreadfully awesome text editor. With this much knowledge, you can read text, search text and do some basic editing in Vim. This will be enough to save you a panic as soon as the idea of using a terminal text editor like Vim strikes you.

You should bookmark this page to refresh what you learned on Vim here. You can also use the vimtutor command. Most Linux distributions have the vimtutor command installed. If you feel like brushing your Vim knowledge or practicing Vim commands, just run vimtutor in a terminal.

If you want to take your Vim skills to next level, I highly recommend the Mastering Vim Quickly resource. It's worth the investment.

I hope this guide helped you to get started with Vim. If you have questions or suggestions, please leave a comment.

Vim Commands Cheat Sheet Pdf

Become a Member for FREE
Become a member to get the regular Linux newsletter (2-4 times a month) and access member-only contents.

Gvim Commands Pdf

Join the conversation.