4

if I have a list of indented text

test test
    test test
    test test
test test
     test test
          test test
     test test

and I wanted to convert it to a markdown list

- test test
    - test test
    - test test
- test test
     - test test
          - test test
     - test test

what would be the best way to do this if vim?

5

There are a few ways you could do this. The way I would probably do it is this:

:%norm I-<space>

Note that the <space> should be a literal space, not the text <space>. This simply applies the set of keystrokes I-<space> to every line in the buffer. Uppercase I enters insert mode on the first non-whitespace character of the current line.

Of course, if you don't want it on every line, and instead on a few lines, you could visually select the lines and do

:norm I-<space>

This is the most straightforward approach. The other thing you could do is a substitute command. For example:

:%s/^ */&- /

Just as with the last command, if you visually select the lines you would like to do this to, you can run

:s/^ */&- /

instead to only do this to certain lines.

  • 1
    (\s* can be used instead of ­ * in the :substitute commands ­ for this to work with tabs as well as spaces indentation whitespace.) – Aaron Thoma Dec 22 '16 at 10:49
  • Or, for the substitute, target non-whitespace with :%s/\S/- &/ – Antony Dec 22 '16 at 15:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.