Thursday, 17 March 2016

vi editor - shortcuts/cheat sheet

vi editor shortcuts, cheat sheet


Invoking vi


Shortcut/commandDescription
vi fileInvoke vi editor on file
vi file1 file2Invoke vi editor on files sequentially
view fileInvoke vi editor on file in read­only mode
vi -R fileInvoke vi editor on file in read­only mode
vi -r fileRecover file and recent edits after system crash
vi + fileOpen file at last line
vi +n fileOpen file at line number n
vi +/patternfile Open file at pattern



Inserting in vi editor


Shortcut/
command
Description
iInsert before cursor
IInsert before line
aAppend after cursor
AAppend after line
oOpen a new line after current line
OOpen a new line before current line
rReplace one character
RReplace many characters
[Ctrl]v charWhile inserting, ignores special meaning of char (e.g., for inserting characters like ESC and CTRL) until ESC is used
[Ctrl]i or tabWhile inserting, inserts one shift width



Changes in inserting mode in vi editor

To delete/revert last inserted word/chars during inserting mode, without leaving insert mode, use these commands.
Shortcut/
command
Description
[Ctrl]h Back /Remove last one character
[Ctrl]w Back one word
[Ctrl]u Back /Remove last Back to beginning of insert



Deleting in vi editor


Shortcut/
command
Description
xDelete character to the right of cursor
nxDeletes n characters starting with current; omitting n deletes current character only
XDelete character to the left of cursor
nXDeletes previous n characters; omitting n deletes previous character only
DDelete to the end of the line
dd or :dDelete current line
:dDelete current line
nddDelete n lines together
:n,mdDeletes lines n through m
dwdelete word
dnwDelete n words
d)Delete to end of sentence
dbDelete previous word
ndbDeletes the previous n words starting with current



Motion/ Moving in vi editor


Shortcut/
command
Description
hMove left
jMove down
kMove up
lMove right
wMove to next word
WMove to next blank delimited word
bMove to the beginning of the word
BMove to the beginning of blank delimted word
eMove to the end of the word
EMove to the end of Blank delimited word
(Move a sentence back
)Move a sentence forward
{Move a paragraph back
}Move a paragraph forward
[[Move a section back
]]Move a section forward
0 or |Move to the begining of the line
n|Moves to the column n in the current line
$Move to the end of the line
1GMove to the first line of the file
GMove to the last line of the file
nGMove to nth line of the file
:nMove to nth line of the file
fcMove forward to c
FcMove back to c
HMove to top of screen
nHMoves to nth line from the top of the screen
MMove to middle of screen
LMove to botton of screen
nLMoves to nth line from the bottom of the screen
%Move to associated ( ), { }, [ ]
[Ctrl]GShows filename, current line number, total lines in file, and % of file location
:.=Display current line number
:=Shows number of lines in file
|Displays tab (^l) backslash (\) backspace (^H) newline ($) bell (^G) formfeed (^L^) of current line



Files, Exit and Save Commands in vi editor


Shortcut/
command
Description
:w fileWrite to file
:wqWrite to file and quit
:w %.newSave current buffer named file as file.new
ZZSame as :wq
:xSave and exit.
:q!Quit without saving changes
:qquit
:e file2Edit other file file2 without leaving vi
:e! file2Discard changes to current file, then edit file2 without leaving vi
:e!Discard all changes since last save
:e#Edit alternate file
:r fileRead file in after line
:r!commandInsert the output from a UNIX command into a file, immediately after the cursor
:nGo to next file
:pGo to previos file



Shell commands in vi editor


Shortcut/
command
Description
:! cmdExecutes shell command cmd; 
!! cmdExecutes shell command cmd, places output in file starting at current line
:!!Executes last shell command
:r! cmdReads and inserts output from cmd
:f fileRenames current file to file
:w !cmdSends currently edited file to cmd as standard input and execute cmd
:cd dirChanges current working directory to dir
:shStarts a sub-shell (CTRL-d returns to editor)
:so fileReads and executes commands in file (file is a shell script)
!}sortSorts from current position to end of paragraph and replaces text with sorted text



Yanking Text/cut copy paste in vi editor


