Basic Vim Commands
作者:XD / 发表: 2024年9月29日 12:43 / 更新: 2024年9月29日 12:43 / 编程笔记 / 阅读量:387
Vim is a powerful text editor with numerous commands. Here's a short guide to help you with basic commands, including how to find and replace text.
1. Opening a File
To open a file in Vim, use:
vim filename
2. Switching Between Modes
- Insert Mode: Press
i
to start editing. - Normal Mode: Press
Esc
to stop editing and run commands.
3. Saving and Exiting
- Save changes and exit:
:wq
- Exit without saving:
:q!
4. Basic Navigation
- Use arrow keys or:
h
(left),j
(down),k
(up),l
(right)
5. Deleting Text
- Delete a character:
x
- Delete a line:
dd
6. Finding Text
To search for a word in Vim: - Forward search (down the document):
/word
Use n
to jump to the next match and N
for the previous match.
- Backward search (up the document):
Similarly, use?word
n
andN
to navigate.
7. Replacing Text
To replace text, Vim has a powerful substitute command: - Replace within the current line:
:s/old/new
This replaces the first occurrence of old
with new
on the current line.
-
Replace all occurrences in the current line:
:s/old/new/g
-
Replace in the entire file:
:%s/old/new/g
-
Replace all occurrences with confirmation:
Vim will prompt you to confirm each replacement with:%s/old/new/gc
y
(yes) orn
(no).
These commands will help you efficiently find, replace, and edit text in Vim. Happy editing!