Vim
Everyone's favorite text editor, said no one ever.
Installation
You can install vim
using apt
:
You can use vim
to open a plain text editor, or can use vim <filename>
to open a file in vim
. If that file doesn't already exist, vim
will create it.
Unless you save the file, vim
will not create it.
Usage
Vim is a modal editor. Vim has three modes:
Normal mode - Used for navigating text.
Insert mode - Used for inserting text.
Visual mode - Used for selecting text.
These are not all the vim
commands, but these are the ones I use the most. I use the Vim Cheatsheet as a common reference for vim
commands.
Movement
Movement is done in normal mode. There are lots of ways to move around in vim
.
Here is the standard way to move around in vim
:
h
- Move leftj
- Move downk
- Move upl
- Move right
You can also use the arrow keys to move around in vim
.
You can also jump by words:
w
- Jump to the start of the next wordW
- Jump to the start of the next word (ignoring punctuation)e
- Jump to the end of the next wordE
- Jump to the end of the next word (ignoring punctuation)b
- Jump to the start of the previous wordB
- Jump to the start of the previous word (ignoring punctuation)ge
- Jump to the end of the previous wordgE
- Jump to the end of the previous word (ignoring punctuation)
You can also jump across the entire file:
gg
- Jump to the start of the fileG
- Jump to the end of the file5gg
- Jump to line 5
You can jump by screen:
H
- Jump to the top of the screenM
- Jump to the middle of the screenL
- Jump to the bottom of the screenCtrl + u
- Jump up half a screenCtrl + d
- Jump down half a screenCtrl + b
- Jump up a screenCtrl + f
- Jump down a screen
You can jump by line:
0
- Jump to the start of the line^
- Jump to the first non-whitespace character of the line$
- Jump to the end of the line
You can jump by paragraph:
{
- Jump to the start of the previous paragraph}
- Jump to the start of the next paragraph
Editing
Editing is done in normal mode and insert mode. Use i
to enter insert mode. Use Esc
to exit insert mode.
You can delete text:
x
- Delete the character under the cursorX
- Delete the character before the cursordw
- Delete the word under the cursord$
- Delete from the cursor to the end of the linedd
- Delete (cut) the current line
You can copy and paste text:
yy
- (Yank) Copy the current linedd
- Cut the current linep
- Paste the copied text after the cursorP
- Paste the copied text before the cursor
You can undo and redo changes:
u
- Undo the last changeCtrl + r
- Redo the last change
You can replace text:
r
- Replace the character under the cursorR
- Replace characters untilEsc
is pressedcw
- Replace the word under the cursorc$
- Replace from the cursor to the end of the linecc
- Replace the current line
You can indent and unindent text:
>>
- Indent the current line<<
- Unindent the current line
You can search for text:
/
- Search forward?
- Search backwardn
- Go to the next search resultN
- Go to the previous search result
Visual Mode
Visual mode is used for selecting text. Use v
to enter visual mode. Use Esc
to exit visual mode.
You can select text:
v
- Select the character under the cursorV
- Select the current lineCtrl + v
- Select a block of text
You can indent and unindent text:
>
- Indent the selected text<
- Unindent the selected text
You can copy and paste text:
y
- Copy the selected textd
- Cut the selected textp
- Paste the copied text after the cursorP
- Paste the copied text before the cursor
Saving and Quitting
Saving and quitting are done in normal mode.
You can save and quit:
:w
- Save the file:q
- Quit the file:wq
- Save and quit the file
You can force save and quit:
:w!
- Force save the file:q!
- Force quit the file:wq!
- Force save and quit the file
You can save and quit all files:
:wa
- Save all files:qa
- Quit all files:wqa
- Save and quit all files
Other
You can use vim
to run commands:
:!
- Run a shell command:r
- Read a file into the current file:e
- Edit a file:tabe
- Edit a file in a new tab
You can use vim
to run macros:
q
- Start recording a macroq
- Stop recording a macro@
- Run a macro
Last updated