Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript basic regular expression


May 24, 2021 Vim


Table of contents


Vim is a text editor, which means that a lot of Vimscript code will focus on working with text. Vim has strong support for regular expressions, although as always there are pits.

Hit the following text into the buffer:

max = 10

print "Starting"

for i in range(max):
    print "Counter:", i

print "Done"

This is the text that we will use to test the positive support of Vimscript. I t happens to be Python code, but don't worry if you don't understand Python. It's just an example.

I would assume that you understand basic regular expressions. I f you don't understand, you should pause reading this book and start reading Zed Shaw's Learn Regex the Hard Way. There is no Chinese, or you can choose another book, or just Google it, and continue after you've finished reading it.

Highlight

Before we begin, take a little time to talk about search highlighting so that we can make the match more obvious.

:set hlsearch incsearch

hlsearch makes Vim highlight all matches in the file, and incsearch makes Vim highlight a match when you're searching for content

Search

Move your cursor to the top of the file and perform the following command:

/print

When you tap letter by letter, Vim starts highlighting them on the first line. When you press carriage return to search, highlight the print of print move your cursor to the next match.

Now try to execute the following command:

:execute "normal! gg/print\<cr>"

This will move to the top of the file print for the print, taking us to the first place to match. W ith what we saw in the previous :execute "normal! ..."

To reach the second match in the file, you only need to add a little else at the end of the command. To execute this command:

:execute "normal! gg/print\<cr>n"

Vim moves the cursor to the second print in the print all matches at the same time).

Let's try to start in the opposite direction. To execute this command:

:execute "normal! G?print\<cr>"

This time we G the end of the file with G and ? to search in reverse.

All the search commands should be rotten - we're getting you used :execute "normal! ..." been practiced over and over again, because it allows you to do everyday things in Vimscript code.

Magic

/ And ? C ommands can accept regular expressions, not just normal characters. Follow these commands:

:execute "normal! gg/for .+ in .+:\<cr>"

Vim complains that he can't find the mode! I told you Vim supports a positive search, so why? Try the following command:

:execute "normal! gg/for .\\+ in .\\+:\<cr>"

This time Vim highlights the "for" loop, as we expected at first. B efore you continue reading, take a minute to think about why. Remember execute accepts a string.

Here's the answer: There are two reasons why we need to write commands like this:

  • First, execute accepts a string in calling normal! When commanded, the double backslash is converted to a SLR slash.
  • Vim has four different "patterns" for parsing regular expressions! The default mode + backslash to be added before the plus to make it represent "one or more previous characters" instead of "a one-word plus sign."

By performing the search directly in Vim, you can easily notice their differences, enter the following command and press Enter:

/print .\+

Now you \+ magic of the . . . Dual backslashes are only required when passing patterns execute strings to execute.

Literal string

As we mentioned in the chapter on strings, Vim allows you to use single quotes to define literal strings that can pass characters directly. For example, 'a\nb' is four characters long.

Can we use literal strings to avoid frequent double backslashes? Think about it for a minute or two, after all, the answer is probably more complicated than you think.

Try the following command (note the single quotes and SLR slashes this time):

:execute 'normal! gg/for .\+ in .\+:\<cr>'

Vim takes you to the top of the file but no longer moves to the first matching place. Are you right?

The command doesn't work because we need the pattern of \<cr> to be escaped into a carriage return to start the search. Because we're using a literal string, it's not equivalent to typing in Vim as usual /for .\+ in .\+:\<cr> .

Don't be afraid, the method is still more difficult than! D on't forget that Vim allows string connections, so you can split commands into easy-to-understand segments. Execute the following command:

:execute "normal! gg" . '/for .\+ in .\+:' . "\<cr>"

This approach connects three small strings before passing them to execute and we can use literal strings for regulars and general strings for others.

More Magic

You may wonder how Vimscript's four different regular resolution patterns are different from the regular expressions in Python, Perl, or Ruby. Y ou can read their documentation if you like. But if you just want to find a simple scientific solution, read on.

Execute the following command:

:execute "normal! gg" . '/\vfor .+ in .+:' . "\<cr>"

Once again, we put regular expressions in separate literal strings, and this time we're \v to guide the pattern. This tells Vim to use its "very magic" resolution mode, which is very similar to other languages.

If you start all your regular expressions with \v you don't have to tangle with Vimscript's other three crazy regular patterns.

Practice

Read :help magic

Read :help pattern-overview the positive types supported by Vim. Stop when you see the acter classes.

Read :help match T ry to perform several times :match Error /\v.../

Add ~/.vimrc file match highlight extra white space as an incorrect mapping. It is <leader>w

Add another map to clear the match (for <leader>W

Adding a normal mode automatically inserts a map of \v search. If you're stuck in this exercise, don't forget that Vim's mapping is very simple, you just need to tell it which keys to convert the mapping keys to.

Add the hlsearch to your ~/.vimrc file and set it as you wish. incsearch

Read :help nohlsearch . Note that this is a command and is not hlsearch

Add a highlighted map that eliminates the last search match in your ~/.vimrc file.