Reputation: 2254
vim indents lists (or bullets) within text files fine:
- this is item one that
is indented correctly
- this is item two that
is also indented
correctly
I can type gqap
within the paragraph above, and the formatting and indenting work correctly.
However, this doesn't work within python comments, and gets indented like so:
# - this is item one that
# is not indented correctly
# - this is item two that
# is also not indented
# correctly
If I type gqap
within the python comment above, vim doesn't even recognize the bullet points. and ends up formatting the entire block into a paragaph.
So with respect to bullets and indenting, how do I get vim to behave the same way inside python comments as it does within a regular text file?
Upvotes: 1
Views: 930
Reputation: 1390
This is actually a pretty hard problem that is not obviously documented even though it is very well supported by formatlistpat. I was able to support multiple styles of lists just by identifying formatlistpat and formatoptions as the settings I wanted to play with in my vimrc.
You can get a full overview with the following:
:help 'formatlistpat'
:help 'formatoptions'
Your working example indicates that you like the way that vim transforms the following:
- this is item one that
is indented correctly
- this is item two that
is also indented
correctly
By entering gqap or gqip in normal mode you get the following:
- this is item one that is indented correctly
- this is item two that is also indented correctly
But there is a problem in that you cannot indent unorder lists in vim with your configuration if you are presented with something like the following:
# - this is item one that
# is not indented correctly
# - this is item two that
# is also not indented
# correctly
# + this is item one that
# is not indented correctly
# + this is item two that
# is also not indented
# correctly
# * this is item one that
# is not indented correctly
# * this is item two that
# is also not indented
# correctly
In this case gpaq will incorrectly format to the following:
# - this is item one that is not indented correctly - this is item two that
# is also not indented correctly + this is item one that is not
# indented correctly + this is item two that is also not indented
# correctly * this is item one that is not indented correctly * this
# is item two that is also not indented correctly
You can immediately correct this indentation issue with the following:
:set formatoptions+=n
:set formatlistpat=^\\s*[\\-\\+\\*]\\+\\s\\+
Which will reformat your comment in the way that you intend:
# - this is item one that is not indented correctly
# - this is item two that is also not indented correctly
# + this is item one that is not indented correctly
# + this is item two that is also not indented correctly
# * this is item one that is not indented correctly
# * this is item two that is also not indented correctly
There are other lists you nay want to support too:
# a) this is item one that is not
# indented correctly
# b) this is item two that is also not
# indented correctly
# C. this is item three that is also not
# indented correctly
Can be correctly formatted with:
:set formatlistpat=^\\s*\\w[.\)]\\s\\+
The following:
# 1 this is item one that
# is not indented correctly
# 2) this is item two that
# is also not indented
# correctly
# 33. this is item three that
# is also not indented
# correctly
Can be correctly formatted with:
:set formatlistpat=^\\s*\\d\\+[.\)]\\s\\+
The following:
# i. this is item one that
# is not indented correctly
# ii. this is item two that
# is also not indented
# correctly
# iv) this is item three that
# is also not indented
# correctly
Can be correctly formatted with:
:set formatlistpat=^\\s*[ivxIVX]\\+[.\)]\\s\\+
You can put that all together with two simple lines:
:set formatoptions+=n
:set formatlistpat=^\\s*\\w\\+[.\)]\\s\\+\\\\|^\\s*[\\-\\+\\*]\\+\\s\\+
Upvotes: 5