VIM
Basic actions
To permanently apply the following, you can put them in the ~/.vimrc
file in your home directory.
To display line numbers:
:set number
To remove line numbers:
:set nonumber
Save the file:
:w
Exit the file:
:q
Comment from line 1 to 8:
:1,8s/^/#
Uncomment from line 1 to 8:
:1,8s/^#/
Clipboard
Cut:
dd
Copy:
yy
Paste:
p
Paste before:
P
Editing
Append:
:a
Insert:
:i
Undo changes:
:u
Redo changes:
Ctrl + r
Search:
/$STRING_TO_SEARCH_FOR
Search a specific string:
/\<$STRING_TO_SEARCH_FOR\>
Search ignoring the case:
/$STRING_TO_SEARCH_FOR\c
Search and replace
:s/$STRING_TO_SEARCH_FOR/$STRING_TO_REPLACE_WITH/
Search and replace an entire file
:%s/$STRING_TO_SEARCH_FOR/$STRING_TO_REPLACE_WITH/
Navigation
Go to specific line number:
:$LINE_NUMBER
Got to the first line:
gg
Got to the last line:
G
Go page up:
Ctrl + u
Go page down:
Ctrl + d
Go to the end of the current word:
e
Go to the beginning of the next word:
w
Go to the beginning of the previous word:
b
Go to the start of the line after whitespaces:
^
Go to the end of the line:
$
Navigate blocks of code:
# Move one code block up
Shift-{
# Move one code block down
Shift-}
Navigate blocks of code:
# Move one code block up
Shift-{
# Move one code block down
Shift-}
Miscellaneous
Set password protection to a file:
vim -x $PATH_TO_THE_FILE
Vim plugins
vim-plug
vim-plug for Vim available here. Installed using the ~/.vimrc
file provided in the overal basic setup section.
vim-rainbow
vim-rainbow for Vim available here.
NERDTree
NERDTree for Vim available here.
Usage
Activate NERDTree:
:NERDTree
Close NERDTree:
:NERDTreeClose
Toggle NERDTree:
:NERDTreeToggle
Or add this line to your .vimrc
file to to toggle when you hit F2
key:
map <F2> :NERDTreeToggle<CR>
Keyboard shortcuts:
t
: Open the selected file in a new tabi
: Open the selected file in a horizontal split windows
: Open the selected file in a vertical split windowI
: Toggle hidden filesR
: Refresh the tree, useful if files change outside of VimCtrl + w w
: Cycle though all windowsCtrl + w h
: Takes you left a windowCtrl + w j
: Takes you down a windowCtrl + w k
: Takes you up a windowCtrl + w l
: Takes you right a window
Overal basic setup
- Create a
~/.vimrc
file:
" Install vim-plug if not found
if empty(glob('~/.vim/autoload/plug.vim'))
silent !curl -fLo ~/.vim/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
endif
" Run PlugInstall if there are missing plugins
autocmd VimEnter * if len(filter(values(g:plugs),'!isdirectory(v:val.dir)'))
\| PlugInstall --sync | source $MYVIMRC
\| endif
call plug#begin()
" The default plugin directory will be as follows:
" - Vim (Linux/macOS): '~/.vim/plugged'
" Make sure you use single quotes
Plug 'junegunn/vim-plug'
Plug 'https://github.com/frazrepo/vim-rainbow'
Plug 'https://github.com/scrooloose/nerdtree'
Plug 'https://github.com/vim-airline/vim-airline'
" Initialize plugin system
call plug#end()
set number
syntax enable
set laststatus=2
colorscheme delek
let g:rainbow_active = 1
map <F4> :RainbowToggle<CR>
map <F2> :NERDTreeToggle<CR>