12

Various plain-text markup languages: reStructuredText, Markdown, asciidoc (others too), use an underline to denote titles.

Chapter 1 Title
===============

Section 1.1 Title
-----------------

Subsection 1.1.1 Title
~~~~~~~~~~~~~~~~~~~~~~

Section 1.2 Title
-----------------

Chapter 2 Title
===============

Is there a convenient way add these in vim?

add a character with a length matching the line above.

  • It doesn't directly answer your question, but it's worth saying that some markup langs (e.g. Markdown) also support a leading # character instead for a level 1 heading, ## for level 2, etc. That's how I write Markdown, and I find it clearer that way anyway. – Andrew Ferrier Feb 5 '15 at 20:12
  • @Andrew Ferrier, Im not using markdown & the question isnt specifically about markdown, its just an example of a markup that can use titles like this. I listed 3x different markups especially to avoid discussion on the specifics on any of the markup languages. – ideasman42 Feb 6 '15 at 3:16
  • I understand. It was just an incidental comment for anyone who does. Your question is totally valid, I agree. – Andrew Ferrier Feb 6 '15 at 6:39
21

A naïve approach (no plugins or scripting) would be EscYpVr=.

Explanation:

  • Yp duplicates the current line and puts the cursor on the lower line.
  • V selects the second line in Visual Line mode.
  • r= replaces all characters on the line with the = character.

You can, of course, :noremap that keystroke sequence to your taste.

eg:

" Add Heading: Control-H
nnoremap <C-h> YpVr

...leaving off the last character so you can type in whichever you want for the title.

  • 2
    This is not naïve by any means. – romainl Feb 5 '15 at 7:42
  • 2
    @romainl it's only naive until you map it to something shorter I guess – Dhruva Sagar Feb 5 '15 at 7:55

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.