Day 11 - 22th Jan 2025
Quick Summary: Create file, edit and save the file
vi <filename.txt >
- if the file doesn't exist, this makes file with file name.i
- enter insert mode.Write your contents in the file.
esc
:wq
enter
!
Most Important Concepts for Using vi
1. Modes in vi
-
Command Mode: Default mode when you open
vi
. You can navigate and execute commands here. -
Insert Mode: Used for editing text. Enter this mode by pressing
i
. -
Escape Mode: Press
Esc
to return to Command mode.
2. Basic Navigation
-
h
: Move left -
l
: Move right -
k
: Move up -
j
: Move down -
Ctrl + d
: Scroll down -
Ctrl + u
: Scroll up
3. Editing
-
i
: Insert before the cursor. -
a
: Append after the cursor. -
o
: Open a new line below the cursor. -
dd
: Delete the current line. -
x
: Delete the character under the cursor. -
u
: Undo the last action. -
Ctrl + r
: Redo the last undone action.
4. Saving and Exiting
-
:w
: Save changes. -
:q
: Quit. -
:wq
: Save and quit. -
:q!
: Quit without saving.
5. Search
-
/text
: Search forward for "text". -
?text
: Search backward for "text". -
n
: Repeat the search in the same direction. -
N
: Repeat the search in the opposite direction.
6. Copy, Paste, and Cut
-
yy
: Copy (yank) the current line. -
p
: Paste the copied text after the cursor. -
dd
: Cut the current line.
7. Replace Text
-
:s/old/new/
: Replace the first occurrence of "old" with "new" on the current line. -
:s/old/new/g
: Replace all occurrences of "old" with "new" on the current line. -
:%s/old/new/g
: Replace all occurrences in the entire file.
Quick Tips
- Always press
Esc
to ensure you're in Command mode before executing commands. - Practice basic navigation and saving before diving into advanced features.
- To exit quickly without saving (if you're stuck), press
Esc
and type:q!
.
Top comments (0)