grimoire

personal wiki
git clone git://git.pyratebeard.net/grimoire.git
Log | Files | Refs

vim.md (6570B)


      1 # vim
      2 
      3 ## copy contents to/from files
      4 to copy the contents into a file
      5   ```
      6   :r <filename>
      7   ```
      8 or
      9   ```
     10   :"qY # yank out of first file
     11   :"qP # put into second file
     12   ```
     13 or read range of lines
     14   ```
     15   :r! sed -n <n>,<m>p /path/to/file.md
     16   ```
     17 
     18 to copy the contents to a new file
     19   ```
     20   :<n>,<m> w <filename>
     21   ```
     22   where `<n>` and `<m>` are numbers or symbols that designate range of lines
     23   
     24 
     25 ## text wrap
     26 (wrap to column)
     27 wrap current line
     28 ```
     29 gqq
     30 ```
     31 
     32 wrap entire file
     33 ```
     34 gqG
     35 ```
     36 
     37 wrap paragraph using visual selection
     38 ```
     39 V}gq
     40 ```
     41 
     42 also use visual or visual block with `gq`
     43 
     44 ## spell
     45 [using_spellcheck][]
     46 check spelling
     47 ```
     48 Spell
     49 ```
     50 
     51 move to word
     52 ```
     53 ]s [s
     54 ```
     55 
     56 and see results
     57 ```
     58 z=
     59 ```
     60 
     61 turn off highlighting
     62 ```
     63 set nospell
     64 ```
     65 
     66 ## buffers
     67 - buffer [faq][]
     68 - using [vanilla vim][]
     69 - [buffers over tabs][]
     70 - open buffer
     71     ```
     72     :e <filename>
     73     :new
     74     ```
     75 - view buffers
     76     ```
     77     :ls
     78     ```
     79 - switch buffers
     80     ```
     81     :buffer <num>
     82     ```
     83 - unload buffer
     84     ```
     85     :bd <num>
     86     ```
     87 
     88 ## incrementing numbers
     89 post on [reddit][]
     90 
     91 select several lines containing '0' and type `g<C-a>`
     92 
     93 ## split
     94 - movement
     95     ```
     96     <Ctrl>-w [h,j,k,l]
     97     <Ctrl>-w w
     98     ```
     99 - rotate
    100     ```
    101     <Ctrl>-w r
    102     ```
    103 - orientate
    104     ```
    105     <Ctrl>-w J
    106     <Ctrl>-w L
    107     ```
    108 - sizing
    109 	```
    110 	<Ctrl>-w -		# decrease height by 1 line
    111 	<Ctrl>-w +		# increase height by 1 line
    112 	<Ctrl>-w <		# change width by 1 line to the left
    113 	<Ctrl>-w >		# change width by 1 line to the right
    114 	<Ctrl>-w 10 -	# decrease height by 10 lines, etc...
    115 	```
    116 
    117 ## diff
    118 ```
    119 :windo diffthis
    120 :windo diffoff
    121 ```
    122 
    123 ## list recent doc
    124 ```
    125 :ol[dfiles]
    126 ```
    127 
    128 ## replace
    129 - change inner word
    130     ```
    131     ciw
    132     ```
    133 
    134 ## delete
    135 - until but not <char>
    136     ```
    137     dt <char>
    138     ```
    139 - until and <char>
    140     ```
    141     df <char>
    142     ```
    143 	
    144 ## visual
    145 - visual select to end of line excluding whitespace
    146 ```
    147 vg_
    148 ```
    149 
    150 ## using the shell
    151 [vim and the shell][]
    152 
    153 - run current buffer as script
    154 ```bash
    155 :!./%
    156 ```
    157 
    158 - in script, highlight line and switch to command mode by pressing `:`
    159 ```bash
    160 :'<,'>w !bash -
    161 ```
    162 
    163 ```python
    164 :'<,'>w !python -
    165 ```
    166 
    167 ##  directory of current buffer
    168 open new file in dir of current buffer
    169 ```
    170 :e %:h/filename
    171 ```
    172 
    173 save file as new file in dir of current buffer
    174 ```
    175 :sav %:h/filename
    176 ```
    177 
    178 ## substitute on multiple lines
    179 [stackoverflow 19996145][]
    180 
    181 ```
    182 :6,10s/<search_string>/<replace_string>/g | 14,18&&
    183 ```
    184 
    185 ## reverse substitute
    186 * adds a tab to the beginning of all lines that don't start with `Pattern`
    187 ```
    188 :%v/^Pattern/s/^/\t/
    189 ```
    190 
    191 ## write regex match lines to new file
    192 ```
    193 :g/^Pattern/ .w >> <filename>
    194 ```
    195 * use `.w!` if file doesn't exist
    196 
    197 ## sort visual block
    198 [vim fandom][]
    199 ```
    200 :'<,'>sort /\ze\%V/
    201 ```
    202 
    203 ## move current line (scroll-cursor)
    204 `z<enter>` or `zt` - move to top of buffer
    205 `z.` or `zz` - move to centre of buffer
    206 `z-` or `zb` - move to bottom of buffer
    207 
    208 (zEnter, z., and z- puts the cursor in the first non blank column. zt, zz, and zb leaves the cursor in the current column)
    209 
    210 ```
    211 :help scroll-cursor
    212 ```
    213 
    214 ## open a terminal
    215 ```
    216 :terminal
    217 :vert term
    218 ```
    219 
    220 `ctrl-w shift-n` puts terminal in normal mode
    221 `a` or `i` returns to shell
    222 
    223 ## yanking into a register
    224 `"qp` paste the contents of the register to the current cursor position
    225 `i` enter insert mode at the begging of the pasted line
    226 `^` add the missing motion to return to the front of the line
    227 `<escape>` return to visual mode
    228 `"qyy` yank this new modified macro back into the q register
    229 `dd` delete the pasted register from the file your editing
    230 
    231 ## editing the register visually
    232 `:let @q='` open the q register
    233 `<cntl-r><cntl-r>q` paste the contents of the q register into the buffer
    234 `^` add the missing motion to return to the front of the line
    235 `'` add a closing quote
    236 `<enter>` finish editing the macro
    237 
    238 ## insert full path of current file
    239 ```
    240 "%p
    241 :put=expand('%:p')
    242 ```
    243 
    244 ## netrw
    245 * [create new file] - `%`
    246 
    247 ## help
    248 * jump to topic - `Ctrl-]`
    249 * return - `Ctrl-o`
    250 
    251 ## folding
    252 | key  | desc                                                      |
    253 | ---  | ---                                                       |
    254 | zf#j | creates a fold from the cursor down # lines               |
    255 | zf/  | string creates a fold from the cursor to string           |
    256 | zj   | moves the cursor to the next fold                         |
    257 | zk   | moves the cursor to the previous fold                     |
    258 | za   | toggle a fold at the cursor                               |
    259 | zo   | opens a fold at the cursor                                |
    260 | zO   | opens all folds at the cursor                             |
    261 | zc   | closes a fold under cursor                                |
    262 | zm   | increases the foldlevel by one                            |
    263 | zM   | closes all open folds                                     |
    264 | zr   | decreases the foldlevel by one                            |
    265 | zR   | decreases the foldlevel to zero -- all folds will be open |
    266 | zd   | deletes the fold at the cursor                            |
    267 | zE   | deletes all folds                                         |
    268 | [z   | move to start of open fold                                |
    269 | ]z   | move to end of open fold                                  |
    270 
    271 ## documentation navigation
    272 | binding | desc                     |
    273 | ---     | ---                      |
    274 | <C-]>   | jump to corresonding tag |
    275 | <C-t>   | go back                  |
    276 
    277 ## disable mouse
    278 ```
    279 set mouse=
    280 set ttymouse=
    281 ```
    282 
    283 ## signify plugin
    284 | binding | desc                     |
    285 | ---     | ---                      |
    286 | c]      | next hunk                |
    287 | [c      | previous hunk            |
    288 
    289 * fold to hunks in new tab (add `!` to fold in current buffer)
    290 ```
    291 :SignifyFold
    292 ```
    293 * diff hunks in new tab (add `!` to diff in current buffer)
    294 ```
    295 :SignifyDiff
    296 ```
    297 
    298 ## tabs or spaces
    299 * change all tabs to spaces
    300 ```
    301 :set expandtab
    302 :retab
    303 ```
    304 
    305 [using_spellcheck]: https://www.linux.com/learn/using-spell-checking-vim
    306 [faq]: http://vim.wikia.com/wiki/Vim_buffer_FAQ
    307 [vanilla vim]: https://stackoverflow.com/questions/16082991/vim-switching-between-files-rapidly-using-vanilla-vim-no-plugins
    308 [buffers over tabs]: https://stackoverflow.com/questions/26708822/why-do-vim-experts-prefer-buffers-over-tabs
    309 [reddit]: https://www.reddit.com/r/vim/comments/a1lvb1/til_gca_for_creating_a_column_of_incrementing/
    310 [vim and the shell]: https://vimways.org/2019/vim-and-the-shell/
    311 [stackoverflow 19996145]: https://stackoverflow.com/a/19996145
    312 [vim fandom]: https://vim.fandom.com/wiki/How_to_sort_using_visual_blocks
    313 [create new file]: https://vimtricks.com/p/creating-files-and-directories/