--- layout: default title: Vim notes categories: software --- #### Symbols and meanings - `%` → current file. An example: `:so %` → Source the current file - `$` → end of line - `.` → Current line An example: `:.!sh` → Pipe current line to `sh` and 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 - `h` `j` `k` `l` → left, down, up, right - `*` → Next whole word under cursor (previous `#`) - `e` → Forward to the end of word. `E` can contain punctuation - `w` → Move forward to the beginning of a word. `W` Same as `w`, but special characters are treated as part of a word. - `b` → Works as `w`, but backwards - `{`,`}` → Jump by paragraphs - `(`,`)` → Jump by sentences - `G` → Jump to the end of the file - `1G` → Jump to the beginning of the file (same as `gg`) - `50G` → Jump to line 50 - `0` → Beginning of line - `_` or `^` → first non-blank character of the line - `g_` → last non-blank character of the line - `fX` → next character `X`. `FX` previous. `;` repeat , `,` repeat in reverse - `tX` → tili next `X` (similar to above, but the cursor is before `X`) - `H` → Jump to the top of the screen - `M` → Jump to the middle of the screen - `L` → Jump to the bottom of the screen #### Scrolling * `10 ` or `10` → Move 10 pages up * `5 ` or `5` → Move 5 pages down. - `zz` → scroll the line with the cursor to the center of the screen - `zt` → to the top - `zb` → to the bottom ### Terminal buffers - `:te[rm[inal]] command` - `:b#` switch buffer - `:ls` list buffers - `:buff 1` or `:b1` switch to buffer 1 ### List of the commands Common meaning of letters in the commands - `w` → word - `i` → 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 ```vim :set statusline=%<%f%h%m%r%=%b\ 0x%B\ \ %l,%c%V\ %P ``` #### Visual Mode - `V` → Selection by lines - `v` → Selection follows the cursor - `Ctrl+v` → Block selection - When selected block you can applay changes to each line by typing `I` editing and finally pressing `Esc` #### Enter Insert Mode - `I` → At the first non white character of the line - `i` → On the left of the cursor - `A` → At the very end of the line - `a` → On the right of the cursor - `c` → Delete selection and enter insert mode - `o` → Create new line below and enter insert mode - `O` → Create new line above and enter insert mode #### Split the editor - `:sp ` → Vertically - `:vs ` → Horizontally - `:set splitbelow`, `:set splitright` #### Markers - `:marks` → list of marks - `ma` → set current position for mark `a` - `` `a`` → jump to the cursor position of mark `a` - `'a` → jump to the beginning of a line of a mark `a` - ``y`a`` → yank text to position of mark `a` * ``` `` ``` → Return to the cursor position before the latest jump * `` `.`` → Jump to the last changed line. ### Recording macros 1. `qa` → Start recording macro under letter `a` 2. `q` → Stop recording 3. `@a` → Play the macro saved under letter a 4. `@@` → 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 and `n` next `N` previous match - `?pattern` → search backward for pattern ### Registers - `:reg` → print all registers - `"ap` → paste the register `a` , if the macro is recorded then it will paste it - `"xy` → yank into register `x` - `:let @a = "kkll"` → set a macro from the command mode - `:let @A='i'` → append to register `a` - `:%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 ```vim function! CalculateAge() normal 03wdei^R=2012-^R"^M^[0j endfunction ``` Key banding to function ```vim nnoremap a :call CalculateAge() ``` Preloading vim with macros like ```vim 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` ```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 relativenumber` - `set encoding=utf-8` - `syntax on` - `map za :FZF` → fuzzy finder over `za` Indentation setup - `set tabstop=2 shiftwidth=2 expandtab` - `filetype plugin indent on` ```vim let g:syntastic_swift_checkers = ['swiftlint', 'swiftpm'] lua << EOF local lspconfig = require('lspconfig') lspconfig.sourcekit.setup{} EOF ``` #### Rename current file ```vim 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 n :call RenameFile() ```