Simple Text Editors
vi
- Console
Commands
Save and quit
ZZ Save and quit :x Save and quit :wq Save and quit :q Quit :q! Quit without saving :w Save :w <file-name> Save as <file-name>
Insert mode
a After the current cursor position A End of the current line i Before the current cursor position I Beginning of the current line esc Switch back to command mode
Delete
x Delete character at current cursor position X Delete character before the cursor cc Replace current line with a blank line dG Delete all lines from cursor to end of file
Traversing a file
:<n> Go to line number n G last line in file
0 First character on the current line ^ First non-whitespace character on the current line $ end of current line w next word b previous word
% go to matching parenthesis, curly brace, or square bracket
Copy and paste
yy Copy current line dd Cut current line into unnamed buffer (delete) y Copy selection delete Cut selection p Paste after the cursor P Paste before the cursor
Visually select text
- Define one end of the selection area:
v Character select shift + v Line select ctrl + v Column select
- Move the cursor using hjkl or the arrow keys to define the other end
Search and replace
:/pattern Search for the specified pattern n Search for the next occurrence of the pattern N Search for the previous occurrence of the pattern
:s/a/b/g Replace every occurrence of "a" with "b" on the current line :%s/a/b/g Replace every occurrence of "a" with "b" in the entire file :s/a/b/c Confirm the substitution :n1,n2s/a/b/g Run the substitution for lines n1 to n2
:hi clear search Disable the search highlighting
Undo
u Undo the last action U Undo recent changes to the current line . Redo the last change (period)
Misc commands
o Add a blank line after the current line O Add a blank line before the current line
Configuration
Set
:set all Show a list of "set" options :set [no]list Show hidden characters :set [no]number Show line numbers :set [no]ignorecase Perform case insensitive searches (:set ic)
Config file
- ~/.vimrc
- Putting commands in this file is the same as typing them individually in vi (:set ...)
Soft tabs
set expandtab set tabstop=4 set shiftwidth=4
Misc
Multiple commands
- Can run multiple commands by separating them with a "|" character:
:s/a/b/g | s/c/d/g
Editing a set of files
vi file1 file2 vi $(...)
- Use
:next,:prev,:first, and:lastto switch between the buffers
vi $(find . -name "*.php" -exec grep -il "text" {} \;)
Resources URL:
notes/linux/resources
Sources URL:
notes/linux/sources