zjk
zjk

Reputation: 2163

How can I a put a line like "==========" quickly in vim

I am editing restructuredtext files. I often need to put some charactors like "=-`~" in one line, and I want the length of the line match the previous line. How should I do this in vim?

a long long title
=================

Thanks!

Upvotes: 6

Views: 313

Answers (5)

Tom
Tom

Reputation: 16198

If your line starts without any trailing whitespace:

Hello World    

Normal Mode:

YpVr=

Gives:

Hello World
===========

Explanation

Y -> Yank entire line, like yy
p -> paste the line
V -> select whole line in visual line mode r -> replace all of select with next character
= -> the character to replace the others

If you line has leading whitespace, eg:

    Hello World

Use:

Ypv$r=

Giving:

    Hello World
    ===========

We use v$ visual selection to the end of the line, rather than using V to select everything on the line.

If you had trailing whitespace you can use the g_ movement to get to the last non whitespace character.

Upvotes: 2

Johan Kotlinski
Johan Kotlinski

Reputation: 25749

How about yyp:s/./=/g ?

You can map that to a key, e.g. :map <F5> yyp:s/./=/g<CR>

Upvotes: 6

Ren&#233; Nyffenegger
Ren&#233; Nyffenegger

Reputation: 40533

When the cursor is placed on a long long line you could use something like

:s/\(.*\)/\=submatch(1) . nr2char(13) . repeat('=', strlen(submatch(1)))/

In order to make it more easy to do the substitution, I'd then use a map:

nmap __ :s/\(.*\)/\=submatch(1) . nr2char(13) . repeat('=', strlen(submatch(1)))/

So, you can underline the line where the cursor is on with typing __.

Upvotes: 0

Barry Brown
Barry Brown

Reputation: 20614

Another that will work:

yypv$r=

Upvotes: 17

chelmertz
chelmertz

Reputation: 20601

I would use yypver= to avoid searching & shift button as much as possible. This could of course also be mapped to a key.

Upvotes: 2

Related Questions