vi (Visual Editor) is a powerful text editor commonly used in Linux and Unix environments. It's known for its efficiency and versatility, especially when working with large text files.
vi operates in three primary modes:
- Command Mode: This is the default mode when you first open
vi. You can enter commands to navigate, edit, and save the file. - Insert Mode: Used to insert text directly into the document. You enter this mode by pressing
i,a,o, orO. - Visual Mode: Used to select blocks of text for editing or copying.
ZZ: Save and exit.A: Append text to the end of the current line.o: Open a new line below the current line.O: Open a new line above the current line.u: Undo the last change.yy: Yank (copy) the current line.dd: Delete the current line.p: Paste from the system clipboard.P: Paste before the cursor.d0: Delete from the beginning of the line to the cursor.d$: Delete from the cursor to the end of the line.w: Move the cursor to the beginning of the next word.w <filename>: Save the current file as a new file with the specified name.- Character-level commands:
x: Delete the character under the cursor.X: Delete the character before the cursor.r: Replace the character under the cursor.yw: Yank (copy) the current word.<num>dw: Delete the specified number of words.
:wq: Write (save) the file and quit.:q!: Quit without saving.:: Enter command mode (you're already in it)./pattern: Search forward for a pattern.?pattern: Search backward for a pattern.:set nuorset number: Set the number display option (shows line numbers).
vim file1.txt file2.txt:args: List the open files.:rew: Rewind to the first file.:n: Go to the next file.:syntax on: Enable syntax highlighting.:set all: List all current settings.
Tip
Remember: vi can be intimidating at first, but with practice, you'll become proficient in its powerful features.