This is the 4th post of my post series on nifty Nvim/Vim techniques that will make my editing experience easier.

Click here to check other posts in this series.

Check if a string contains a pattern?

There are two simple ways:

  • Use match(str, pattern)
  • Use =~# or =~?

For match() function, Vim assumes that magic (see :h /magic) option is set for patterns. If pattern is found in str, it will return the index where the pattern starts. Otherwise, it will return -1.

You can also use regex match via =~# and =~? (see :h expr4). =~# 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 s: (see 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? M or A

According to documentation (:h key-notation), <M-...> and <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 <NUM> repeated <Chars>.


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 :echo &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 S, and press the wrapping characters such ' or " or {.

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 n.

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 u+0041) in Neovim, the correct format is /\%u0041, see :h /character-classes for more info.