Vim Flashcards
Select All
ggVG
- Check that you are in normal mode - hit the ESC key.
- Move the cursor to the beginning of the file - gg.
- Enter visual mode which lets you see the highlight portions - V
- Select from the current cursor position to the end of the file - G
Copy Selected within Vim
After performing the select all sequence above, you can use this command to copy the whole entire file: y
Copy All File to clipboard
So if you wanted to copy the selected code to use outside the terminal you’ll need to use gg”*yG
Move the cursor to the beginning of the file - gg
Copy it to the clipboard from current position - “*y
Move the cursor to the end of the file - G
Vim paste all
In order to paste what you’ve copied, you can hit p to paste after the cursor and P to paste before the cursor.
Vim delete all lines
To clear all the lines of a file: :%d
Go to normal mode - ESC
Enter command mode - :
Select All - %
Delete - d
Undo an accidental deletion
You can use the u command (:u) to undo in Vim, and the lines that were deleted will show up again
Vim Substitute
Here’s a basic invocation of substitute in Vim to search and replace a string:
:s/fizz/buzz
replaces first fizz pattern found on current line with buzz
Use Vim Substitute to Replace Patterns
:s/<fizz|buzz>/fizzbuzz
searches for the first exact matches on the current line of the words fizz or buzz and replaces it with fizzbuzz
deleting the fizz occurrence from the line
:s/fizz//
omitting a replacement string will find the first occurrence of fizz and replace it with no string - effectively deleting the fizz occurrence from the file.
Replace Tabs with Spaces in Vim
Using the pattern matching approach above, we can use vim substitute to replace tabs with spaces: :%s/\t/ /g.
Replace Double Quotes with Single Quotes in a file
Similarly, to replace double quotes with single quotes on the entire file, developers can run this command: :%s/”/’/g.
Vim Substitute Flags
g - global replace all
:s/fizz/buzz/g
note the global - g - flag being added
this will replace all occurrences found of fizz pattern found on the current line with buzz
Without this, the substitute command will run only for the first occurrence on the current line that you’re on.
Vim Substitute Flags
c - confirmation
:s/fizz/buzz/gc
note the confirmation - c - flag being added
replace all occurrences found of fizz pattern found on current line with buzz but confirms with you before making the replacement
Vim Substitute Flags
i - ignore casing
:s/FIZZ/buzz/gi
note the ignore casing - i flag being added
replace all occurrences found of FIZZ pattern regardless of casing found on current line with buzz
searching for HELLO will provide the same results as if you searched for heLlo
Limiting the scope of Vim substitute with ranges
By providing a range option, developers are able to limit the substitution to specific lines or the entire file.
:%s/hello/world
adding a % applies the substitution across the entire file
:2,7s/hello/world
applies the substitution from line 2 to 7
:.,+7s/hello/world
applies the substitution from current line to the next 7 lines
:.,$s/hello/world
applies the substitution from current line to the end of file
Vim find and replace in selection
Alternatively, you can enter visual mode and choose a block selection. After highlighting your intended section, type :
.
:'<,'>s/fizz/buzz/g
applies substitution from the beginning of the selected block to the end of the block
note: entering command line mode with :
will pre-fill the command line with the range :'<,'>
3 Insert modes
Pressing i (for insert) will directly switch to Insert mode.
Pressing a (for append) will switch to Insert mode and move the cursor after the current character.
Pressing o will switch to Insert mode and insert a new line below the line the cursor is on.
Visual mode
To enter the Visual mode from the Normal mode, you can press on v—which will mark the beginning of the selection—and then use the arrow keys to move the cursor to the desired end of the selection.
Alternatively, you can also use:
V (for Visual Line mode) which will automatically select entire lines.
<Ctrl> + v (for Block Visual mode) which will select rectangular regions of the text.
</Ctrl>
Command-line mode
To enter the Command-line mode, you can press on :
, which will cause the cursor to move at the bottom of the window in the command box. You can then write any command you like and press enter to execute it
Save the buffer to the file
:w (for write) will save the buffer to the file.
Attempt to close the program (without saving)
:q (for quit) will attempt to close the program (without saving).
Replace mode
The Replace mode allows you to replace existing text by directly typing over it.
To enter the Replace mode from the Normal mode, you can press on R and start typing characters, which will automatically move the cursor on to the next character.
When you’re done typing, simply press on <Esc> to come back to the Normal mode.</Esc>
Replace mode Undo
Note that when in Replace mode, hitting <Backspace> will automatically undo the changes and replace the new characters by the previous ones.</Backspace>
Delete Lines in Normal mode (4 ways)
dd deletes the whole line under the cursor.
5dd deletes multiple (5) lines, starting at the cursor.
d$ deletes to the end of the line, starting at the cursor.
dG deletes all lines starting from the line under the cursor
Deleting Lines in Visual Mode
Press: Vd
Copying (Yanking) 4 ways
yy: Copy the current line in vi
3yy: To yank multiple lines in vim, type in the number of lines followed by yy. This command will copy (yank) 3 lines starting from your cursor position.
y$: Copy everything from the cursor to the end of the line
y^: Copy everything from the start of the line to the cursor.
yiw: Copy the current word.
Cutting (Deleting)
dd: Cut the current line
3dd: Cut 3 lines, starting from the cursor
d$: Cut everything from the cursor to the end of the line
Putting (Pasting)
P (uppercase): Paste before your cursor
p (lowercase): Paste after your cursor
Basic commands:
insert, delete character, delete line, paste, undo, redo, save, quit, save and quit
i (insert), x (delete character), dd (delete line), p (paste), u (undo), Ctrl-r (redo), :w (save), :q (quit), and :wq (save and quit)
Quit without saving
:q!
Delete the unwanted character in normal mode
x
Insert text after the cursor in normal mode
i
Append at the end of the line
A
Append after the cursor
a
Delete from the cursor up to the next word
dw
Delete from the cursor to the end of a line
d$
Delete to the end of a word
de
Move to the start of the line
0
Move to the end of the line
$
3 forward motions
w, e, $
Motions: until the start of the next word, EXCLUDING its first character
w
Motions: to the end of the current word, INCLUDING the last character
e
Motions: to the end of the line, INCLUDING the last character
$
Count for motions: move the cursor two words forward
2w
Count for motions: move the cursor to the end of the third word forward
3e
Delete the two UPPER CASE words
d2w
Delete a line
dd
Delete 2 lines
2dd
Undo last command
u
Undo everything on the line
U
Redo
<C-r>
CTRL+R
</C-r>
Put after deleting a line
dd (delete a line)
p (paste deleted line)
Replace a character (bad > bed)
re
rx where x is the new character
Change operator: change until the end of a word
(deletes the word and places you in Insert mode)
ce
Change operator: change to the end of a line
c$
Show your location in a file and the file status
<C-g>
</C-g>
Move you to the bottom of the file
G
Move you to the start of the file
gg
Move to a line in the file
10G (go to 10-th line)
Search
/searchword
Search next
n
Search back
N
Search for a phrase in the backward direction
?
instead of /
Return from Search
(back and forward)
<C-o>
<C-i> goes forward
</C-i></C-o>
Find a matching ),], or }
Place cursor and %
Substitute “new” for “old”
:s/old/new/g
/g - globally in the line
Substitute every occurrence of a character string between two lines
:#,#s/old/new/g
where #,# are the line numbers of the range of lines where the substitution is to be done
Сhange every occurrence in the whole file
:%s/old/new/g
Find every occurrence in the whole file, with a prompt whether to substitute or not
:%s/old/new/gc
Execute any external shell command
:!
Get a listing of your directory
:!ls
Write a listing of directory to the file TEST
:!ls
:w TEST
Select text and save to a new file
v motion :w FILENAME
Insert the contents of a file
:r FILENAME
Copy selection
select using visual select
y
Paste copied
p
Open a line BELOW the cursor and start Insert mode
o
Open a line ABOVE the cursor and start Insert mode
O
Replace until pressed ESC
R
Search ignoring lower/upper case
:set ic
Show partial matches for a search phrase
:set is
Highlight all matching phrases
:set hls
Invoke help
:help
:help w
:help c_CTRL-D
:help insert-index
:help user-manual
Follow hyperlink
K
Navigate Page Up/Page Down
<C-b>/<C-f>
</C-f></C-b>
Select between the single quotes
Copy, change, delete
vi’
To make the selections “inclusive” (select also the quotes, parenthesis or braces) you can use a
instead of i
Copy yi(,
Change ci)
Delete di(
Select inside a parenthesis block (select inner block)
vib
Select inside a curly braces block (capital B)
viB
Select lines 4 through 10 without placing cursor on them.
Delete them
:4,10
To delete selection:
:4,10d
Set relative/hybrid line numbers
:set number
:set relativenumber
:set nu rnu # hybrid
Disable line numbers
:set nonumber
:set nornu
Create neovim config file
~/.config/nvim/init.vim
Bind next/previous tab in config file
map ]b :bnext<CR>
map [b :bprevious<CR></CR></CR>
Jump to definition
Ctrl + ]
Jump back
Ctrl + o
Navigate after using Substitute Confirmation flag
Respond y to replace the match, l to replace the match and quit substitute, n to skip the current match, and q to quit.
Copy selection to clipboard
"+y
Jump between left and right split views
CTRL+W, h
- jump leftCTRL+W, l
- jump right
Paste from clipboard
In command mode "+p or "*p.
In insert mode Ctrl+R followed by + or *.
Tab selection
Select text
tab right >
tab left <
Remove without copying to registry.
"_dd