VI editor
VI is useful in a CLI-only envireonment, especially when changing remote configurations in a service.
Basics
vi sample.txtCreates a text file- A tilde
~represents an unused line; if there is an empty line without the tilde, there is a newline or tab character present :q!quits the editor (quit):w!saves the change in file (write):w newfile.txtsave as newfile.txt:wq!saves the file and quits the editor
Modes in VI
VI is a model editor that has two modes: Command and Insert
- Command mode
- Default mode
- Enables administrative tasks (i.e. saving the file, executing commands, moving cursor, etc)
- Use this mode when you want to move around the file without changing it (“a safe place”)
- Insert mode
- Enables you to insert text into file
- Hit
ito change to insert mode - Hit
escto get out of insert mode`
Making changes in file
- Deleting
dd: delete a whole linen dd: delete n lines
D: delete the text on the right side of the cursor
- Reverting
u: undo the last action- Ctrl
r: redo the last action
Command mode
- Searching
- Special characters must be preceded by a backslash (i.e.
\n) - Hit Enter: take cursor to the first character of the first match
/patternperforms a forward search; it pattern matches the text after the cursorNto continue searching
?patternperforms a backward search; it pattern matches the text before the cursornto continue searching
- Special characters must be preceded by a backslash (i.e.
- Search and replace
:%s/pattern/replace/gc- Replace
patternwithreplace gchanges all occurences (greedy)casks for confirmation
- Replace
- Changing display using
:setnu: display line numbers on the left hand sidewm: word wrap the textwm=2: set word wrap margin to 2 characters