A curated list of those Vim How-Tos we search frequently in our daily codingWritten on March 20th, 2018 by @10000TB
Vim has been my editor for day to day work for a while, and often times I find myself questing same vim How-To on stackoverflow and websites as such. To help manage a list of my frequent used Vim tips/commands, and potentially be useful to you, I am curating this list of vim How-Tos.
visual selection in vim ?
Ctl-v- visual block.
v- select chars.
V- select current line.
Shortcuts to move cursor in vim ?
gg- to move to start of file.
G- to move to end of file.
''- to move to last active point.
0- to move to start of current line.
$- to move to end of current line.
- How to run command within vim ?
%represent that we pass in whole buffer of vim into command for filtering. We can also specify a range instead of passing the whole buffer. For example:
:1,100![cmd]A list of examples of running commands within vim:
# Do word count. :%!wc #
For buffer to be used, when a visual selection is active, that will automatically be used as buffer input.
%!: pass current buffer.
4,!: line 4 through end of file.
- How to beautify ugly json within vim?
python -m json.tool:
:%!python -m json.tool
This trys to beautify the whole file.
If there is a need to only beautify a range of lines, use following instead:
:[start],[end]!python -m json.tool
To make it even more convenient for your daily work,
nmapthis command to some short cut.
Following nmap maps the first command to
nmap =j :%!python -m json.tool<CR>
How to search within visually selected text in vim ?
For many reasons/occasions, we need to search within a scope, for example within visually selected text in vim. A simple way:
- Visually select text. (With v, V or Control-V)
- Press ‘:’ (colon)
Now you should see prompt as
- Following that, type in seach or seach replace command, then the search/search and replace will only apply within the selected scope
Say you have a list in python and you forgot to quote items, and you can take advantage of this vim tip instead of adding quotes for each of them mannually.
How to cancel search hilight ?
There are times when you have unwanted search highlight, and you simplely want to cancel the highlight.
:nohcommand exactly do the trick:
- Simply type
:(colon) and follow with
- Simply type