This guide provides a practical, step-by-step Linux Vim cheat sheet to help you master the text editor from command line to file manipulation. You will need access to a Linux-based operating system, such as Ubuntu, CentOS, or macOS, with the terminal application installed. No external software downloads are required; Vim is typically included by default in most Linux distributions. This guide covers the basic modes, essential navigation commands, file operations, search and replace, and advanced editing techniques to make you proficient quickly.

What You'll Need

  • Linux Terminal: A command-line interface, such as Bash, Zsh, or Fish, running on a Linux server or desktop.
  • Text File (Optional): A file created for practice, such as test.txt, to apply commands.
  • Basic Keyboard Proficiency: Familiarity with standard typing and the Shift, Ctrl, and Alt keys.

Step-by-Step

  1. Open Vim and Enter Command Mode: Navigate to your terminal and type vim filename.txt and press Enter. The file will open, and you will be in Command Mode. In this mode, Vim treats keystrokes as commands rather than text. The bottom left corner of the screen typically displays -- INSERT -- to indicate you are in Insert Mode.
  2. Enter Insert Mode to Write Text: Press the i key on your keyboard to switch to Insert Mode. You can now type text into the file just as you would in a standard word processor. The status bar at the bottom will change to show -- INSERT --. Type a few lines of text, such as "Hello World" and a blank line, to use for practice.
  3. Navigate the File with Movement Commands: Press the Esc key to return to Command Mode. Use the arrow keys to move around, but for faster navigation, use the following commands: h moves left, j moves down, k moves up, and l moves right. This layout mimics the Vim movement keys.
  4. Delete Text with Delete Commands: Place your cursor on the line you wish to modify. In Command Mode, press x to delete the character under your cursor. Press dw to delete from the current cursor position to the end of the word. Press d$ to delete from the current cursor position to the end of the line.
  5. Copy and Paste with Yank and Put: You can copy text without removing it using the y (yank) command. Move your cursor to the beginning of the text you want to copy. Press yy to copy the entire current line. Move your cursor to the line where you want to paste the text and press p to paste it below the current line, or P to paste it above.
  6. Search for Text Within the File: In Command Mode, press the forward slash key / followed by the text you want to find and press Enter. Vim will highlight the first occurrence of the text. To find the next occurrence, press n. To find the previous occurrence, press N.
  7. Replace Text Using Substitute Command: To replace a single occurrence of a word, use the :%s/old_word/new_word/g command. Replace old_word with your target text and new_word with your replacement. The % symbol indicates the entire file, s stands for substitute, and g stands for global (replace all occurrences on the line). For a case-insensitive replacement, add the i flag: :%s/old_word/new_word/gi.
  8. Undo and Redo Actions: Press u to undo the last command. Press Ctrl + r to redo an undone action.
  9. Save and Exit the File: After making changes, press Esc to ensure you are in Command Mode. To save the file and exit, type :wq and press Enter. This writes the file to disk and closes Vim. To save the file without exiting, type :w and press Enter. To exit without saving changes, type :q! and press Enter.
  10. Save and Quit Without Writing: If you made changes and accidentally typed :q, Vim will display an error message like E37: No write since last change (add ! to override). To force quit, add an exclamation point: :q!. This is a critical command to remember when you want to discard all changes and close the file.

Common Mistakes

One of the most common mistakes when using Vim is forgetting that you must press Esc to return to Command Mode before saving or exiting. If you are in Insert Mode and type :wq, Vim will treat the colon as part of your text. Always check the bottom left corner of the screen for the mode indicator; if it shows -- INSERT --, you are still in Insert Mode.

Another frequent error is attempting to use mouse clicks to select text. Vim does not support mouse selection by default. To select text, you must use visual mode commands like v (character-wise), V (line-wise), or Ctrl + v (block-wise) to enter the appropriate visual mode before copying or deleting.

Users also often struggle with the difference between p and P. p pastes the text after the cursor, while P pastes it before the cursor. This distinction is crucial for precise text placement.

Tips

Use the h, j, k, l keys for navigation instead of the arrow keys whenever possible. This saves your hands from having to move across the keyboard and allows for faster, more fluid movement in the terminal.

Learn the dd command to delete an entire line. It is faster and more reliable than using the arrow keys to highlight a line and then pressing Ctrl + x.

Use the w and b commands to jump between words. w moves to the start of the next word, while b moves to the start of the previous word. This is much faster than using the arrow keys to navigate word by word.

Use the ZZ shortcut to save and exit. This is a common shortcut that saves you from having to type :wq. It is equivalent to typing :wq.

Use the :e filename command to edit a file without closing Vim. This allows you to quickly switch between files and reference content from one file while editing another.

Use the Ctrl + g command to view the filename and line number of the current file. This is helpful for keeping track of your location within a large file.

Troubleshooting

If Vim opens and shows a blank screen, the issue is likely that you are in Command Mode and the file is empty. Press i to enter Insert Mode and start typing. If you cannot see the text, press Ctrl + l to redraw the screen.

If you are in Insert Mode and cannot type, check to see if you are in a specific mode like Replace Mode. Press Esc to return to Command Mode. If you are in Replace Mode, the bottom right corner of the screen will show -- REPLACE --. Press Esc to exit Replace Mode.

If Vim freezes or becomes unresponsive, you can force it to close by pressing Ctrl + c. This will send an interrupt signal to Vim and close the terminal window.

If you cannot save a file because of permission errors, check the file permissions. You may need to use the chmod command to change the file permissions. For example, chmod 644 filename.txt will give the owner read and write permissions, and the group and others read-only permissions.

Frequently Asked Questions

How do I exit Vim?

To exit Vim, first press Esc to ensure you are in Command Mode. Then, type :q to quit, :wq to save and quit, or :q! to quit without saving changes. You can also use the shortcut ZZ to save and quit.

How do I insert text at the beginning of a line?

Press Esc to return to Command Mode. Move your cursor to the first character of the line where you want to insert text. Press I (capital i) to enter Insert Mode. You can now type text at the beginning of the line.

How do I search for a word in Vim?

Press Esc to go to Command Mode. Press the forward slash key / followed by the word you want to search for and press Enter. Vim will highlight the first occurrence. Press n to find the next occurrence, and N to find the previous one.

How do I copy multiple lines in Vim?

Press Esc to go to Command Mode. Move your cursor to the first line you want to copy. Press yy to copy the line. Move your cursor to the last line you want to copy. Press yy again. Move your cursor to the line where you want to paste the text and press p to paste all copied lines.

How do I undo an action in Vim?

Press u to undo the last action. Press Ctrl + r to redo an action that was previously undone.

How do I delete a word in Vim?

Press Esc to go to Command Mode. Move your cursor to the beginning of the word you want to delete. Press dw to delete the word.