search

Home  >  Q&A  >  body text

gvim - How to insert repeated symbols n times until aligned with the previous line in Vim?

Recently I am using Vim to write some small scripts. When writing comments, I want to separate them in this way:
"------------------------------------------------ ---------
"-------------This is Comments ----------------------------
"------------------------------------------------ ---------
I know that ni- can be used to insert n "-" signs, but the middle line needs to be entered manually. Because the content length of Comments is unknown, it needs to be entered manually every time, and sometimes misalignment occurs. I wonder if there is a way to automatically insert enough "-" signs to align with the previous line after entering Comments?

为情所困为情所困2742 days ago846

reply all(4)I'll reply

  • 我想大声告诉你

    我想大声告诉你2017-05-16 16:42:54

    Add this to your vimrc:

    inoremap <expr> <C-r>* repeat('*', strdisplaywidth(getline(line('.')-1))-strdisplaywidth(getline('.')))
    

    Then press <C-r>* when you need to use an asterisk to align with the previous line.

    Another solution is to copy all the asterisks in the previous line before writing the text part, and then press gR where you need to write text to enter the virtual replacement mode, so you don’t have to count the asterisks.

    reply
    0
  • 巴扎黑

    巴扎黑2017-05-16 16:42:54

    .Use of number. .

    reply
    0
  • 漂亮男人

    漂亮男人2017-05-16 16:42:54

    I don’t know how to do it directly, but there is another way to achieve your needs, and it’s not troublesome:

    1. Insert more symbols each time
    2. Use block selection to delete redundant symbols at once

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-05-16 16:42:54

    Uh~, can you write three asterisks first, jump to the middle, and then press R?

    reply
    0
  • Cancelreply