Vim
What is Vim?
Vim is a greatly improved version of the good old UNIX editor Vi. Many new features have been added: multi-level undo, syntax highlighting, command line history, on-line help, spell checking, filename completion, block operations, script language, etc. There is also a Graphical User Interface (GUI) available. Still, Vi compatibility is maintained, those who have Vi “in the fingers” will feel at home. See runtime/doc/vi_diff.txt for differences with Vi.
This editor is very useful for editing programs and other plain text files. All commands are given with normal keyboard characters, so those who can type with ten fingers can work very fast. Additionally, function keys can be mapped to commands by the user, and the mouse can be used.
Vim runs under MS-Windows (XP, Vista, 7, 8, 10), macOS, Haiku, VMS and almost all flavours of UNIX. Porting to other systems should not be very difficult. Older versions of Vim run on MS-DOS, MS-Windows 95/98/Me/NT/2000, Amiga DOS, Atari MiNT, BeOS, RISC OS and OS/2. These are no longer maintained.
——Github/Vim
How to use Vim?
In Terminal
vim
: Open Vimvim [filename]
: Open [filename] in Vim
Modes
In Vim, there are three modes of operation: Normal, Insert, and Visual.
Normal mode
Normal mode is the initial mode of the Vim editor. When you open a new file edit an existing one, it starts in normal mode by default. In normal mode, you cannot insert any character. Normal mode is also known as command mode because all the keystrokes you perform are interpreted as commands.
To access normal mode from other modes, press Esc
key.
Insert mode
Insert mode is where you can insert your text in the file. This mode inserts every character you type at the current cursor location.
Visual mode
Visual mode allows you to select text so that you may perform certain operations (cut, copy, delete) on it.
Changing the modes
- When creating or opening a file in vim, it first opens in Normal mode.
- Switch to Insert mode from Normal mode:
i
orI
,o
,O
,a
,A
. - Switch to Visual mode from Normal mode:
v
orV
- Switch between Inset mode and Visual mode: shift to Normal mode first.
Commands (in Normal mode)
File related commands
Key | Command |
---|---|
:w |
Write the file to the disk |
:w [filename] |
Save the file as [filename] |
:q |
Quit vi without saving the file |
:wq |
Write the file to disk and quit vim |
:wq [filename] |
Quit and save the file as [filename] |
:q! |
Ignore the warning and discard the change |
Moving the cursor
Key | Command |
---|---|
h / Left |
Move the cursor left one character |
j / Down |
Move the cursor down one line |
k / Up |
Move the cursor up one line |
l / Right |
Move the cursor right one character |
w |
Move the cursor forward one word |
b |
Move the cursor backward one word |
Control f / Page Down |
Move the cursor forward one page |
Control b / Page Up |
Move the cursor backward one page |
[number]gg |
Move the cursor to line [number] |
[number]j |
Move the cursor down [number] lines |
[number]k |
Move the cursor up [number] lines |
Finding text
Key | Command |
---|---|
/[text] |
Find all [text] |
n |
Move to next [text] |
Shift n |
Move to previous [text] |
Inserting text
Key | Command |
---|---|
I |
Insert text at the beginning of the line |
i |
Insert text before the current cursor location |
a |
Insert text after the current cursor location |
o |
Create a new line for the text below the current cursor location |
O |
Create a new line for text above the current cursor location |
Changing text
Key | Command |
---|---|
cc |
Remove the whole line and start Insert mode |
c[number]c |
Remove the whole [number] lines and start Insert mode |
s |
Remove the character under the cursor and start Insert mode |
r |
Replace the character under the cursor |
Copying & Pasting
Key | Command |
---|---|
y |
Copy the selected text to clipboard |
yy |
Copy current line |
P |
insert the text before the cursor |
p |
Insert the text at the point after the cursor |
Deleting text
Key | Command |
---|---|
X |
Delete the character before the current location |
x |
Delete the character under the current location |
D |
Cut to the end of line |
dd |
Cut current line |
Undo & Redo
Key | Command |
---|---|
u |
Undo last change |
Control r |
Redo last change |
Customize Vim
Enter vim ~/.vimrc
in Terminal
syntax on
: Code highlightingset number
: Line number
About this Post
This post is written by OwlllOvO, licensed under CC BY-NC 4.0.