This is the series 4 of my blog posts about some nifty techniques of using Nvim.
Check if a string contains a pattern?
There are two simple ways:
match() function, Vim assumes that
:h /magic) option is
set for patterns. If
pattern is found in
str, it will return the index
pattern starts. Otherwise, it will return -1.
You can also use regex match via
matches cases during matching, while
=~? ignores cases. During matching, it
is always assumed that
magic option is set.
When should I capitalize the function names?
For custom global functions, i.e., functions without the
local-function), the function name must start with an uppercase letter. But
for script local functions and auto-loaded functions (see
:h autoload), you
do not need to start the actual function name with uppercase letter.
How to represent
ALT key in mapping?
According to documentation (
<A-...> are the
same. Both can be used to refer to Alt key.
How to check the actual key press that Nvim receives when I press a key?
Sometimes, due to various reasons, when you press some key, what Nvim receives
is not that key press. To check the key that Nvim actually receives, press
Ctrl-V in insert mode and then press the key you want to check.
How to repeat some character N times？
If you are in normal mode, use
<NUM>a<Chars><ESC> to input
How do I execute a normal mode command in insert mode?
Ctrl-O is used to leave insert mode, execute one normal mode
command and go back to insert mode. For example, if you want to see your
runtimepath value, you can first press
ctrl-o and then use
&runtimepath to see the option’s value.
Wrap selection with quotes or other characters
Install vim-surround. Go to visual
block mode (press
v in normal mode), select text you want to wrap, press
and press the wrapping characters such
If you want to indent the code and as well as wrap it, then you need to go to
visual line mode (press
V in normal mode)
Rename multiple occurrences of a variable
First, search the variable you want to rename. Then press
cgn to change it.
Go back to normal mode, press
. (dot), the next match will be replaced with
the new name. If you want to skip some match, press
It is not as powerful as the Sublime Text multiple cursor feature, but should suffice for refactoring your code most of the time.
Search Unicode characters using its code point
For example, if we would like to search
a (Unicode code point is
Neovim, the correct format is
:h /character-classes for more
License CC BY-NC-ND 4.0