VIM Modes
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
and a
is i
will leave the curse in the same position whilst a
will move the cursor forward a character.
Movement:
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 :bp
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.
Deleting
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.
Pressing d
will delete.
Pressing dd
will delete and remove directly.
Pressing u
will undo.
Searching
type /
to enter the search command followed by what you're searching for.
Pressing n
will go to the next occurrence.
Pressing shift+n
will go to the previous occurrence.
Copying and pasting
Enter visual mode select letters and press y
to yank (alias of copy)
paste with p
to copy an entire line press yy
Access Terminal
:!
followed by a command will be run in the terminal ie :! /vendor/bin/phpunit
will run phpunit on the terminal.
VIM Config
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:
:so %
This will source the current file.
To add comments place them inside double quotes
Turn on syntax highlighting:
syntax enable
Set the colour scheme
colorscheme desert
Enable backspace to delete letters from indents, the end of lines, and the start.
set backspace=indent,eol,start
Turn on line number.
set number
Turn on line numbers above and below the current line and set the line number for the current line.
set relativenumber
Set the leader key, this is the key used for most shortcuts, by default the leader key is set to backspace, let's use ,
instead.
let mapLeader = ','
Adding a shortcut for open .vimrc while in another file:
nmap <Leader>ev :tabedit $MYVIMRC<cr>
(This as comma edit vim)
this means 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
When run from another file a new tab (buffer) will be opened, to close this tab (buffer) type:
:bd
Read this as buffer delete. This will then delete the buffer.
Instead, you can close the current tab with this command:
:tabc
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:
https://www.reddit.com/r/vim/comments/qe8i1y/switch_from_vscode_to_vim/
https://www.youtube.com/watch?v=gnupOrSEikQ
https://www.youtube.com/watch?v=CcgO_CV3iDo&list=PLu-ydI-PCl0OEG0ZEqLRRuCrMJGAAI0tW&index=1
https://www.freecodecamp.org/news/a-guide-to-modern-web-development-with-neo-vim-333f7efbf8e2/
https://blog.inkdrop.app/how-to-set-up-neovim-0-5-modern-plugins-lsp-treesitter-etc-542c3d9c9887
https://www.reddit.com/r/vim/comments/u7amd6/can_i_use_vim_or_neovim_through_ssh_like_i_would/
Top comments (0)