I would like to copy paste everything after a comment into the [] place holder of the next line.


%This is a comment
Target box: [] with some more text

The desired output would be:

%This is a comment
Target box: [This is a comment] with some more text

I am trying to do a macro to achieve this (but am quite new to Vim) and I would appreciate your help.

So far I put my cursor anywhere on the commented line:

Esc > 0 > l > SHIFT RIGHT (to select the rest of the line) > y > j > f[ > p

But it doesn't work as I expect. I am using the Vim keybindings in Overleaf as I edit some LaTeX.

1 Answer 1


I would do (assuming you run the macro from Normal mode):




If you plan to run the macro from insert mode.

Where ^[ stand for Esc

  • 0 go to the start of the line
  • l move one character right
  • yg_ yank (copy) till the end of the line not including the carriage return
  • + move to the first non blank character of the next line
  • f[ search for the first [
  • p paste

Remark: But on Overleaf you have to replace yg_ by y$ since the g_ key command is not supported and replaced by y$ (like for VSCode)

  • 1
    Thanks for getting back to me. On my end the yg_ doesn't seem to work - it also copies the next line. Could it be an issue with overleaf?
    – Hugh
    Commented Mar 7 at 4:04
  • 1
    thanks so much, yes y$ works
    – Hugh
    Commented Mar 7 at 4:34
  • 2
    Golf! + is a keystroke shorter than j0
    – Rich
    Commented Mar 7 at 9:47
  • 2
    Even if there were whitespace at the start of the line, we'd want to skip it.
    – Rich
    Commented Mar 7 at 10:58
  • 2
    @Friedrich high fives all round :D
    – Rich
    Commented Mar 7 at 11:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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