Vim/NeoVim
- vimawesome: look for plugins and configurations
- vimscript.pdf source
- VIM as Perfect IDE
- Learn Vimscript the hard way
Notes: - Tabs contain windows - From a tab to another the disposition of the windows differs
Plugins
vim-surround
cs{[ " to replace the surround { and } by [ and ]
S{ " to surround the visual selection with {
ds{ " to remove delimiters {}
NERDTree
m " menu, then 'c' to create a file
R " refresh the menu (in case you create/remove files/directories)
tN " to open the file under cursor in a new tab
O " open recursively (unfold) the nodes
? " open the help with all shortcuts
Markdown
za " open fold where the cursor is
zR " open all folds
Folding
zo " open current fold (one level)
zc " close fold (one level)
zO " open current
zC " close fold
zR " open all folds
zM " close all folds
zj " jump next fold
zk " jump previous fold
CtrlP
F5 " refresh cache file
:CtrlPClearCache " same as above
ALE
Useful commands:
:ALEPopulateQuickfix " show lint errors
:ALEGoToDefinition " go to definition, use Ctrl+O to come back on previous
:ALEFindReferences " find all references of code
:ALESymbolSearch pattern
Fugitive
:Git blame " Who wrote the line ?
Git-gutter
]c " Jump to next git diff
[c " Jump to previous git diff
\hu " revert the changes of the current hunk
Vimrc
Reload file when changing
set autoread
Note: you can refresh file by using :e!
Reload all buffers at once
:bufdo e
Show lines
set number
Syntax color
syntax on
Note: seems to be obsolete and enable by default.
Indent
Indent from 4 columns:
set shiftwidth=4
Smart indent:
set smartindent
Keep autoindent on current line
set autoindent
Indentation before saving
" use 4 spaces for tabs
set tabstop=4 softtabstop=4 shiftwidth=4
" display indentation guides
set list listchars=tab:❘-,trail:·,extends:»,precedes:«,nbsp:×
" convert spaces to tabs when reading file
autocmd! bufreadpost * set noexpandtab | retab! 4
" convert tabs to spaces before writing file
autocmd! bufwritepre * set expandtab | retab! 4
" convert spaces to tabs after writing file (to show guides again)
autocmd! bufwritepost * set noexpandtab | retab! 4
Note: seems to be obsolete and enable by default.
Copy/Past & Mouse use
Require gpm
packet.
set mouse=a
Commands
Basics
See all existing mapping with :help index
source
:e " edit
:w " save
:q " quit
:qa " quit all
i " insert before cursor
I " insert before line
a " append after cursor
A " append after line
:d " delete/cut current line
:d2 " delete 2 line from the current line
d " delete/cut current line
dw " delete current word from the cursor
daw " delete word around the cursor
dt) " delete until finding closing parenthesis
f) " find first parenthesis, can be used e.g. in visual mode
vip " select all in current block
^ " first non-blank char of line
$ " end of line
ci( " go at first parenthesis, remove all between, and insert
% " move first matching separator/bracket/condition
w " reach next word
W " similar and faster
:u[ndo] " or u
:red[o]
. " repeat last action
:! linux command
Insert notes: On multiple lines
" go on word through visual mode, or just with the cursor on the letter
<C-v> " block mode
j " to select next line
I " to insert, or d to delete
" press esc when done
Delete note: to avoid unexpected paste :set nopaste
Select all
:%y+
gg"+yG " other way
Copy/Paste
Normal Mode
d " cut the line
yy " to copy the first line, equivalent to Y
y2y " copy 2 lines
p " to paste from the current line
P " to paste after the copy
"3p " print the 3'rd history copy
"+y " to copy in clipboard, this require vim-gtk (http://vim.wikia.com/wiki/Accessing_the_system_clipboard)
Command-line mode
<C-r>"
Notes:
- "
is the latest register
- Other approach by using <C-f>
to open history and use normal command like p
- you can list old/history of copy with :reg
Replace a selection
Configure (vimrc):
let g:mc = "y/\\V\<C-r>=escape(@\", '/')\<CR>\<CR>"
vnoremap <expr> cn g:mc . "``cgn"
Usage: selector<C-v>
, type cn
, do your change, then n
to go next occurence, and .
to replay the change
Notes: Other techniques exists:
- vim-multiselect: (+) pure vim script, (-) unmaintained ?, (-) requires vim 8.0+
- vim-visual-multi: (+) features, (-) overkill, (-) not pure vim script
- reddit debates
Research and replace
/research sentence " type 'n' to go to the next occurence
:%s/x/z/ " replace first occurence 'x' by 'z'
:%s/x/z/g " replace all 'x' by 'z'
:%s/x/z/gc " replace all 'x' by 'z' with prompt to confirm
Note:
- Clean research last highlighting research
:noh
- You can research from current word by using
*
, type it again to find next occurence
Summary research
:vimgrep pattern %
:cwin
Head/Bottom of file
Head: gg
or 1G
Bottom: G
Jump Line/Col
7G5| " Jump line 7, col 5
:56 " Jump line 56
Visual Mode
V
for the whole line
v
from the cursor
Block Mode
<C-v>
Buffer
:ls " list files in the buffer
:bd " close current buffer
:%bd|e# " close all buffers except this one
Autocompletion (omni)
<C-n>
Tabs
z See : http://vim.wikia.com/wiki/Using_tab_pages
gt " go to next tab (or Ctrl+Up)
gT " go to previous tab (or Ctrl+Down)
{i}gt " go to tab in position i
:newtab " new tab
:tabedit <file> " new tab
:tabn <number> " change tab
:tabclose " close current tab (and all its windows)
:b <file> " switch current tab to opened <file> in tab, autocomplete's working to find <file> faster, b mean buffer
:tab sball " open each buffer in a tab
:tab split " put current window in full size (will open a new tab)
With the following mappings (which require gvim), you can press Ctrl-Left or Ctrl-Right to go to the previous or next tabs, and can press Alt-Left or Alt-Right to move the current tab to the left or right.
nnoremap <C-Left> :tabprevious<CR>
nnoremap <C-Right> :tabnext<CR>
nnoremap <silent> <A-Left> :execute 'silent! tabmove ' . (tabpagenr()-2)<CR>
nnoremap <silent> <A-Right> :execute 'silent! tabmove ' . (tabpagenr()+1)<CR>
Source: http://vim.wikia.com/wiki/Using_tab_pages
Open multiple files into tabs at once
:args *.c
:tab all
Windows
<C-w>n " new horizontal split (editing a new empty buffer)
<C-w>s " split window horizontally (editing current buffer)
<C-w>v " vsplit window vertically (editing current buffer)
<C-w>c " close window
<C-w>o " close all windows, leaving :only the current window open
<C-w>w " go to next window
<C-w>p " go to previous window
<C-w>x " reorder windows
<C-w>_ " maximize a window vertically
<C-w>| " maximize a window horizontally
<C-w>= " resize all windows to equal size
<C-w>T " move current windoz to a new tab
" switch from vertical split to horizontal and vice versa
<C-w>K " move current window to top (all remaining below)
<C-w>H " move current window to left (all remaining to right side)
<C-w><Up> " go to window above
<C-w><Down> " go to window below
<C-w><Left> " go to window on left
<C-w><Right> " go to window on right
:resize 50 " fix size on horizontal
:vertical resize +5 " incremental size on vertical
Tips
Know more about mappings
Official vim mapping: :help index
Custom mapping and from plugins: :help map
Know more about special key
e.g. <CR>
for carriage return
See :help key-notation
Check plugins are well configured
:checkhealth
Recording
# start recording
q<letter>
# end recording
q
# replay a record
@<letter> " or you can use .
Format an opened json file
Consider the following json content
[ {"name": "Homer", "budget": {"total": 20, "left": 0}}, {"name": "Marge", "budget": {"total": 30, "left": 30}} ]
You can use something like: :%!jq '.[0]'
, which will gives:
{
"name": "Homer",
"budget": {
"total": 20,
"left": 0
}
}
Dlete without copy (no cut)
yy # copy in register "0p
d # delete in default register ""
"0p # parse the copy register