11

While Vim supports automatic indenting in lists, the default setting only covers ordered lists, starting with digits, such as:

1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
   tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
2. veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
   commodo consequat.

I have not been able to figure out how to extend this to unordered, bulleted lists, such as:

* Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
  tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim
* veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea
  commodo consequat.

Changing the formatlistpat RegEx did not lead to the desired results (indeed, it even broke ordered lists).

Kazark
  • 3,419
  • 3
  • 26
  • 35
AnC
  • 334
  • 4
  • 10

4 Answers4

12

Knowing what you tried to set the value to would help, but I'm guessing you didn't properly escape the backslashes.

The default value is

formatlistpat=^\s*\d\+[\]:.)}\t ]\s*

but to actually set that value (in your vimrc or at the cmdline) you have to use

set formatlistpat=^\\s*\\d\\+[\\]:.)}\\t\ ]\\s*

This is explained in :help option-backslash. A simple modification to allow formatlistpat to work with * delimited, unordered lists would be

set formatlistpat=^\\s*[0-9*]\\+[\\]:.)}\\t\ ]\\s*
jamessan
  • 1,071
  • 7
  • 12
  • Indeed, that works - many thanks! I thought I had properly escaped everything (even reduced the pattern's complexity), but apparently I was wrong. I might have escaped the asterisks, which would explain my troubles... – AnC Jan 22 '10 at 21:42
  • 2
    As explained in the help for `[]` it is faster to use `[[:digits:]*]` or `[\d*]` rather than `[0-9*]` – Aditya Oct 21 '10 at 05:11
  • 1
    One could sidestep the quoting issue by using `let` rather than `set`, e.g: `let &formatlistpat='^\s*\(\d\+[\]:.)}\t ]\|[*-][\t ]\)\s*'`. (This adds support for `*` and `-` as bullets, in addition to numbers.) – wjv May 02 '14 at 07:42
  • @wjv I've tried your solution and it works for - lists, but not with asterisks. Any idea what could cause this? – languitar Jun 03 '15 at 11:25
  • @wjv seems the * inside the `comments` variable, despite being the middle part of a three-part comment, lets vim confuse the asterisk with a line comment. – languitar Jun 03 '15 at 12:42
  • @languitar I get the idea we’re pushing edge cases. What I’ve done since posting that comment is to separate out Vim configuration common to all text formats and put that in one file, and ditto for config that applies only to editing code (irrespective of language). These files are loaded based on the file type. Hence, I now set things like `formatlistpat` _only_ for editing text or text-like formats. – wjv Jun 03 '15 at 13:41
  • @wjv That's also what I've done now. – languitar Jun 04 '15 at 11:06
3

In addition to what jamessan wrote (in particular his formatlistpat suggestion for working with * lists), it is important to have the 'c' option (comment formatting) unset in formatoptions:

set formatoptions-=c

otherwise Vim gets confused between the formatting of * bulleted lists and the formatting of comments. You end up with an extra * on the 2nd and following lines.

Blixtor
  • 31
  • 1
  • Not only that, but the `q` (allow formatting of comments with `gq`) `formatoptions` flag can cause problems when formatting with `gq`. +1 – Kazark Mar 13 '13 at 21:29
2

I am writing a new answer to this question because I had to combine multiple answers from this question and this other question to make this work. Here is my final configuration.

set formatlistpat=^\\s*[0-9*]\\+[\\]:.)}\\t\ ]\\s*
set formatoptions-=c
set comments-=mb:*
merlin2011
  • 1,855
  • 3
  • 23
  • 33
1

I had some trouble getting lists like a) recognized, so I'll post my solution here:

" Recognise lists like 1), 1., a), a., and so on
" Note that | need to be escaped AND preceeded by a literal backslash
set formatlistpat=^\\s*\\([0-9]\\+\\\|[a-z]\\)[\\].:)}]\\s\\+
RobM
  • 675
  • 1
  • 6
  • 10