Wednesday, November 21, 2018

VI keyboard shortcuts

List of shortcut keys
ShortcutCommandHow useful
Insert

VI

iInserts text to the left of the cursor.+       –
IInserts text at the beginning of the line, no matter where the cursor is positioned on the current line.+       –
Append

VI

aBegins inserting after the character (append) on which the cursor is positioned.+       –
ABegins inserting at the end of the current line, no matter where the cursor is positioned on that line.+       –
Open

VI

oBegins inserting text on a new, empty line that is opened for you, below the current line. This is the only command that will allow you to insert text BELOW the LAST line of the file.+       –
OBegins inserting text on a new, empty line that is opened for you, above the current line. This is the only command that will allow you to insert text ABOVE the FIRST line of the file.+       –
Deleting,copying and changing

VI

dDelete text. (see explanation above)+       –
yCopy text (that is, yank it into a holding area for later use). (see explanation above)+       –
cChange text from one thing to another, which you will type. (see explanation above)+       –
!Filter text through a program.+       –
<Shift a region of text to the left.+       –
>Shift a region of text to the right.+       –
Single Key Movements

VI

hMove cursor to the left one character.+       –
lMove cursor to the right one character.+       –
jMove cursor down one line.+       –
kMove cursor up one line.+       –
^Move cursor to the beginning of the line.+       –
$Move cursor to the end of the current line.+       –
1GMove cursor to the first line of your document. Other numbers will move to the line specified by number (ex. 50G goes to the 50th line).+       –
GMove cursor to the last line of your file.+       –
CTRL UMove cursor up in file 12 lines. Hold down the key marked CTRL (stands for control) and type U. CTRL is like another shift key.+       –
CTRL DMove cursor down in file 15 lines.+       –
wMove cursor forward to the next word, stopping at punctuation.+       –
WMove cursor forward to the next word, ignoring punctuation.+       –
eMove cursor forward to the end of the word, stopping at punctuation.+       –
EMove cursor forward to the end of the word, ignores punctuation.+       –
bMove cursor backwards to the previous word, stopping at punctuation.+       –
BMove cursor backwards to the previous word, ignores punctuation.+       –
HMove cursor to the top line of the screen, (as opposed to the top of the document which may not be the same place).+       –
MMove cursor to the middle of the screen.+       –
LMove cursor to the last line on the screen.+       –
%Move cursor to the matching parenthesis, bracket or brace. Great for debugging programs.+       –
(Move cursor to the beginning of the previous sentence (where a punctuation mark and two spaces define a sentence).+       –
)Move cursor to the beginning of the next sentence.+       –
{Move cursor to the beginning of the current paragraph.+       –
}Move cursor to the beginning of the next paragraph.+       –
;Repeat the last f or F command (see below).+       –
Almost Single Key Movements

VI

'Move cursor to a previously marked location in the file. (ex. ma marks the location with the letter a, so a (apostrophe a) moves back to that location).+       –
fFind the character corresponding to the next keystroke typed. Move the cursor to the next occurrence of that character (on the current line only).+       –
FSame as f but movement is backwards.+       –
Useful

VI

xDelete character(s) to the right of the cursor, starting with the one beneath it.+       –
rReplace the character under the cursor with the next character you type. This can be a very useful command. If you wanted to split up a line between two words, you might put the cursor on the blank space before the word you would like to go on the next line and type r . This would replace the space between the words with a carriage return and put the rest of the line onto a new line.+       –
JJoin lines; the opposite of the line splitting operation above. This will join the current line with the next line in your file. Also very useful.+       –
RReplace lines; puts you in INSERT mode but types over the characters that are already on the current line.+       –
pPaste line(s) you deleted (or yanked) back into the file. This is an excellent command if you want to move a few lines somewhere else in your file. Just type 3dd to delete three lines, for example, and then move to where you want those lines to be and type p to paste the lines back into your file below the cursor.+       –
.The period . command repeats the last text modification command, whatever it may have been (insert, deletion, etc).+       –
:r filename RETURNRead a file into the current file being edited. The file be added gets placed below the current cursor position. Please note the colon : before the r in this command.+       –
CTRL LRedraw the screen. If somebody writes to you while you are in the middle of vi and junk appears all over your screen, dont panic, it did not hurt your file, but you will have to hold down the CTRL key and type L to clean it up (CTRL L).+       –
d$Delete (including the current character), to the end of the line.+       –
d^Delete (excluding the current character), to the beginning of the line.+       –
dwDelete a word(s), stops at punctuation.+       –
dWDelete a word(s), ignoring punctuation.+       –
deDelete to the end of next word.+       –
ddDelete a line(s).+       –
dGDelete from the current line to the end of the document. CAREFUL: Slightly dangerous.+       –
dHDelete from the current line to the line shown at the top of the screen.+       –
Search and Replace

VI

/theFinds the next occurence of the. This will also find their, them, another, etc.+       –
?theFinds the previous occurence of the.+       –
nRepeats the last search command. Finds the Next occurence.+       –
d/theDeletes until the next occurence of the. This is to demonstrate how the delete prefix can be used with any cursor movement command.+       –
:g/oldword/s//newword/gcThis will find all occurences of oldword and replace them with newword. The optional c at the end of the command tells vi that you would like to confirm each change. Vi will want you to type in y to make the change or n to skip that replacement. Great for spelling fixes.+       –
Exit

VI

ESC :wq RETURNSave and exit VI+       –
ESC :q! RETURNExit WITHOUT saving changes+       –