translations: [ es/Español ] · [ fr/Français ] · [ de/Deutsch ]
Table of contents
Vi and Vim are powerful text editors available on most Unix-like systems. This guide covers essential commands and techniques for effective use, focusing on keyboard navigation, text manipulation, and interactive modes.
Vi vs. Vim
Vi is the original text editor that is standard on Unix systems, while Vim (Vi IMproved) is an extended version of Vi that includes additional features like syntax highlighting, multi-level undo, and a more robust set of commands. Vim also supports plugins and provides better support for modern programming languages, making it a more powerful tool for developers and advanced users.
Getting Started
To open a file in Vim, use the following command in your terminal:
vim filename.txt
If the file does not exist, Vim will create it.
Modes in Vim
Vim has several modes, but the two most important are:
- Normal mode: For navigating and manipulating text.
- Insert mode: For entering text.
To switch to Insert mode, press i
. To return to Normal mode, press Esc
.
Basic Navigation
Moving the Cursor
Navigating text efficiently is crucial in Vi and Vim. Most cursor movements can be performed in Normal mode, which is the default mode when you open a file. Here are some essential commands for moving the cursor:
h
: Move left by one character.j
: Move down to the next line.k
: Move up to the previous line.l
: Move right by one character.0
: Jump to the beginning of the current line.\$
: Jump to the end of the current line.gg
: Jump to the top of the file.G
: Jump to the bottom of the file.w
: Move forward to the beginning of the next word.b
: Move backward to the beginning of the previous word.e
: Move to the end of the current word.
These commands allow for quick navigation without needing to use the mouse or arrow keys. To perform these movements, you should always be in Normal mode (not Insert mode). You can switch to Normal mode by pressing Esc
.
Scrolling
Ctrl + u
: Scroll up half a pageCtrl + d
: Scroll down half a pageCtrl + b
: Scroll up a full pageCtrl + f
: Scroll down a full page
Deleting Text
Deleting Lines
dd
: Delete the current lineNdd
: DeleteN
lines (e.g.,3dd
deletes three lines)
Deleting Characters
x
: Delete the character under the cursordw
: Delete from the cursor to the end of the wordd\$
: Delete from the cursor to the end of the line
Searching and Replacing Text
Searching
/search_term
: Search forward forsearch_term
?search_term
: Search backward forsearch_term
n
: Repeat the last search in the same directionN
: Repeat the last search in the opposite direction
Simple Replacement
To replace a single instance of a word:
r<char>
: Replace the character under the cursor with<char>
(e.g.,rX
replaces the character with X).
To replace all instances of a word in the current line:
:s/old/new/
: Replace the first occurrence ofold
withnew
in the current line.:s/old/new/g
: Replace all occurrences ofold
withnew
in the current line.
To replace in the entire file:
:%s/old/new/g
: Replace all occurrences ofold
withnew
in the entire file.
Copying and Pasting Text
Copying Text
yy
: Copy (yank) the current lineNyy
: CopyN
lines (e.g.,3yy
copies three lines)
Pasting Text
p
: Paste after the cursorP
: Paste before the cursor
Interactive Mode
Vim can be operated interactively for commands. You can enter commands directly in Normal mode:
:
: Enter command-line mode.- Example commands:
:w
: Save the file.:q
: Quit Vim.:wq
: Save and quit.:q!
: Quit without saving.
- Example commands:
Additional Tips
Undo and Redo
u
: Undo the last actionCtrl + r
: Redo the last undone action
File Navigation
:e filename
: Open another file:split filename
: Split the window and open another file
Exiting Vim
- To save changes and exit, use
:wq
. - To exit without saving changes, use
:q!
.
Created on: Oct 30, 2024
Discover More within this Subject:
- Linux Philosophy Everything Is a File Explained
- Mastering Podman Pods a Comprehensive Guide
- Automating Container Updates With Podman Auto Update
- Networking With Podman a Beginners Guide
- Advanced Container Management With Podman