Vim Flashcards

(101 cards)

1
Q

Select All

A

ggVG

  1. Check that you are in normal mode - hit the ESC key.
  2. Move the cursor to the beginning of the file - gg.
  3. Enter visual mode which lets you see the highlight portions - V
  4. Select from the current cursor position to the end of the file - G
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

Copy Selected within Vim

A

After performing the select all sequence above, you can use this command to copy the whole entire file: y

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Copy All File to clipboard

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Vim paste all

A

In order to paste what you’ve copied, you can hit p to paste after the cursor and P to paste before the cursor.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Vim delete all lines

A

To clear all the lines of a file: :%d

Go to normal mode - ESC
Enter command mode - :
Select All - %
Delete - d

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Undo an accidental deletion

A

You can use the u command (:u) to undo in Vim, and the lines that were deleted will show up again

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Vim Substitute

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

Use Vim Substitute to Replace Patterns

A

: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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

deleting the fizz occurrence from the line

A

: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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

Replace Tabs with Spaces in Vim

A

Using the pattern matching approach above, we can use vim substitute to replace tabs with spaces: :%s/\t/ /g.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Replace Double Quotes with Single Quotes in a file

A

Similarly, to replace double quotes with single quotes on the entire file, developers can run this command: :%s/”/’/g.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Vim Substitute Flags
g - global replace all

A

: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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Vim Substitute Flags
c - confirmation

A

: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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Vim Substitute Flags
i - ignore casing

A

: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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

Limiting the scope of Vim substitute with ranges

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Vim find and replace in selection

A

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 :'<,'>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

3 Insert modes

A

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.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

Visual mode

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

Command-line mode

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
20
Q

Save the buffer to the file

A

:w (for write) will save the buffer to the file.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
21
Q

Attempt to close the program (without saving)

A

:q (for quit) will attempt to close the program (without saving).

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
22
Q

Replace mode

A

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>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
23
Q

Replace mode Undo

A

Note that when in Replace mode, hitting <Backspace> will automatically undo the changes and replace the new characters by the previous ones.</Backspace>

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
24
Q

Delete Lines in Normal mode (4 ways)

A

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

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
25
Deleting Lines in Visual Mode
Press: Vd
26
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.
27
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
28
Putting (Pasting)
P (uppercase): Paste before your cursor p (lowercase): Paste after your cursor
29
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)
30
Quit without saving
:q!
31
Delete the unwanted character in normal mode
x
32
Insert text after the cursor in normal mode
i
33
Append at the end of the line
A
34
Append after the cursor
a
35
Delete from the cursor up to the next word
dw
36
Delete from the cursor to the end of a line
d$
37
Delete to the end of a word
de
38
Move to the start of the line
0
39
Move to the end of the line
$
40
3 forward motions
w, e, $
41
Motions: until the start of the next word, EXCLUDING its first character
w
42
Motions: to the end of the current word, INCLUDING the last character
e
43
Motions: to the end of the line, INCLUDING the last character
$
44
Count for motions: move the cursor two words forward
2w
45
Count for motions: move the cursor to the end of the third word forward
3e
46
Delete the two UPPER CASE words
d2w
47
Delete a line
dd
48
Delete 2 lines
2dd
49
Undo last command
u
50
Undo everything on the line
U
51
Redo
CTRL+R
52
Put after deleting a line
dd (delete a line) p (paste deleted line)
53
Replace a character (bad > bed)
re rx where x is the new character
54
Change operator: change until the end of a word (deletes the word and places you in Insert mode)
ce
55
Change operator: change to the end of a line
c$
56
Show your location in a file and the file status
57
Move you to the bottom of the file
G
58
Move you to the start of the file
gg
59
Move to a line in the file
10G (go to 10-th line)
60
Search
/searchword
61
Search next
n
62
Search back
N
63
Search for a phrase in the backward direction
? instead of /
64
Return from Search (back and forward)
goes forward
65
Find a matching ),], or }
Place cursor and %
66
Substitute "new" for "old"
:s/old/new/g /g - globally in the line
67
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
68
Сhange every occurrence in the whole file
:%s/old/new/g
69
Find every occurrence in the whole file, with a prompt whether to substitute or not
:%s/old/new/gc
70
Execute any external shell command
:!
71
Get a listing of your directory
:!ls
72
Write a listing of directory to the file TEST
:!ls :w TEST
73
Select text and save to a new file
v motion :w FILENAME
74
Insert the contents of a file
:r FILENAME
75
Copy selection
select using visual select y
76
Paste copied
p
77
Open a line BELOW the cursor and start Insert mode
o
78
Open a line ABOVE the cursor and start Insert mode
O
79
Replace until pressed ESC
R
80
Search ignoring lower/upper case
:set ic
81
Show partial matches for a search phrase
:set is
82
Highlight all matching phrases
:set hls
83
Invoke help
:help :help w :help c_CTRL-D :help insert-index :help user-manual
84
Follow hyperlink
K
85
Navigate Page Up/Page Down
/
86
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(`
87
Select inside a parenthesis block (select inner block)
`vib`
88
Select inside a curly braces block (capital B)
`viB`
89
Select lines 4 through 10 without placing cursor on them. Delete them
:4,10 To delete selection: :4,10d
90
Set relative/hybrid line numbers
:set number :set relativenumber :set nu rnu # hybrid
91
Disable line numbers
:set nonumber :set nornu
92
Create neovim config file
~/.config/nvim/init.vim
93
Bind next/previous tab in config file
map ]b :bnext map [b :bprevious
94
Jump to definition
Ctrl + ]
95
Jump back
Ctrl + o
96
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.
97
Copy selection to clipboard
`"+y`
98
Jump between left and right split views
`CTRL+W, h` - jump left `CTRL+W, l` - jump right
99
Paste from clipboard
In command mode `"+p or "*p.` In insert mode `Ctrl+R followed by + or *.`
100
Tab selection
Select text tab right `>` tab left `<`
101
Remove without copying to registry.
`"_dd`