Clear Last Highlighting Until Next Search
:noh
or turn off highlighting completly:
:set nohlsearch
or, toggle it:
:set hlsearch!
" to map this in a .vimrc file we can write
nnoremap <F3> :set hlsearch!<CR>
Config Files
- global vimrc:
:echo $VIM
- user vimrc
:echo $MYVIMRC
- use other file: in .vimrc add
source /path/to/config/file
Ctrlp Wildignore
set wildignore+=*/tmp/*,*.so,*.swp,*.zip,*/node_modules/*
- ctrlp.vim
Disable folding
:set nofoldenable
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.
Format JSON File
To change a JSON file into a human-readable format, just write:
:%!python3 -m json.tool
We can add it to the .vimrc
file as a command:
command FormatJSON %!python3 -m json.tool
Explained here. Using it as a bash oneliner here.
Highlight Search Pattern Matches
To search, in normal
mode type: /pattern
To remove highlighting, in normal
mode type: :noh
(an abbreviation for :nohlsearch
)
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.
in addition to manual refreshing, you can add :set autoread
in the ~/.vimrc
file
Scroll up/down, keeping your cursor in its row
One line
Ctrl+Y
-> Move viewport downCtrl+E
-> Move viewport up (Extra lines)
Half a screen
Ctrl+U
-> Move viewport UpCtrl+D
-> Move viewport Down
Move a number of lines
z+
-> move viewport right to the top of the window.z<numner>
-> move viewport N lines from the top of the window.
Set file language for highlighting
:setf language
- or
:set filetype=language
(executing:set filetype
will show current language used for syntax highlighting.)
Sort lines of text
Press ‘v’ and then select the text you want, then press:
which will let you
enter a command. It should have already filled out some characters (so you have
:'<,'>
in the command line area), son just type sort
after it.
lorem
ipsum
dolor
sit
amet
consectetur
adipiscing
elit
quisque
a
:<',>'sort
converts to:
a
adipiscing
amet
consectetur
dolor
elit
ipsum
lorem
quisque
sit
Notes:
- pressing
ggvG
will select al lines in the file- use
:%sort!
instead of:sort
for reverse sort- use
:%sort u
to remove duplicate lines- use
:sort n
to do numeric sort.
Split and Tabs
from command line:
$ vim -p[N] [files] # create N tab pages (default: one for each file)
$ vim -o[N] [files] # create N horizontal splits for files. by default is one split for each file.
$ vim -O[N] [files] # same as above but vertically.
from normal mode:
ctrl+w
s
-> split horizontallyctrl+w
v
-> split verticallyctrl+w
q
-> close splitctrl+w
ctrl+w
-> switch to next windowctrl+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
Switching case of characters
- Toggle case
"HellO"
to"hELLo"
withg~
then a movement. - Uppercase
"HellO"
to"HELLO"
withgU
then a movement. - Lowercase
"HellO"
to"hello"
withgu
then a movement.
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.
Save file as UTF-8
:set fileencoding=utf8
:w filename
Remove unwanted spaces
:%s/\s\+$//e