Carlos Gustavo Ruiz

Back to Notebook

VIM

set file language for highlighting

  • :setf language
  • or :set filetype=language (executing :set filetype will show current language used for syntax highlighting.)

fold commands

zf#j creates a fold from the cursor down # lines.
zf/string creates a fold from the cursor to string .
zj moves the cursor to the next fold.
zk moves the cursor to the previous fold.
zo opens a fold at the cursor.
zO opens all folds at the cursor.
zc close a fold at the cursor.
zm increases the foldlevel by one.
zM closes all open folds.
zr decreases the foldlevel by one.
zR decreases the foldlevel to zero -- all folds will be open.
zd deletes the fold at the cursor.
zE deletes all folds.
[z move to start of open fold.
]z move to end of open fold.

config files

  • global vimrc: :echo $VIM
  • user vimrc :echo $MYVIMRC
  • use other file: in .vimrc add source /path/to/config/file

split and tabs

from command line:

from normal mode:

  • ctrl+w s -> split horizontally
  • ctrl+w v -> split vertically
  • ctrl+w q -> close split
  • ctrl+w ctrl+w -> switch to next window
  • ctrl+w j (moves to adjacent window, you can use hjkl for move direction)

switch splits orientation

  • from horizontally to vertically: ctrl+w t + ctrl+w H
  • from vertically to horizontally: ctrl+w t + ctrl+w K

source

switching case of characters

  • Toggle case "HellO" to "hELLo" with g~ then a movement.
  • Uppercase "HellO" to "HELLO" with gU then a movement.
  • Lowercase "HellO" to "hello" with gu then a movement.

source

clear last search highlighting

turn of highlighting until next search

:noh

or turn off highlighting completly:

set nohlsearch

or, toggle it:

set hlsearch!

nnoremap <F3> :set hlsearch!<CR>

source

format JSON

to change a JSON file into a human-readable format, just write:

:%!python3 -m json.tool

Using vim as diff tool

command vim -d <file1> <file2> or vimdiff <file1> <file2>.

vimdiff basic commands:

dp      diffput: puts changes under the cursor into the other file
                 making them identical (thus removing the diff).
do      diffget: (o => obtain). The change under the cursor is replaced
                 by the content of the other file making them identical.

]c      Jump to the next diff
[c      Jump to the previous diff

use the :diffthis command to initiate a diff when Vim is already running.

source

Reload changed content of file opened in Vim

Use the command :edit (or :e) command without specifying a file name to reload the current file. If you made changes to the file use :edit! (or :e!) to force the reload.

source

in addition to manual refreshing, you can add :set autoread in the ~/.vimrc file.