2

I'm learning .vim configuration and scripting by studying a real world .vimrc file used by Damian Conway (a well known Perl programmer). He has this bit in his file:

augroup Perl_Setup
    autocmd!
    autocmd BufNewFile   *.p[lm65],*.t   0r !perl_file_template <afile>
    autocmd BufNewFile   *.p[lm65],*.t   /^[ \t]*[#].*implementation[ \t]\+here/
augroup END 

I think I grok the first autocmd line which looks like it runs a script he created, perl_file_template, that gets passed the filename as an argument and which then inserts its output into the top of buffer.

I am at a loss as to what the second autocmd line might do, though. It looks like a regex but I'm not sure what it might do in this context of a vim command.

1

There are 7 seven types of Ex commands, which you can read about in :h getcmdtype().

A forward search (/pattern) is a type of Ex command, and its effect is to position the cursor on the first character of the next found occurrence.

Since a forward search is an Ex command, it can be automatically executed by an autocmd.

As you said, it seems that the purpose of your first autocmd is to import the output of a shell command.
As to the 2nd autocmd, its purpose seems to be to look for a pattern inside the text which was imported in the buffer, and position the cursor on its first character. The pattern is:

^[ \t]*[#].*implementation[ \t]\+here

Which could be broken down like this:

  ^[ \t]*[#].*implementation[ \t]\+here
" ||| | ||| |||             || |  ||
" ||| | ||| |||             || |  |+-- word 'here' literally
" ||| | ||| |||             || |  +-- the character inside the collection can be present 1 or several times
" ||| | ||| |||             || +-- tab
" ||| | ||| |||             |+-- space
" ||| | ||| |||             +-- a collection
" ||| | ||| ||+-- word 'implementation' literally
" ||| | ||| |+-- quantifier to repeat the last character 0, 1, or several times
" ||| | ||| +-- any character
" ||| | ||+-- a number sign
" ||| | |+-- another collection of characters
" ||| | +-- any character inside the previous collection can be present 0, 1 or several times
" ||| +-- tab character
" ||+-- space
" |+-- collection of (2) characters
" +-- beginning of line

I'm not sure why the number sign is inside a collection, maybe I misunderstood something.

As an example, I think it could match the following line:

    # foobar implementation    here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.