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

Vimscript automatic command


May 24, 2021 Vim


Table of contents


Now let's talk about something as important as mapping: automatic commands.

Automatic commands allow Vim to automate certain specified commands that are executed when certain events occur. Let's look at an example first.

Use :edit foo open a new file, and then immediately use :quit to close. L ook at your hard drive and you'll see that this file doesn't exist. This is because before you first save the file, Vim_ didn't actually create it.

Let's make some changes to Vim so that Vim can create files when you start editing them. Execute the following command:

:autocmd BufNewFile * :write

There's a lot to explain, but before that I suggest you feel how it works. E xecute :edit foo use :quit and then look at the hard drive. The file will exist at this time (of course the contents of the file are empty).

You can't delete this automatic command until you turn off Vim. We'll show you how to avoid this in a later section.

The automatic command structure

Let's drill down into the automatic commands we just created:

:autocmd BufNewFile * :write
         ^          ^ ^
         |          | |
         |          | 要执行的命令
         |          |
         |          用于事件过滤的“模式(pattern)”
         |
         要监听的“事件”

The first part of this command is the type of event we want to listen to. V im provides a lot of events that you can listen to. These events include:

  • Start editing a file that does not currently exist.
  • Read a file, regardless of whether it exists.
  • Change the filetype buffer.
  • Do not press a key on the keyboard for a certain period of time.
  • Go into insertion mode.
  • Exit insert mode.

Only a small part of the available events is mentioned above. There are many other events that you can use to do something interesting.

The next part of this automatic command is a "pattern" that further limits the scope of execution of the command you want to execute. Open a new Vim instance and execute the following command:

:autocmd BufNewFile *.txt :write

This is basically the same as the previous automatic command, but it .txt which means that when you create a new file as a txt file, Vim automatically executes the white command to save the file to the hard disk when the file is created.

Try :edit bar then :quit :edit bar.txt then :quit You'll find that bar.txt automatically, bar its suffix name is not txt and doesn't match the pattern.

The last part of this automatic command is the command that we want to execute when the event occurs. T his section is easy to understand, as we do with other commands, except that special characters cannot be used in this <cr> as . We'll talk later in the book about how to break this limit, and now you just have to follow it.

Let's take another example

Let's define another automatic command, this time using a different event. Execute the following command:

:autocmd BufWritePre *.html :normal gg=G

The normal command is normal here, and I'll talk about it later in the book, which may be a little ahead of the time, but I think it's a good example of using automatic commands, so please put up with it first.

Create a new file foo.html Edit it with Vim and enter the following text, make sure that the text you enter is exactly the same, including blank characters:

<html>
<body>
 <p>Hello!</p>
                 </body>
                  </html>

Execute :w Save this file. L et's see what happens. Vim appears to have re-indented the text before the file was saved.

ok, please first believe that my text indentation processing :normal gg=G dry, don't be :normal gg=G can do this.

We should focus on automatic commands. The event used in this automatic command is BufWritePre which is triggered before you save any characters to the file.

*.html pattern, which guarantees that commands will only be executed when editing html files. T his is where automatic commands are powerful because it can execute the commands we want to execute specifically for a particular type of file. ok, let's continue to explore it.

Multiple events

You can create an automatic command that binds to events that are separated by commas. Execute the following command:

:autocmd BufWritePre,BufRead *.html :normal gg=G

This is basically the same as the automatic command above, the difference is that it lets Vim not only indent when writing html files, but also when reading html files. This command can be useful if some of your colleagues don't like to make HTML file formats look good.

There is an unwritten rule in Vim script programming that you should run commands using both BufRead and BufNewFile events, so that when you open a file of a certain type, it executes regardless of whether the file exists or not. Execute the following command:

:autocmd BufNewFile,BufRead *.html setlocal nowrap

The command above will make it off whenever you edit HTML file auto-line.

FileType event

The most useful event is FileType event. This event is triggered when Vim sets filetype a buffer.

Let's set up some useful maps for different file types. Run the command:

:autocmd FileType javascript nnoremap <buffer> <localleader>c I//<esc>
:autocmd FileType python     nnoremap <buffer> <localleader>c I#<esc>

Open a Javascript file .js move the cursor to a line, tap <localleader>c line on which the cursor is located will be annotated.

Now open a Python file .py move the <localleader>c to a line, tap the same line, and the same line will be annotated, between using Python's comment character at this point!

By including the local buffer mappings we learned in the last chapter in the automatic commands, we can create maps that will be processed differently depending on the type of file we are editing.

This can lighten the burden of thinking a lot when we code. If we want to add a comment, we might think that we have to move the cursor to the top of the line and then add a comment character, and with the map above, we just need to simply understand it as "comment out the line."

Practice

Browse :help autocmd-events the events that automatic commands can bind. Y ou don't need to remember every event right now. Just need to know what you can do with these events.

Create some FileType that setlocal make some settings for the file types you like. You can set wrap, wrap list spell number file types.

Create commands like "comment out this line" on some file types that you'll often work with.

Write all these automatic commands into ~/.vimrc file. Remember to do this by using the quick editing and ~/.vimrc mentioned in the previous section, which is necessary!