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

Vimscript executes normal!


May 24, 2021 Vim


Table of contents


Now that we've execute normal! , we can dive into a Vimscript idiom. Execute the following command:

:execute "normal! gg/foo\<cr>dd"

This moves to the beginning of the file, foo first appears, and deletes that line.

We've tried to use normal! t o execute a search command but cannot enter the required carriage return to begin the search. C ombine execute normal! will solve this problem.

execute allows you to create commands so that you can use Vim's normal escape string to generate the "can't type" characters you need. Try the following command:

:execute "normal! mqA;\<esc>`q"

What did this order do? Let's open up and say:

  • :execute "normal! ..." Executes the command sequence, as they were entered in normal mode, ignores all maps, and replaces the escape string.
  • mq Save the current location to the tag "q".
  • A Move to the end of the current line and enter ininsert mode after the last character.
  • ; : We are now in ininsert mode, so only one is written"; "。
  • \<esc> representing the Esc key, taking us away from the insert pattern.
  • `q Go back to where the tag "q" is located.

It looks a bit winding, but it's really useful: it fills in a stop sign at the end of the current line and keeps the cursor still. When writing Javascript, C, or other languages that use a sign as a statement separator, this mapping will help you once you forget to add a sign.

Practice

:help expr-quote should have seen it before) to remind you how to pass special characters to execute with execute by escape normal!

Before opening the next chapter, put down the book and have a rest. E at a sandwich or have a cup of coffee. Feed your pet if you have one.