0

In vim, many syntax elements automatically get inserted on the next line when I start a newline. For example, if I am inside a vim file editing a comment in insert mode, when I go to the next line, I will see the " character inserted at the appropriate indent level so I can continue my comment. I want to know how this is done inside a vim syntax file.

As a more concrete example, let's suppose that I want to make a syntax file that recognizes a bulleted list (using * as the bullet). I could add a simple match expression

    syntax match Bullet /^\s*\*\ /

I can then highlight that bullet with something like

   highlight link Bullet Statement

However, after I complete one bullet point, and hit enter to go to the next line, I need to add something else to get it to automatically add a * before my cursor. Obviously this should take place within the context of the automatic indenting. What exactly do I need to do in order for this syntax insertion to take place?

2 Answers 2

1

The commands in a Vim syntax file control the highlighting of elements in a file according to the syntax of the language used in the file. They do not control the automatic insertion of anything.

Vim automatically inserts a " at the start of a new line if it knows that " is a comment leader, it thinks that you are typing a comment, and certain other settings are in effect that tell Vim that you want it to automatically insert a comment leader at that point. See

:help 30.6

for an overview of commenting. Also see

:help 'comments'
:help 'formatoptions'

To make Vim recognize * as a bullet point, you can execute this:

:set comments+=b:*

How well that works will depend on the 'filetype' of the file you are editing and the values of the 'comments' and 'formatoptions' options. It's hard to make a specific recommendation without knowing more about the conditions under which you want to use * as a bullet.

0

Many thanks to garyjohn who pointed me in the right direction. For the simple example I asked about, I was able to come up with the following solution.

In ~/.vim/syntax/list.vim, I included the previously mentioned syntax highlighting, namely:

if exists("b:current_syntax)
    finish
endif

syntax match Bullet /^\s*\*\ /
highlight link Bullet SpecialChar

let b:current_syntax = 'list'

Next, in order for vim to automatically detect the list filetype, I added a file ~/.vim/ftdetect/list.vim with the following:

autocmd BufRead,BufNewFile *.list set filetype=list

This file also made it so that vim automatically used any filetype plugins associated with lists. Specifically, I added ~/.vim/ftplugin/list.vim with the following:

setlocal comments+=b:*\ 
setlocal formatoptions+=cro

With respect to this file, there are few items to note. First off, note that I used setlocal so that this command only gets applied to list files. Second, note the trailing whitespace which is being escaped. This makes it so that the comment is a * followed by a space (which matches the regex in the syntax file). Third, note the various things that I added to formatoptions to make it so that vim automatically helps you with the bulleted list. The documentation for these can be found with

:help fo-table

To save the reader time, I'll describe just these four options briefly. The 'c' auto-wraps the comments, inserting the comment leader (in this case '* '). The 'r' automatically inserts the comment leader in insert mode after hitting enter. This option respects indentation so it will allow you to easily make multi-leveled bulleted lists. Finally, the 'o' inserts the comment leader when the o key in normal mode.

Clearly this bulleted lists is a simple example, but it serves to illustrate how more complex comment continuations can be added to ftplugins.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .