Skip to Content

Ultimate vi Cheat Sheet for Efficient Text Editing in Linux

Introduction to the vi Command

vi (short for “visual editor”) is a screen-oriented text editor that is ubiquitous in Linux and Unix-like operating systems.

It’s known for its power, efficiency, and modal editing approach, which can initially seem cryptic to new users but offers significant advantages once mastered.

Modal Editing: vi’s modal editing distinguishes it from most modern text editors. It has two primary modes:

  • Command Mode: This is the default mode when you open a file in vi. Keystrokes in this mode are interpreted as commands—moving the cursor, deleting text, copying and pasting, searching, and executing various editing operations.
  • Insert Mode: To insert text directly, you must explicitly switch to insert mode using specific commands. In this mode, keystrokes are entered as characters into the file.

Ubiquity and Accessibility: vi is installed by default on virtually all Linux and Unix systems, making it readily available even in minimal environments. This accessibility has made vi an essential tool for system administrators, programmers, and anyone working on remote servers or terminals where graphical text editors might not be practical.

Boost Your Website Speed!

If you want your website to run as fast as ours, consider trying Cloudways. Their powerful cloud infrastructure and optimized stack deliver exceptional performance. Free migration!

Efficiency for Experienced Users: While vi has a steep learning curve, its modal editing and command structure allow experienced users to perform complex editing tasks with remarkable speed and efficiency. Commands can be combined, repeated with counts, and leveraged to manipulate text in sophisticated ways.

Variations: The vim editor (Vi IMproved) is a popular extension of vi that adds features like syntax highlighting, multiple windows, and a more extensive plugin ecosystem. vi and vim are often used interchangeably, with vim being the preferred choice in many cases.

A Detailed vi Command Cheat Sheet for Linux

This cheat sheet focuses on the vi text editor in Linux, going beyond a basic list of commands to provide explanations and context, aiding your understanding of how to effectively use vi.

Essential vi Commands

  • Navigation
    • h, j, k, l: Move the cursor left, down, up, right, respectively.
    • w: Move to the beginning of the next word.
    • b: Move to the beginning of the previous word.
    • 0: Move to the beginning of the line.
    • $: Move to the end of the line.
    • G: Go to the last line of the file.
    • gg: Go to the first line of the file.
    • Ctrl-f: Scroll forward one screen.
    • Ctrl-b: Scroll backward one screen.
  • Entering Insert Mode
    • i: Insert text before the cursor.
    • a: Insert text after the cursor.
    • o: Open a new line below the current line and enter insert mode.
    • O: Open a new line above the current line and enter insert mode.
  • Editing
    • x: Delete the character under the cursor.
    • dw: Delete from the cursor to the end of the word.
    • dd: Delete the current line.
    • D: Delete from the cursor to the end of the line.
    • yy: Yank (copy) the current line.
    • p: Paste the yanked or deleted text after the cursor.
    • P: Paste the yanked or deleted text before the cursor.
    • u: Undo the last change.
    • Ctrl-r: Redo the last undone change.
  • Searching and Replacing
    • /pattern: Search forward for the specified pattern.
    • ?pattern: Search backward for the specified pattern.
    • n: Repeat the last search in the same direction.
    • N: Repeat the last search in the opposite direction.
    • :s/old/new/g: Replace all occurrences of “old” with “new” on the current line.
    • :%s/old/new/g: Replace all occurrences of “old” with “new” in the entire file.
    • :s/old/new/gc: Replace all occurrences of “old” with “new” in the entire file with confirmation for each replacement.
  • Saving and Exiting
    • :w: Save the file.
    • :wq: Save the file and quit.
    • :q!: Quit without saving.
    • 😡: Save and quit (same as :wq).

Advanced vi Features

  • Visual Mode:
    • v: Enter visual mode to select text. You can then operate on the selected text using commands.
    • V: Enter line-wise visual mode to select entire lines.
    • Ctrl-v: Enter block visual mode to select a rectangular block of text.
  • Macros:
    • q: Start recording a macro into the specified letter.
    • q: Stop recording the macro.
    • @: Execute the macro stored in the specified letter.
  • Registers:
    • “p: Paste text from the specified register.
    • “y: Yank text into the specified register.
  • Split Windows:
    • :split or :sp: Split the window horizontally.
    • :vsplit or :vsp: Split the window vertically.
    • Ctrl-w, Ctrl-w: Switch between split windows.
  • Customizing vi:
    • Use :set to customize settings, such as :set number to display line numbers or :set syntax=on to enable syntax highlighting.

Tips for Using vi Effectively

  • Stay in Command Mode: Spend most of your time in command mode and only switch to insert mode when you need to type text. This allows you to take advantage of vi‘s powerful editing commands.
  • Use Counts: Most commands can be preceded by a count to repeat the operation multiple times. For example, 3dd will delete three lines.
  • Learn the Dot Command: The dot command (.) repeats the last editing command. This is incredibly useful for repetitive tasks.
  • Explore the Help System: Use :help within vi to access its built-in help system for more detailed information on specific commands and features.

Conclusion

This cheat sheet provides a solid foundation for using vi effectively. Remember that mastering vi takes time and practice. Experiment with different commands and combinations to improve your workflow and productivity.

Edward Dan

Wednesday 30th of October 2024

Great cheat sheet! It's super helpful for both beginners and seasoned users. I appreciate how you’ve organized the commands; it makes it easy to find what I need quickly.

For those new to vi, it’s worth noting that it's a modal editor, meaning it operates in different modes (like insert and command mode). This can take some getting used to, but it allows for powerful text manipulation. Additionally, vi is available by default on almost all Linux systems, making it an essential tool for sysadmins and developers alike. Thanks for sharing!