Vi Editor Commands Flashcards
What are various types of modes in vi editor?
- Command 2. Insert 3. Ex
What is the default vi editor mode on startup?
- Command mode
How to open a new file or open and existing file in vi editor?
vi <filename></filename>
How to quit from vi editor?
Enter command mode by pressing “Esc” and then type
q! - quit without saving
wq - quit after saving
w <filename> - save as functionality</filename>
How to denote a word in vi editor in command line?
‘c’ denotes- change textg
cw - change word
cc - change entire line
c$ - change from cursor positioin to end of line
c0 - change from cursor position to beginning of line
c3w -change the next 3 consecutive words
what does the command :%s/UNIX/Linux/gci do?
Command Breakdown
:: Enters command-line mode in vi.
%: Specifies the entire file as the range for the substitution.
s/UNIX/Linux/g: This is the substitution command.
s: Indicates substitution.
/UNIX/Linux/: Specifies the search pattern (UNIX) and the replacement string (Linux).
g: Global flag; replaces all occurrences on each line. Without g, only the first occurrence per line is replaced.
c: Confirm each substitution.
i: Ignore case while searching.
How to copy in vi editor?
The y command is used to yank (copy) text into the buffer.
yw - yanks a word
y$ - yanks till end of line
y0 - yanks from current position to beginning of line
yy - yanks entire line (alias Y)
3yy- yanks 3 consecutive lines
how to paste copied / deleted words/lines?
‘p’ command is used to paste text that has been deleted or yanked (copied) from the buffer
how to delete a line in vi editor?
‘dd’ deletes a line
8dd deletes 8 consecutive lines
How to see the difference between two files (usually one is updated version of other)?
diff <filename1> <filename2></filename2></filename1>
how to search in vi editor?
Enter command mode by pressing “Esc” then type
/<search> - searches in forward direction - press 'n' to continue search in forward direction , N to continue searching in backward direction
?<search> - searches in backward direction - press 'n' to continue search in backward direction, N to continue searching in forward direction.</search></search>
How to set / undo line numbers in the text file?
Enter command mode by pressing “Esc” then type
:se nu - displays line numbers
:se nonu - hides liine numbers
How to set word wrap
set wm=2 will set the wrap margin to 2