There are multiple modes in vim, normal mode is loaded by default.
Command mode. Press
: from normal mode, This mode is used when escape is pressed.
Insert mode. Press
i to enter insert mode, from here typing is allowed within the file.
Visual mode. Press
v to enter visual mode from here you can select and move lines.
Visual line. Press capital
V to enter visual line mode, this will select entire lines.
By default command mode will be loaded to run command type
: followed by the command
in terminal type
vim to enter vim or
vi file/path to create a file or edit a file if the path already exists.
Edit File (must be inside vim for this to work)
:e file/path - open a file from a vim instance
Save and write to file
:q = quit
:qw = quit and write to the file
:q! = quit and discard any changes
:w = write to the file aka save
from normal mode pressing a will turn on edit mode and place the cursor 1 character forward. The difference between pressing
i will leave the curse in the same position whilst
a will move the cursor forward a character.
Move around by pressing these keys:
h = move left.
j = move down.
k = move up.
l = move right.
gg = move to the top of the file.
shift + g move to the bottom of the file.
zz = move the current line to the center of the screen.
ctrl+^ to toggle between your current and file same as
Selecting text with visual mode.
To select text from normal mode press
v to enter visual mode. Now as your cursor moves it will select the letters. To select lines press escape to go back to normal mode and press
shift + v to these visual lines.
From visual mode:
d = delete the selected text but won't be removed until your next action.
dd = delete and remove the line directly.
From visual or normal mode pressing
d will delete anything selected.
d will delete.
dd will delete and remove directly.
u will undo.
/ to enter the search command followed by what you're searching for.
n will go to the next occurrence.
shift+n will go to the previous occurrence.
Copying and pasting
Enter visual mode select letters and press
y to yank (alias of copy)
to copy an entire line press
:! followed by a command will be run in the terminal ie
:! /vendor/bin/phpunit will run phpunit on the terminal.
Customize Vim by editing a
.vimrc file, this is a config file for Vim by default it will be empty to create this file and open in Vim in one go type
vi ~/.vimrc this will open
.vimrc from the home directory.
When any changes have been made the file should be reloaded into memory this can be done by sourcing the file in the terminal:
This will source the current file.
To add comments place them inside double quotes
Turn on syntax highlighting:
Set the colour scheme
Enable backspace to delete letters from indents, the end of lines, and the start.
Turn on line number.
Turn on line numbers above and below the current line and set the line number for the current line.
Set the leader key, this is the key used for most shortcuts, by default the leader key is set to backspace, let's use
let mapLeader = ','
Adding a shortcut for open .vimrc while in another file:
nmap <Leader>ev :tabedit $MYVIMRC<cr>
(This as comma edit vim)
n for normal mode map the shortcut
,ev then open an edit tab
:tabedit use the location of the .zimrc file
$MYVIMRC and press enter <cr>
When run from another file a new tab (buffer) will be opened, to close this tab (buffer) type:
Read this as buffer delete. This will then delete the buffer.
Instead, you can close the current tab with this command:
Set config to auto source on save
autocmd BufWritePost .vimrc source %
This will source the .vimrc file upon saving
This will run upon every save, but this can cause Vim to freeze, to stop it create an auto group and place the commands inside the group. Placing
autocmd! inside the group ensures it's essentially reset on each call.
augroup autisourcing autocmd! autocmd BufWritePost .vimrc source % augroup END
Great discussions and other resources: