This Vim wizardry

vim

In a question about jVi and its capability to parse .vimrc, the author gives an example of a complex vim binding that he asserts jVi would not be able to handle:

inoremap hh <c-o>?\%<c-r>=line('.')<Return>l\({}\\|\[]\\|<>\\|><\\|()\\|""\\|''\\|><lt>\)?s+1<Return>

I wonder what (mostly how) it does. So far I get:

  • <c-o> : execute one command in normal mode and return to insert mode
  • ?/%n : look backwards for a character with the specified dec / hex code
    • what is the point of inserting the line number here?
  • the last part looks for a specific combination of brackets and places the cursor there
    • I don't understand how l can be used like that

Care to clarify?

Best Answer

The Ctrl+O is as you indicated. And the first ? does start a backwards search.

A pattern sequence like \%15l restricts the search to line 15, the l after the first <Return> is the end of such a sequence. The mapping uses Ctrl+R, the special = register (:help i_CTRL-R), and the expression line('.') to insert the current line number into that sequence, preventing the search from going to a different line.

The rest of the mapping up to the final ? character is a fairly straight forward regular expression to match any of a number of character sequences.

The final ? indicates the end of the pattern, the s+1 portion causes the cursor to be positioned 1 character after the start of the match (:help search-offset).

Related Question