In the Terminal, simply type vim
to open editor, or type
vim <filename>.<extension>
to open a new or existing file
Example: vim file.txt
neovim can also be used interchangeably as a replacement for vim
Example: nvim file.txt
type :q <Enter>
to exit
type :q! <Enter>
to exit without saving
vim has a number of modes that can be used as needed. By default vim opens up in NORMAL mode.
In NORMAL mode, vim accepts a number of commands and Key Bindings ( Keyboard Shortcuts ). Every NORMAL mode command starts with a colon :
.
To switch from NORMAL mode to INSERT mode press i
INSERT mode allows us to directly edit text within the file. Any text typed in this mode will affect file buffer, which can then be saved.
To switch from INSERT mode to NORMAL mode, press Esc
In NORMAL mode press :w <Enter>
to write(save) to the file.
To save to file and exit, press :wq <Enter>
The following key bindings will produce mentioned side effects before switching from Normal to Insert mode
- Type
i
, anything typed now will be added before the character highlighted by the cursor. (insert) - Type
a
, anything typed now will be added after the character highlighted by the cursor. (append) - Type
o
, cursor will be shifted to a newly created line below the current. (new line) - Type
<shift>+i
orI
will shift the cursor to the beginning of the current line. (insert) - Type
<shift>+a
orA
will shift the cursor to the end of the current line. (append) - Type
<shift>+o
orO
will shift the cursor to a newly created line above the current line. (new line)
In Normal mode, type :set number <Enter>
to enable line numbers
Use directional keys ←
, ↓
, ↑
, →
to move around the document.
Typing a number before the pressing the directional key will multiply the movement with that number.
For Example:
Typing 5 ↓
will move the cursor 5 units down.
Typing 7 ↑
will move the cursor 7 units up.
Typing 15 →
will move the cursor 15 units right.
The keys h
j
k
l
can be used to move Left, Down, Up, & Right respectively.
h
: Left
j
: Down
k
: Up
l
: Right
This can also be combined with movement multiplication. Example: 4k <Enter>
, 6j <Enter>
Line Numbering should already be enabled with :set number <Enter>
, for this setting to take effect.
In Normal mode, type :set relativenumber <Enter>
to enable relative line numbers.
With this setting, all lines except the current line will be labeled with a relative number. This setting can be combined with movement multiplication.
Type :set mouse=a <Enter>
to enable Mouse click navigation.
Type :set tabstop=4 <Enter>
to make tab characters in your file to appear 4 character cells wide.
Type :set shiftwidth=4 <Enter>
sets tab indents to length 4.
Type :colorscheme <scheme> <Enter>
to set color scheme.
Type :colorscheme <Tab>
to browse available schemes.
Any changes made to the editor configuration will not be made permenant unless saved in the vim configuration file which is .vimrc
located in the user directory.
To edit this file, type vim ~/vimrc
in the terminal.
Here is a sample configuration setting:
set number
set relativenumber
set tabstop=4
set shiftwidth=4
set autoindent
set mouse=a
colorscheme slate
Enter these settings in the ~/.vimrc
file and save with :wq <Enter>
. Now the settings must be permenant.
Switch to Normal Mode with Esc
and
- Type
u
to Undo - Type
<ctrl>+r
to Redo
Type a number followed by above Key Binding to repeat the operation that many times
For example: 5 u
will undo 5 times.
While in Normal Mode, type v
to enter visual mode. This mode is useful for selecting text.
To exit into Normal Mode, press Esc
.
Use navigation keys keys to select text.
After selecting text,
- press
d
to delete selected text & switch to Normal Mode - press
y
to yank/copy selected text, - press
c
to cut/delete text & enter Insert Mode
- press
p
to paste copied/yanked text. ( prints before the cursor in case of in-line paste ) - Press
yy
, to copy/yank entire line. - Press
P
, to paste copied content to a new line. ( prints after the cursor in case of in-line paste )
Multiplication can also be done with above commands. Example: 10p
pastes copied text 10 times.
- Press
cc
to delete entire line in Normal Mode and enter Insert Mode. - Press
D
to delete all text at and right of the cursor, at the line.
To change a single character, place the cursor there and press r
to enter REPLACE mode.
Then press the replacement character key to replace it.
- Press
w
to move cursor to next word. ( space or hyphen word separation ) - Press
W
to move cursor to next word. ( space separation only ) - Press
b
to move cursor to previous word. ( space or hyphen word-separation ) - Press
B
to move cursor to previous word. ( space word-separation only ) - Press
e
orE
to move cursor to end of the word.
- Press
dw
to delete word or to to delete characters at and after the cursor in the word. - Press
db
to delete word or to to delete characters at and before the cursor in the word. - Press
<n>dw
ord<n>w
to delete n words forwards. - Press
<n>db
ord<n>b
to delete n words backwards. - Press
diw
to delete inner word. Useful if the cursor is placed anywhere on the word. - Press
ciw
to delete inner word and enter Insert Mode. - Press
<x>d<y>w
to delete y words x times.
- Press
I
or0
to move to beginning of a line. - Press
$
to go to end of a line.
- Press
dd
to delete entire line. ( Also moves line into registry ) - Press
<n>dd
will delete n number of lines. - Press
d0
to delete all characters on the left side of the cursor to beginning of the line. - Press
d$
orD
to delete all characters at and after the cursor till the end of the line. - Press
yiw
to yank/copy inner word.
Surrounded by
""
ci"
to change inner quotation marks. ( Delete everything surrounded by"
and switch into INSERT mode )di"
to delete inner quotation marks. ( Delete everything surrounded by"
)yi"
to yank/copy inner quotation marks. ( Copy everything surrounded by"
)
Surrounded by
()
ci(
to change inner parenthesis. ( Delete everything surrounded by()
and switch into INSERT mode )di(
to delete inner parenthesis. ( Delete everything surrounded by()
)yi(
to yank/copy inner parenthesis. ( Copy everything surrounded by()
)
- Press
%
to jump to matching opening/closing parenthesis or curly braces or square brackets. - Press
d%
to delete everything inside the brackets including the brackets.
Jumps and Deletion
- Press
f<c>
to jump to the next occurrence of the character<c>
. - Press
t<c>
to jump to character before the next occurrence of the character<c>
. - Press
dt<c>
to delete everything until the next occurrence of the character<c>
. - Press
d<c>
to delete everything until the next occurrence of the character<c>
. - Press
T<c>
to jump to the character right after the previous occurrence of the character<c>
. - Press
F<c>
to jump to the previous occurrence of the character<c>
.
Yanks
- Press
yt<c>
to yank/copy everything until the next occurrence of the character<c>
.
Jumps
- Press
gg
to jump to the beginning of the file. - Press
G
to jump to the end of the file. - Press
<n>G
or:<n><Enter>
to jump to line n. - Press
zz
to bring the cursor line to the center of the screen.
- Press
>>
to indent current line to the right. - Press
<<
to indent current line to the left. - These operations can also be used on selected text to indent multiple lines at once.
Press <shift>+v
to enter Visual Line Mode. In this mode it is possible to select entire lines with movement keys.
Yanking and pasting can also be done after selection.
Press <ctrl>+v
to enter Visual Block Mode. In this mode it is possible to select text column-wise as well as row-wise.
- Press
=
to auto intent a line. - Press
=
after selecting multiple lines to indent multiple lines at once. - Press
gg=G
to Auto Indent the entire file.
- Press
/<x><EnterKey>
to search for of a term,<x>
. ( cycle forwards ) - Then press
n
to cycle forwards through all occurrences. - Press
N
to cycle backwards through all occurrences.
- Press
?<x><EnterKey>
to search backwards for previous occurrences a term,<x>
. ( cycle backwards ) - In backwards search the roles of
n
andN
are reversed.n
cycles backwards andN
cycles forwards
When the cursor is placed on a term,
- Press
#
to find the next occurrence of that term. - Press
*
to find the previous occurrence of that term.
- Press
mx
to mark the cursor position as x. - Press
'x
to go to the position x.
- Enter
:%s/<word>/<replacement>/g
to replace all occurrences of a word in the file with a replacement. - Example:
:%s/character/symbol/g
will remove all occurrences of the wordcharacter
with the wordsymbol
, in the file. (Global Replace):s/<word>/<replacement>/g
to replace all occurrences of a word, within the selected text with a replacement.
- Press
.
to repeat previous command. -
- Example:
- Pressing
.
after pressingdd
will delete line again. - Pressing
.
after pressingdt"
will delete until"
again.
- When you yank, copy, or delete something it is automatically copied into the register memory.
- Type
:reg<Enter>
to view registers - Type
"<register number>p
to paste text from a particular register entry. - Example:
"7p<Enter>
will paste the text from registry 7 to the cursor position."7yy<Enter>
will yank the line at cursor position into registry 7."0p
will paste the last thing you yanked or copied.
- You can also paste something that you just deleted since it will be stored in the registry. In this sense, deleting is same as cutting
A macro is a list/sequence of recorded key-bindings that can be run at any time. This sequence of key-bindings are stored in a register and can be recalled as many times as needed. Every macro is created and identified with a macro name or key, which can be specified during macro creation.
While in Normal Mode:
- Press
q<any-alpha-numeric-key>
to start recording a macro. - Press
q
stop recording the macro. - Press
@<alpha-numeric-key>
to repeat or rerun the recorded key-binding. - Press
<number>@<alpha-numeric-key>
to run the macronumber
times. - Press
:reg<Enter>
to view recorded macros in the registers.
Examples:
- Press
qa
to start recording a macro in the registrya
. - Pressing
qad3wA100[Esc]q
will record a macro in the registrya
. This particular macro will delete three words, enter Insert (append) mode, append '100' and then switch into Normal mode. - Pressing
@a
will recall/rerun the above macro. That is to delete three words, enter Insert (append) mode, append '100' and then switch into Normal mode. - Press
10@a
to run the macro 10 times.
A macro can also use other macros. To do this simply use a macro while recording another macro.
Watched source tutorial till 01.03.11