How can I a put a line like “==========” quickly in vim

后端 未结 5 1744
被撕碎了的回忆
被撕碎了的回忆 2020-12-30 14:36

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

相关标签:
5条回答
  • 2020-12-30 14:40

    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.

    0 讨论(0)
  • 2020-12-30 14:50

    Another that will work:

    yypv$r=
    
    0 讨论(0)
  • 2020-12-30 14:57

    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 __.

    0 讨论(0)
  • 2020-12-30 15:02

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

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

    0 讨论(0)
  • 2020-12-30 15:04

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

    0 讨论(0)
提交回复
热议问题