|vi filename||edit filename starting at line 1|
|:q||quit (or exit) vi|
:q! quit vi even though latest changes have not been saved for this vi call
|:0 or 1G or gg||move cursor to first line in file|
|:n or nG||move cursor to line n|
|:$ or G||move cursor to last line in file|
|i||insert text before cursor, until hit|
|/string||search forward for occurrence of string in text|
n move to next occurrence of search string
N move to next occurrence of search string in opposite direction
:e refresh the currently loaded file
|CTRL-u||Moves screen up ½ page|
|CTRL-d||Moves screen down ½ page|
|CTRL-b||Moves screen up one page|
|CTRL-f||Moves screen down one page|
:w write to disk ZZ write to disk and exit :q! exit without writing to disk
Actually, the command for quitting vi is :q. You can save and quit by typing :wq but ZZ does the same thing1 and takes one less keystroke. If there are unsaved changes to the text and you try to quit using :q, vi will warn you that you have unsaved changes and will prevent you from quitting. In order to quit without saving the changes you must use the override switch, !.
For the purist, :wq and ZZ are not exactly the same. :wq always saves, whereas ZZ saves only if changes have been made since the last save.
To paste from clipboard: First, make sure you’re in edit mode (press i). Then you can paste with Ctrl+Shift+V
remove a char -> x
delete the line -> dd
delete the rest of the line -> D
start and end of the line
start of the line -> 0 OR ^
end of the line -> $
Move cursor around
b to move word back(end) and forth(begin).
) to move cursor between sentences(by
. to find next char. and followed with
; for the next(in long line).
this would take advantage of the substitute() command.
% is for the entire file
s is for substituion
g is global,
n is for making no change to the document.
:%s/pattern//gn OR word: :%s/\<word\>//gn
cannot save file due to not-root/not-sudo
In the page Top Ten One-Liners from CommandLineFu Explained is suggested this trick (the #3):
:w !sudo tee %
this write the current buffer to the
stdin of the command after the
% symbol is substituted with the current filename.
show line numbers
delete multiple lines
- from current line to the end:
dGJust to note, the action
dindicates delete, and
Gindicates the last line of the file. So, while at any line you press dG, it deletes all the line starting from the current line till the last one.
- from current line to certain line, first we show line number using the above command, let say we want to delete from 55 to 100, we first
:set nuto see the line number and then navigate to line
55, then we do
d100Gto remove the line from current to
in the last line and edit
Often time, we use
G and in the last line, if I want to start a new line to edit, I have to use
$ to the end and press
i to edit mode and then move cursor to right and then hit
enter to the new line. Turns out i could use one command to do all above:
o Open a new line after current line // this IS the solution!!
O Open a new line before current line
our standard is to use 2 spaces. so have the below setting in
gg=G to format the file from beginning to end. Here
= is the key for format/indent. to indent a line, just use
To paste, use
p, however to paste before the cursor like the beginning of the line, we have to use