Reputation: 1025
In VIM, is there a way to select a block of text and convert it into bullet points or list?
Given this text:
abc
def
ghi
I want to be able to visually select them and convert them to list:
* abc
* def
* ghi
I have been doing this manually, using visual block mode <C-v>
on the first column of the entire text, and then doing insert (i.e <C-v>jjjI* <esc>
). However, it is very cumbersome, and I'd love to make this operation easier to execute.
Bonus: It would be great to toggle between bullet points and normal text. Also it would be great to be able to create numbered list instead of bullet list (but that may be another SO question).
Upvotes: 0
Views: 1022
Reputation: 7627
You could define the following maps:
nnoremap <m-n> vip:s/^/* /<cr>
nnoremap <m-u> vip:s/^/\=(line('.')-line("'<")+1).' '/<cr>
The first map (provided in earlier answer by Kent) will put *
in front of every line of current paragraph. The second map will make a numbered list.
Upvotes: 2
Reputation: 195169
nnoremap bip vip:s/^/* /<cr>
This mapping may make it faster.
In normal mode, you type bip
the paragraph which you cursor locates is gonna be converted into bullet points.
bip
-> "bullet in paragraph"
Upvotes: 2
Reputation: 191779
The limitation here is really know where your list content begins and ends. Once you have it highlighted I'm not sure you can get much faster than I* <esc>
.
If you have a long list and you know that there is an empty line immediately after the list with each element on its own line, you can move to the start of the list, then <C-V>)bI* <esc>
which will be faster if your list is particularly long. )
moves you to the end of the current paragraph.
If your list ends at the end of the file, <C-v>GI* <esc>
will work.
You can redo the previous set of commands with .
, so moving to the start of the list and running I* <esc>
, then j.j.j.j.
will work, but I don't think that's much better than what you're already doing.
In general if you can find an faster way to move to the end of the list, I* <esc>
is probably fast enough. If you wanted you could create an key mapping in your .vimrc
to do this faster if it's something you need to do a lot.
Upvotes: 4