vim commands Flashcards
How do you open another file while you are in vim
Enter command mode and type:e filenameYou can also use :e to reload the file that you have currently opened. Typically this is used to discard the edits and start over from the previous saved file
How do you save a file?
Enter command mode and pressing :w If you like to save and quit press :wq
How do you exit vim
Enter command mode and press :q if you edited the file and you like to dicard the edits and exit vim without saving press :q!
Save the contents in vim as a different file
Enter command mode and :w newfilename
Repeats the last change made in normal mode
Enter comamnd mode and type .
Repeat 5 times the last change made in normal mode
Enter command mode and type 5.
move the cursor to the end of the word or beginning of word
e - endb - beginning
move the cursor to the beginning/end of the line
0 beginning of line$ end of line
move the cursor to the end/start of the file
In command modeG - endgg - first line
move the cursor to the bottom of the screen
L
move cursor to line 59. Replace 59 by the desired line number.
:59 to go to line 59
to configure vim to
show line numbers
:set nu
to not show line numbers
:set nonu
Copy commands
Enter escape modePress
y to copy the selected text to clipboard
press yy to copy the current line press
5yy to copy 5 lines
press y$ to copy from cursor to end of line
press y^ to copy from start of line to cursor
Paste clipboard contents
Enter command mode and press p
Delete or cut
Enter escape mode and press dd to Cut current lineD cut from cursor to end of line
Search fred or joe
/fred|joe
Replace all occurrences of old by new in file
enter escape mode and :%s/old/new/g
:%s/onward/forward/gi for case insensitive
:%s/old/new/gc replace all occurrances with confirmation
Replace the beginning of each line by hello
Replace the end of each line by Harry
:%s/^/hello/g
:%s/$/Harry/g
Delete all lines containing string
Delete all lines containing which didn’t contain string
:g/string/d
:v/string/d Delete all lines containing which didn’t contain string
Replace Bill by Steve in current line
Replace Bill by Steve in all the file
:s/Bill/Steve/g
:%s/Bill/Steve/g
:%s/^M//g
Delete DOS carriage returns (^M)
Vu
Lowercase line
VU
Uppercase line
ggguG
Set all text to lowercase
gggUG
Set all text to uppercase
:!pwd
Execute the pwd unix command then returns to Vi
Temporary returns to Unix and return back to vi
:sh return to shellexit to return back to vi
:5,$s/old/new/g
Replace all occurrences from line 5 to EOF
search commands
/word Search word from top to bottom
/\cword top to bottom case insensitive
?word Search word from bottom to top
?\cword bottom to top case in sensitive
How to configure vim to show line numbers each time it starts
Edit the file ~/.vimrc
and add a line
set -o vi
save the file. After that each time you run vim it will show line numbers