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, $