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

Vimscript more Opera-Pending mapping


May 24, 2021 Vim


Table of contents


The concepts contained in Operators and Movements are a very important concept in Vim and the biggest reason why Vim is so efficient. In this chapter we will do more practice in this piece, which will make Vim stronger.

Let's say you're writing some text to Markdown right now. I f you haven't used Markdown, it doesn't matter, it's easy for what we're going to do now. Enter the following text into a file:

Topic One
=========

This is some text about topic one.

It has multiple paragraphs.

Topic Two
=========

This is some text about topic two.  It has only one paragraph.

A = uses the s as an underscore is treated as a title by Markdown. W e've now created some maps that allow us to navigate to the title through movements. Run the following command:

:onoremap ih :<c-u>execute "normal! ?^==\\+$\r:nohlsearch\rkvg_"<cr>

This mapping is somewhat complicated. N ow place your cursor somewhere in the text (don't put it on the title) and tap cih Vim deletes the title of the chapter in which the cursor is located and then remains in insert mode, which can be called "change inside heading".

Here are some things we've never seen before, so it's important to analyze the meaning of each section separately. T he first part of this mapping, :onoremap ih is the mapping command, which we are familiar with and need not say much. <c-u> are not deeply aware of it now.

Then look at the rest:

:execute "normal! ?^==\\+$\r:nohlsearch\rkvg_"<cr>

Normal

:normal command is followed by a string of characters, and whatever they mean, :normal command executes them as if it were tapping them in normal mode. W e'll talk about more :normal a later chapter, and since this has happened many times, I think it's necessary to make a simple explanation now. Execute the following command:

:normal gg

Vim jumps the cursor to the top of the file. Now do the following command:

:normal >>

Vim will indent the current row.

That normal ! W hat the hell is that? Leave it alone and talk about it later.

Execute

execute command is followed by a Vim script string (which will look into its details later) and then executed as a command. Execute the following command:

:execute "write"

Vim will write the file as if you had already entered it: :write<cr> Now do the following command:

:execute "normal! gg"

Vim executes :normal! gg which jumps the cursor to the top of the file, just like before. N ow the question comes, why should we make such an normal! egg pain, execute normal! Can't you do it?

Take a look at the following command and guess what it will do:

:normal! gg/a<cr>

This command seems to do the following:

  • Jump the cursor to the top of the file.
  • Prepare for the search.
  • Search for "a" as the target string.
  • Press the return key to perform the search.

Do it yourself, Vim jumps the cursor to the top of the file, and then... No more!

This is due to normal! O ne of the problems, this problem normal! " Special characters" are not recognized, such <cr> There are many ways to solve this problem, the simplest of which is execute which execute makes the script easier to read.

When execute any string you want it to execute. I t replaces all the special characters in this string first. I n this example, \r is an escape character that represents a carriage return character. Two backslashes are also escape characters that place an anti-slash as a normal character in a string.

If we replace the special characters in this map with the above analysis, then we can figure out how this mapping will be performed:

:normal! ?^==\+$<cr>:nohlsearch<cr>kvg_
                ^^^^           ^^^^
                 ||             ||
这里的<cr>实际上是一个回车符,而不是由4个字符——“左尖括号”,“c“,”r“和“右尖括号”组成的字符串。

So normal! T hese characters are executed as if we had struck them in a common mode. Let's split these characters with carriage returns and see how they perform:

?^==\+$
:nohlsearch
kvg_

The first ?^==\+$ for any lines consisting of two or more equal marks that do not contain any characters other than the equal sign. This command, when executed, leaves the cursor at the top of the line that meets the search criteria.

You use back search because when you want to "modify the title" and the current cursor is on the text of a section, the most likely thing you want to do is to modify the title of the section, not the title of the next section.

The second part is :nohlsearch command. This command simply clears the highlights of previous search results to prevent them from distracting us.

The final part is a sequence of commands in three common modes:

  • k Move one line up. The first part has positioned the cursor to the first character of the line made up of equal sign symbols, so after executing this command, the cursor is positioned on the first character of the title.
  • v Enter visual mode (characterwise).
  • g_ Moves to the last non-empty character on the current line. There is no $ $ $ the line change symbol, which is not what we want.

Results

This mapping does a lot of work, so it looks a little complicated, but we've figured out every part of the map. Now let's summarize:

  • We created an operator-pending map for "inside the title of the chapter."
  • We used execute and normal! These two commands execute the common commands we use to select titles, which allows us to use special characters in these commands.
  • Our map searches for lines of equal signes to navigate to a title, and then selects the text of the title in common mode.
  • Vim does the rest of the work on the title.

Let's look at another map. Execute the following command:

:onoremap ah :<c-u>execute "normal! ?^==\\+\r:nohlsearch\rg_vk0"<cr>

Place the cursor on the text of a section and tap cah to try it. T his time Vim will not only delete the title of this section, but also the lines of equal signs connected to the title. You can think of this movement as the title of the section of "

What's the difference between this mapping? Let's take a look at the previous mapping:

:onoremap ih :<c-u>execute "normal! ?^==\\+$\r:nohlsearch\rkvg_"<cr>
:onoremap ah :<c-u>execute "normal! ?^==\\+$\r:nohlsearch\rg_vk0"<cr>

The only difference is the part of the map that is used later to select the text:

inside heading: kvg_
around heading: g_vk0

The other parts are exactly the same, so let's start by positioning the cursor to the first character of the equal sign line:

  • g_ Moves to the last non-empty character of the current line, which consists of equal marks.
  • v Enter visual mode (characterwise).
  • k Move one line up. This moves the cursor to a line that contains the title text.
  • 0 Move to the first character of this line .

The result of this series of commands is to select the lines of the title's text and equal sign in visual mode, on which Vim performs the appropriate action.

Practice

Markdown can also use - character to qualify the title. A djusting the regular expressions above allows these maps to work on different types of titles. Y ou may want to :help pattern-overview Remember that the positive expression is in a string, so the backslash needs to be escaped.

Add two automatic commands to create these maps to your ~/.vimrc file. Make sure that these maps are used only for the appropriate buffers, and that you use automatic command groups to prevent copies of these automatic commands from being created each time you load ~/.vimrc

Read :help normal

Read :help execute .

Read :help expr-quote escape sequences that you can use in strings.

Create an operator-pending map for Inside Next Address, and you can use Change inside the next email address. U sing in@ a mapped key is a good choice. Y ou may also need to map this key to /...some regex...<cr>