Azure and DevOops

Other Neovim editing basics

ยท Christian Piet

Searching, replacing, and commenting in Neovim

In my previous posts, I covered the basics of navigating Neovim and editing text. While moving around words, lines, or paragraphs in Normal mode and selecting text with Visual mode, it works swiftly to navigate around text; sometimes, you need to look for something specific one or multiple times. In this post, I will cover another set of basic editing features: searching, replacing text, and commenting.

Searching

There are several ways to search for text in Neovim. The fastest way is to use Normal-mode commands. You can also use Visual-mode to search for the selected text. Finally, you can use Command-mode to perform more complex searches and replacements.

Searching in Neovim sometimes highlights text matches in your buffer/opened file. You can disable this highlighting using the :nohlsearch-command, or use the shortcut :noh. If you think “Oh, I can’t remember this command”, you can map it to a key combination of your choice. In a later post, I will cover how to create custom mappings.

Searching in Normal mode

The easiest way to search for text depends on your starting context/where your cursor is. If your cursor is on the word you’re looking for, you can use * to search forward for the next occurrence of that word, or # to search backward. To go to the next occurrence of the word, you can press n, and to go to the previous occurrence, you can press N.

The status line will show the search term and the current match number out of the total number of matches.

Search-method Key/Command
Backwards search of word under cursor *
Forward search of word under cursor #
Select all text on line

However, if you’re nowhere near the word you’re looking for, you can also search for the term directly. You can use / to search forward by pressing / or ? to search backward.

After typing / or ?, you can type the word or phrase you want to search for and press Enter. Neovim will then move the cursor to the next occurrence of that word or phrase.

Search-method Key/Command
Backwards search ?<pattern><CR>
Forward search /<pattern><CR>

<CR> means pressing the Enter-key. You can ignore casing using \c at the end of your search term, or enforce casing with \C. You can also disable case sensitivity for all searches by adding :set ignorecase to ignore it, and :set noignorecase to enable case-sensitivity again. If you want to override this setting and enforce case sensitivity when searching, you can add set smartcase to your configuration file. This will make searches case-sensitive if your search term contains uppercase letters.

Select all text on line

Regex searching

The keen-eyed among you might have noticed that, when pressing * and #, the editor populates the search commands with the term under your cursor. So, if you’re wondering how to search for this specific word manually, you can see that after pressing * and #, the search term is populated. You can then edit this term to your liking and press Enter to search for it.

You can also use regular expressions in your search terms. For example, if you want to search for all text on a line that starts with “foo” and ends with “bar”, you can use the search term /foo.*bar. For more information on regular expressions, you can read :h regular-expression and :h magic.

Select all text on line

There are also options to search for an offset of characters/lines before/after your matched text. You can read more on pattern searching at :h pattern-searches or at the Vim user manual written by Bram Molenaar (the creator of Vim).

Searching with Visual mode

Sometimes you want to search for a specific piece of text near your cursor, but it’s not a single word. In this case, you can use Visual-mode to select the text you want to search for and then press * or # to search for the next or previous occurrence of that text.

This works exactly the same as in Normal-mode, but now you can select the text you want to search for. This is especially useful if you wish to search for a specific set of characters or a phrase.

Select all text on line

Replacing

All this searching is great, but sometimes you want to replace text. You can do this in Command-mode using the :substitute, or :s command. The :s command allows you to search for a specific term and replace it with another term. Providing no replacement term will delete the matched text.

The substitute command is shown as follows in the help (:h :s): :[range]s[ubstitute]/{pattern}/{string}/[flags] [count]

The range specifies the lines you want to search and replace in. If you don’t specify a range, it will only search and replace in the current line.

You can specify a range using line numbers, for example, :1,19s will search and replace in lines 1 to 19. You can also use % to specify the entire file, for example, :%s/<pattern><CR> will search and replace in the whole file. Command ranges are explained in more detail at user manual - 10.3 - Command Ranges.

The pattern is the text you want to search for, just like we did earlier with the / and ? commands. The string is the text you want to replace the pattern with.

The flags are optional and can be used to modify the behavior of the substitute command. The most common flags are:

Flag Description
i Ignore casing when searching for the pattern.
I Enforce casing when searching for the pattern.
g Replace all occurrences of the pattern in the line. If you don’t specify this flag, only the first occurrence will be replaced.
c Confirm each replacement.

You can find more info on flags for substitute at :h :s_flags.

Replace-example Key/Command
Replace matched pattern on current line :s/<pattern>/<replacing text><CR>
Replace first match searched across entire file :%s/<pattern>/Iamthereplacingtext
Replace all matches across the entire file :%s/<pattern>/Iamthereplacingtext/g
Case insensitivy search and remove all pattern matches across the entire file and ask for confirmation on each try :%s/<pattern>//gci

Example

Example of replacing all occurrences of ‘dns’ with nothing (deleting), across the entire file, ignoring case, and asking for confirmation on each replacement:

Select all text on line
Select all text on line
Select all text on line

There are several options to repeat a search and flags you can use. This should be sufficient to get you started. You can read more about the substitute command at :h :s and :h substitute.

Commenting

Another common task when editing code is commenting and uncommenting code. There are several ways to do this in Neovim, accelerated by plugins, but there are also native methods available. You can read help on commenting at :h commenting.

In Normal-mode you can use gcc to comment or uncomment the current line. You can also use gc in Visual-mode to comment or uncomment the selected lines.

gcc works by toggling the comment on the current line. If the line is already commented, it will uncomment it. If the line is not commented, it will comment it. This also supports multiple lines if you provide a count before the command; for example, 3gcc will comment or uncomment the following three lines.

Do note that commenting doesn’t work if Neovim is unaware of the filetype you’re editing. You can check the filetype with :set filetype?. If Neovim doesn’t know the filetype, it won’t know how to comment the code. There are options to set the file type, but I find it easier to start a file with :e myfile.<filetype>.

Select all text on line
Comment-method Key/Command
Comment/uncomment current line gcc
Comment/uncomment next 3 lines 3gcc
Comment/uncomment selected lines Visually select and press gc in Visual mode

The End

That’s it for the basics of navigating, editing, searching, replacing, and commenting in Neovim. I hope you found this post helpful. In my next post, I will cover how to customize your experience using options and create custom mappings to further speed up your workflow. Happy coding, everyone!

#neovim #vim #basics