Vim Flashcards

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
Q

Deleting Lines in Visual Mode

A

Press: Vd

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

Copying (Yanking) 4 ways

A

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.

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

Cutting (Deleting)

A

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

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

Putting (Pasting)

A

P (uppercase): Paste before your cursor
p (lowercase): Paste after your cursor

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

Basic commands:
insert, delete character, delete line, paste, undo, redo, save, quit, save and quit

A

i (insert), x (delete character), dd (delete line), p (paste), u (undo), Ctrl-r (redo), :w (save), :q (quit), and :wq (save and quit)

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

Quit without saving

A

:q!

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

Delete the unwanted character in normal mode

A

x

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

Insert text after the cursor in normal mode

A

i

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

Append at the end of the line

A

A

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

Append after the cursor

A

a

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

Delete from the cursor up to the next word

A

dw

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

Delete from the cursor to the end of a line

A

d$

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

Delete to the end of a word

A

de

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

Move to the start of the line

A

0

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

Move to the end of the line

A

$

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

3 forward motions

A

w, e, $

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

Motions: until the start of the next word, EXCLUDING its first character

A

w

42
Q

Motions: to the end of the current word, INCLUDING the last character

A

e

43
Q

Motions: to the end of the line, INCLUDING the last character

A

$

44
Q

Count for motions: move the cursor two words forward

A

2w

45
Q

Count for motions: move the cursor to the end of the third word forward

A

3e

46
Q

Delete the two UPPER CASE words

A

d2w

47
Q

Delete a line

A

dd

48
Q

Delete 2 lines

A

2dd

49
Q

Undo last command

A

u

50
Q

Undo everything on the line

A

U

51
Q

Redo

A

<C-r>
CTRL+R
</C-r>

52
Q

Put after deleting a line

A

dd (delete a line)
p (paste deleted line)

53
Q

Replace a character (bad > bed)

A

re
rx where x is the new character

54
Q

Change operator: change until the end of a word
(deletes the word and places you in Insert mode)

A

ce

55
Q

Change operator: change to the end of a line

A

c$

56
Q

Show your location in a file and the file status

A

<C-g>
</C-g>

57
Q

Move you to the bottom of the file

A

G

58
Q

Move you to the start of the file

A

gg

59
Q

Move to a line in the file

A

10G (go to 10-th line)

60
Q

Search

A

/searchword

61
Q

Search next

A

n

62
Q

Search back

A

N

63
Q

Search for a phrase in the backward direction

A

?
instead of /

64
Q

Return from Search
(back and forward)

A

<C-o>
<C-i> goes forward
</C-i></C-o>

65
Q

Find a matching ),], or }

A

Place cursor and %

66
Q

Substitute “new” for “old”

A

:s/old/new/g
/g - globally in the line

67
Q

Substitute every occurrence of a character string between two lines

A

:#,#s/old/new/g

where #,# are the line numbers of the range of lines where the substitution is to be done

68
Q

Сhange every occurrence in the whole file

A

:%s/old/new/g

69
Q

Find every occurrence in the whole file, with a prompt whether to substitute or not

A

:%s/old/new/gc

70
Q

Execute any external shell command

A

:!

71
Q

Get a listing of your directory

A

:!ls

72
Q

Write a listing of directory to the file TEST

A

:!ls
:w TEST

73
Q

Select text and save to a new file

A

v motion :w FILENAME

74
Q

Insert the contents of a file

A

:r FILENAME

75
Q

Copy selection

A

select using visual select
y

76
Q

Paste copied

A

p

77
Q

Open a line BELOW the cursor and start Insert mode

A

o

78
Q

Open a line ABOVE the cursor and start Insert mode

A

O

79
Q

Replace until pressed ESC

A

R

80
Q

Search ignoring lower/upper case

A

:set ic

81
Q

Show partial matches for a search phrase

A

:set is

82
Q

Highlight all matching phrases

A

:set hls

83
Q

Invoke help

A

:help
:help w
:help c_CTRL-D
:help insert-index
:help user-manual

84
Q

Follow hyperlink

A

K

85
Q

Navigate Page Up/Page Down

A

<C-b>/<C-f>
</C-f></C-b>

86
Q

Select between the single quotes
Copy, change, delete

A

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
Q

Select inside a parenthesis block (select inner block)

A

vib

88
Q

Select inside a curly braces block (capital B)

A

viB

89
Q

Select lines 4 through 10 without placing cursor on them.
Delete them

A

:4,10

To delete selection:
:4,10d

90
Q

Set relative/hybrid line numbers

A

:set number
:set relativenumber
:set nu rnu # hybrid

91
Q

Disable line numbers

A

:set nonumber
:set nornu

92
Q

Create neovim config file

A

~/.config/nvim/init.vim

93
Q

Bind next/previous tab in config file

A

map ]b :bnext<CR>
map [b :bprevious<CR></CR></CR>

94
Q

Jump to definition

A

Ctrl + ]

95
Q

Jump back

A

Ctrl + o

96
Q

Navigate after using Substitute Confirmation flag

A

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
Q

Copy selection to clipboard

A

"+y

98
Q

Jump between left and right split views

A

CTRL+W, h - jump left
CTRL+W, l - jump right

99
Q

Paste from clipboard

A

In command mode "+p or "*p.
In insert mode Ctrl+R followed by + or *.

100
Q

Tab selection

A

Select text
tab right >
tab left <

101
Q

Remove without copying to registry.

A

"_dd