0

I just learned about the shortcuts that allow you to manipulate ranges, e.g. :+5,+10co. to copy after the current line.

I wondered if there is a way to copy and move at the start of the current line?

Currently I have to move the cursor at the end of the preceding line in order to attain the desired result.

To illustrate, given the following file, with the cursor being at line 5:

  3 A
  4 B
→ 5
  6 C
  7 D

Applying a copy via :+1,+2co. results in the undesired empty line at line 5:

  3 A
  4 B
  5 
  6 C
→ 7 D 
  8 C
  9 D

Is there a way to copy at the start of the current line or is one limited to perform this command at the end of the preceding line?

  • a way to copy and move at the start of the current line That does not make sense for linewise (Ex) commands: "copy" can add after or before the current line. But it does not replace existing lines (even empty ones). Either don't create unneeded empty lines, or delete them manually, or use "normal" commands. – Matt Nov 17 at 11:17
  • 1
    The operation you describe is the equivalent of selecting lines of text (i.e. including the line endings...CR or CRLF) in a gui/wysiwyg editor, hitting Ctrl-C (copy), placing the cursor on line 5, and hitting Ctrl-V (paste). Does that put the text on line 5 after the cursor? Nope. It puts it on the next line, leaving line 5 blank. As Matt indicates, you need characterwise operations and Normal (or Visual) mode is better suited for that then Command (line) mode. – B Layer Nov 17 at 12:29
  • 1
    An example of characterwise but multiline operation (while in Normal mode): g_ is a motion to the last non-blank character of the current line and [count-1] lines downward. So if we do y2g_ we'll do a characterwise yank of two lines. Then we could do 5Gp to move cursor to line 5 and paste yanked text after it. IOW, one might find it easier to remove line 5 first and use your original commands... ;) – B Layer Nov 17 at 12:44
  • 2
    You had one line then you've inserted two lines more. Now you have three lines in total. That's very intuitive, IMO. Just don't create that empty line and do +1,+2co-1 or something like that. – Matt Nov 17 at 12:47
  • 1
    Cool. No problem. (I'm guessing a lot of users are not familiar with that one. :) – B Layer Nov 19 at 15:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.