Vim notes
Symbols and meanings
%→ current file. An example::so %→ Source the current file$→ end of line.→ Current line An example::.!sh→ Pipe current line toshand replace it with the output
Entering !! in normal mode is translated to :.! I. e. Typing !!date in normal mode replaces current line with the date.
Tips
:e[dit]→ Edit the current file. This is useful to re-edit the current file, when it has been changed outside of Vim.:e!Force reload file:help index→ Get all default mappings
Navigation
hjkl→ left, down, up, right*→ Next whole word under cursor (previous#)e→ Forward to the end of word.Ecan contain punctuationw→ Move forward to the beginning of a word.WSame asw, but special characters are treated as part of a word.b→ Works asw, but backwards{,}→ Jump by paragraphs(,)→ Jump by sentencesG→ Jump to the end of the file1G→ Jump to the beginning of the file (same asgg)50G→ Jump to line 500→ Beginning of line_or^→ first non-blank character of the lineg_→ last non-blank character of the linefX→ next characterX.FXprevious.;repeat ,,repeat in reversetX→ tili nextX(similar to above, but the cursor is beforeX)H→ Jump to the top of the screenM→ Jump to the middle of the screenL→ Jump to the bottom of the screen
Scrolling
10 <PageUp>or10<CTRL-B>→ Move 10 pages up5 <PageDown>or5<CTRL-F>→ Move 5 pages down.
zz→ scroll the line with the cursor to the center of the screenzt→ to the topzb→ to the bottom
Terminal buffers
:te[rm[inal]] command:b#switch buffer:lslist buffers:buff 1or:b1switch to buffer 1
List of the commands
Common meaning of letters in the commands
w→ wordi→ inner
| Command | |
|---|---|
dd |
Delete one line |
d |
Delete selection |
x |
Delete character under cursor |
d+ |
Delete 2 lines |
:%d or :1,$d |
Delete the whole of the file |
dw, diw |
Delete what that the cursor is over |
di( |
Delete inner brackets. da( → including brackets |
:r[ead] !date |
Execute commend and put content into editor |
. |
Repeat the last operation |
gU |
Uppercase the selection, gu → lower |
% |
Jump to matching bracket { } [ ] ( ) |
:%!column -t |
Put text in columns |
:%!sort |
Sort the whole file |
:'<,'>!grep text |
Keep lines that contains text |
:'<,'>!sort |
Sort selected lines |
:eariler 1m |
State from the 1 min before |
ga |
Display hex, ascii value of character under cursor |
g8 |
Display hex value of utf-8 character under cursor |
ciw |
Change inner word |
yiw |
Yank inner word |
viwp |
Select word and then replace it with previously yanked text |
rX |
replace every character in selection or under cursor with X |
guiw |
Lower case word |
guu |
Lowercase line |
gUU |
Uppercase line |
= |
Indent the selection |
=% |
Indent the current braces |
G=gg |
indent entire document |
ZZ |
Write current file, if modified, and exit (same as :wq) |
ZQ |
Quit current file and exit (same as :q!) |
:so % |
Source current file |
@: |
Execute last command again |
Status line
:set statusline=%<%f%h%m%r%=%b\ 0x%B\ \ %l,%c%V\ %P
Visual Mode
V→ Selection by linesv→ Selection follows the cursorCtrl+v→ Block selection- When selected block you can applay changes to each line by typing
Iediting and finally pressingEsc
- When selected block you can applay changes to each line by typing
Enter Insert Mode
I→ At the first non white character of the linei→ On the left of the cursorA→ At the very end of the linea→ On the right of the cursorc→ Delete selection and enter insert modeo→ Create new line below and enter insert modeO→ Create new line above and enter insert mode
Split the editor
:sp <filename>→ Vertically:vs <filename>→ Horizontally:set splitbelow,:set splitright
Markers
:marks→ list of marksma→ set current position for marka`a→ jump to the cursor position of marka'a→ jump to the beginning of a line of a markay`a→ yank text to position of marka
``→ Return to the cursor position before the latest jump`.→ Jump to the last changed line.
Recording macros
qa→ Start recording macro under letteraq→ Stop recording@a→ Play the macro saved under letter a@@→ Play the last macro
Searching
:%s/Plug.*$//→ Search and delete all lines that starts from Plug:%s/foo/bar/gc→ Replace all occurrence of foo by bar with confirmation'<,'>:s/find/replacewith/Replace selection/pattern→ search for pattern then enter andnnextNprevious match?pattern→ search backward for pattern
Registers
:reg→ print all registers"ap→ paste the registera, if the macro is recorded then it will paste it"xy→ yank into registerx:let @a = "kkll"→ set a macro from the command mode:let @A='i'→ append to registera:%normal @a→ execute the macro on all lines of the current file:'<,'>normal @a→ execute the macro on a visually selected lines:10,20 normal @a→ execute the macro for lines from 10 to 20:g/pattern/ normal @a→ Search for pattern and execute macro for it
Functions - An example
Function definition
function! CalculateAge()
normal 03wdei^R=2012-^R"^M^[0j
endfunction
Key banding to function
nnoremap <leader>a :call CalculateAge()<CR>
Preloading vim with macros like
let @a='03wdei^R=2012-^R"^M^[0j'
Call function from the command mode
:call CalculateAge()
Configuration
The config file is located at .config/nvim/init.vim
if empty(glob('~/.local/share/nvim/site/autoload/plug.vim'))
silent !curl -fLo ~/.local/share/nvim/site/autoload/plug.vim --create-dirs
\ https://raw.githubusercontent.com/junegunn/vim-plug/master/plug.vim
autocmd VimEnter * PlugInstall --sync | source $MYVIMRC
endif
call plug#begin('~/.vim/plugged')
Plug 'junegunn/fzf'
Plug 'neovim/nvim-lspconfig'
Plug 'neoclide/coc.nvim', {'branch': 'release'}
Plug 'vim-airline/vim-airline'
Plug 'vim-airline/vim-airline-themes'
Plug 'vim-syntastic/syntastic'
Plug 'tokorom/syntastic-swiftlint.vim'
call plug#end()
set relativenumberset encoding=utf-8syntax onmap za :FZF<CR>→ fuzzy finder overza
Indentation setup
set tabstop=2 shiftwidth=2 expandtabfiletype plugin indent on
let g:syntastic_swift_checkers = ['swiftlint', 'swiftpm']
lua << EOF
local lspconfig = require('lspconfig')
lspconfig.sourcekit.setup{}
EOF
Rename current file
function! RenameFile()
let old_name = expand('%')
let new_name = input('New file name: ', expand('%'), 'file')
if new_name != '' && new_name != old_name
exec ':saveas ' . new_name
exec ':silent !rm ' . old_name
exec ':bd ' . old_file
redraw!
endif
endfunction
map <leader>n :call RenameFile()<cr>