Understanding the Basics of vi
Before we jump into the commands, it’s important to grasp how vi operates. Unlike modern graphical editors, vi uses modes to distinguish between inserting text and issuing commands. This modal nature might seem confusing at first, but once you get used to it, it becomes incredibly efficient.Modes in vi
- **Normal Mode (Command Mode):** This is the default mode when you open vi. Here, you can navigate the file, delete text, copy, paste, and perform various commands without inserting new text.
- **Insert Mode:** In this mode, you can insert or edit text. You switch to insert mode from normal mode by pressing keys like `i`, `a`, or `o`.
- **Visual Mode:** Used for selecting blocks of text for copying, deleting, or formatting.
- **Command-line Mode:** Accessed by pressing `:` in normal mode, this mode lets you execute commands such as saving, quitting, or searching within the file.
Essential vi Commands Cheat Sheet for Beginners
If you’re just starting, here’s a curated list of the most important vi commands that will get you editing like a pro in no time:Starting and Exiting vi
- `vi filename` — Open or create a file named "filename".
- `:w` — Save (write) changes to the file.
- `:q` — Quit vi.
- `:q!` — Quit without saving changes.
- `:wq` or `ZZ` — Save and quit.
Switching Between Modes
- `i` — Enter insert mode before the cursor.
- `a` — Enter insert mode after the cursor.
- `o` — Open a new line below and enter insert mode.
- `Esc` — Return to normal mode from any other mode.
Navigation Commands
- `h` — Move cursor left.
- `j` — Move cursor down.
- `k` — Move cursor up.
- `l` — Move cursor right.
- `0` (zero) — Move to the beginning of the line.
- `$` — Move to the end of the line.
- `gg` — Go to the beginning of the file.
- `G` — Go to the end of the file.
- `w` — Jump to the beginning of the next word.
- `b` — Jump to the beginning of the previous word.
Editing and Deleting
- `x` — Delete the character under the cursor.
- `dd` — Delete the entire current line.
- `dw` — Delete from the cursor to the end of the current word.
- `u` — Undo the last change.
- `Ctrl + r` — Redo undone changes.
- `r` — Replace the character under the cursor.
- `c` — Change (delete and enter insert mode).
- `yy` — Yank (copy) the current line.
- `p` — Paste after the cursor.
- `P` — Paste before the cursor.
Searching and Replacing
- `/pattern` — Search forward for "pattern".
- `?pattern` — Search backward for "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" in the entire file.
- `:s/old/new/g` — Replace all occurrences in the current line.
Intermediate vi Commands Cheat Sheet: Boosting Productivity
Once you’re comfortable with the basics, you can unlock more advanced features that take your vi usage to the next level.Visual Mode Commands
Visual mode is handy for selecting blocks of text for operations:- `v` — Enter visual mode (character-wise selection).
- `V` — Enter visual line mode (select whole lines).
- `Ctrl + v` — Enter visual block mode (select columns).
- Once a selection is made, you can perform commands like `d` to delete, `y` to copy, or `>` to indent.
Working with Multiple Files
- `:e filename` — Open a new file within vi.
- `:bn` — Go to the next buffer (file).
- `:bp` — Go to the previous buffer.
- `:bd` — Close the current buffer.
Marks and Jumping
Marks allow you to bookmark positions in your file:- `m[a-z]` — Set a mark named `[a-z]` at the current cursor position.
- ``` `[a-z]` ``` — Jump to the exact position of the mark.
- `'[a-z]` — Jump to the beginning of the line of the mark.
Macros and Repeating Actions
- `q[a-z]` — Start recording a macro into register `[a-z]`.
- Perform the actions you want to record.
- `q` — Stop recording.
- `@[a-z]` — Play back the macro.
- `@@` — Replay the last macro.
Tips and Tricks for Using a vi Commands Cheat Sheet Effectively
No cheat sheet is useful unless you know how to integrate it into your workflow. Here are some practical tips to make the most of your vi commands cheat sheet:- **Practice Consistently**: The modal editing style of vi can be unintuitive at first. Regular practice helps build muscle memory.
- **Keep a Printed or Digital Cheat Sheet Nearby**: When you’re starting, having a quick reference at hand can reduce frustration.
- **Customize Your .vimrc or .exrc**: If you use vim (an enhanced version of vi), you can tweak your configuration file to remap keys or enable helpful plugins.
- **Use Incremental Search**: By enabling incremental search (`set incsearch` in vim), you get real-time feedback as you type search patterns.
- **Leverage Syntax Highlighting**: This doesn’t come with plain vi but is available in vim and greatly improves readability.
- **Combine Commands for Efficiency**: For example, `d2w` deletes two words, and `3dd` deletes three lines, allowing you to chain commands.