Shortcut/
command
Description
yy or YYank the current line
:yYank the current line
ywYank word to general buffer
“a9ddDelete 9 lines to buffer a
“A9ddDelete 9 lines; Append to buffer a
“apPut text from buffer a after cursor
ppaste yanked clipboard content after cursor
Ppaste yanked clipboard content before cursor
JJoin lines
y$yanks to the end of the line



Changing text in vi editor


Shortcut/
command
Description
CChange to the end of the line
CTextChanges rest of the current line to text until ESC is used
cc or sChange the whole line
sTextSubstitutes text for the current character until ESC is used
xpSwitches character at cursor with following character
cwChange word (Esc)
cwTextChanges current word to text until ESC is used
c$Change to end of line
rcReplace character with c
<< or >>Shifts the line left or right (respectively) by one shift width (a tab)
n<< or n>>Shifts n lines left or right (respectively) by one shift width (a tab)



Markers in vi editor


Shortcut/
command
Description
mcSet marker c on this line
`cGo to beginning of marker c line.
'cGo to first non-blank character of marker c line.
'Return to begining of line containing previous mark
``Return to previous mark or context



Search operations in vi editor


Shortcut/
command
Description
/stringSearch forward for string
?stringSearch back for string
nSearch for next instance of string
NSearch for previous instance of string
fxsearch forward for character x in current line
Fxsearch backward for character x in current line
txsearch forward for character before x in current line
Txsearch backward for character after x in current line
;Repeat previous current­line search
,Repeat previous current­line search in opposite direction
&Repeats last :s command
:set icIgnores case when searching
:set noicPays attention to case when searching
:n,ms/str1/str2/optSearches from n to m for str1; replaces str1 to str2; using opt-opt can be g for global change, c to confirm change (y to acknowledge, to suppress), and p to print changed lines
:g/str/cmdRuns cmd on all lines that contain str
g/str1/s/str2/str3/Finds the line containing str1, replaces str2 with str3
:v/str/cmdExecutes cmd on all lines that do not match str



Replace operations in vi editor

The search and replace function is accomplished with the :s command. It is commonly used in combination with ranges or the :g command
Shortcut/
command
Description
:s/pattern/string/flagsReplace pattern with string according to flags.
gFlag - Replace all occurences of pattern
cFlag - Confirm replaces.
&Repeat last :s command



Regular expressions in vi editor


Shortcut/
command
Description
. (dot)Any single character except newline
*zero or more occurances of any character
[...]Any single character specified in the set
[^...]Any single character not specified in the set
^Anchor - beginning of the line
$Anchor - end of line
\<Anchor - begining of word
\>Anchor - end of word
\(...\)Grouping - usually used to group conditions
\nContents of nth grouping



Ranges in vi editor


Shortcut/
command
Description
:n,mRange - Lines n-m
:.Range - Current line
:$Range - Last line
:'cRange - Marker c
:%Range - All lines in file
:g/pattern/Range - All lines that contain pattern



Parameters in vi editor


Shortcut/
command
Description
:set list Show invisible characters
:set nolist Don’t show invisible characters
:set number Show line numbers
:set nonumber Don’t show line numbers
:set autoindent Indent after carriage return
:set noautoindent Turn off autoindent
:set showmatch Show matching sets of parentheses as they are typed
:set noshowmatch Turn off showmatch
:set showmode Display mode on last line of screen
:set noshowmode Turn off showmode
:set all Show values of all possible parameters



Scrolling in vi editor

To scroll within vi editor, use following commands.
Shortcut/
command
Description
[Ctrl]F Scroll forward one screen
[Ctrl]B Scroll backward one screen
[Ctrl]D Scroll down one­half screen
[Ctrl]U Scroll up one­half screen
[Ctrl]E Show one more line at bottom
Ctrl]Y Show one more line at  top of window
zReposition window: cursor at top
nzMakes the line n the top line on the page
z.Reposition window: cursor in middle
nz.Makes the line n the middle line on the page
z-Reposition window: cursor at bottom
nz-Makes the line n the bottom line on the page



Other operations in vi editor


Shortcut/
command
Description
~Toggle upp and lower case
.Repeat last text-changing command
uUndo last change
UUndo all changes to line



tags: vi editor shortcuts, vi editor cheat sheet, vi editor commands, how to navigate in vi editor

No comments:

Post a Comment