Vim: Replacing the line with another, tightened before

At least once a day I have the following situation:

A: This line should also replace line X ... X: This is line should be replaced 

I believe that I am not performing this task effectively.

What am I doing:

  • Go to line A: AG
  • Yank line A: yy
  • Go to line X: XG
  • Insert row A: P
  • Go to old line: j
  • Delete old line: dd

This has the additional drawback that line X is now in the default case, which is annoying if I find another line that should be replaced by A. Janking and pasting from the extra case ( "ayy , " aP ) makes this simple task even less efficient .

My questions:

  • Did I skip the Vim built-in command to replace the line twitching before?
  • If not, how can I bind my own command that leaves (or restores) the yanked string in the default register?
+78
vim
Dec 26 '10 at 10:44
source share
14 answers

What will i do:

  • aG
  • Y
  • xG
  • Vp

You do not need to leave the normal mode, but it pulls the line. However, you can use V"0p , which will always put the line drawn in step 2.

+67
Dec 26 '10 at 13:30
source share

Vp : select row, insert what was pulled

+55
Jan 10 '14 at 20:24
source share

This has the additional drawback that line X is now in the default register mode, which is annoying if I find another line that needs to be replaced with A.

To delete text without affecting regular registers, you can use the black hole case "_ :

 "_dd 
+40
Dec 27 '10 at 9:14
source share

I would use command line mode (Ex) and execute the following two commands

 :XmA :Ad 

It just moves the line X just below A, and then removes the A that line up

for example

 :7m3 :3d 
+8
Jan 28 2018-12-12T00:
source share
  • yoo
  • j (go to the line you want to replace) and then
  • Vp (uppercase v and then p will replace with pulled content)
+7
May 24 '13 at 1:55
source share
  • Move to the beginning of the first line.

  • y , $ - copy the line without ending the line at the end

  • Move to the beginning of the target line.

    • V , p - replace only one target line

    • c , c , Ctrl r , 0 , Esc - replace the target line with the original yank

  • Go to the beginning of the next target line.

  • . - repeats the command issued in 4.2.

Notes:

  • 4.1 - y , $ , because if you execute y , y or y , you copy the line, and Ctrl r , 0 actually adds a linebreak below your target line.

  • 4.2 replaces V p , which does not work with repetition, because technically the last action is deleted, therefore . just delete the line.

  • If someone knows how to release โ€œreplace the current line with a register from EX mode (command line), I would like to hear from you (and know where you found the documentation). There may be a repeatable EX command, which is faster than 4.2, and / or does not have a line outline.

+5
Aug 26 '13 at 5:39 on
source share

You can use this with visual mode.

  • Go to line A: A G
  • Select a line with visual mode: V ESC
  • go to line X: X G
  • Enter a replacement mode for the string: S
  • Paste the copied line: shift + insert (or something else that you matched to paste from the clipboard).
+4
Dec 26 '10 at
source share

Here is what i will do

  • Move beginning of line A, A G (where A is line number, obviously)
  • Yank line to some register, for example. a (no new line). Type " A y $
  • Move to insert line, X G
  • Replace the line, S
  • Paste from register a, Ctrl-R A
+3
Dec 26 '10 at 13:23
source share

You can use these commands in normal mode:

 :AmX | Xd 

the m command is for m[ove] , which moves line number A after line number X, if you want to copy instead of moving line, use co[py] . the d command is for d[elete] .

You can move (copy with co ) a range of lines using

 :start,end m X 
+2
Jun 07 '13 at 9:54 on
source share

Based on the answers that suggest using Vp or Vp to insert over the line - to avoid changing the contents of the yank register, I find that the most ergonomic command is simple:

VPY

+2
Mar 31 '17 at 5:03
source share
  • : ay (where a is the line number. Example: 20y). It holds the line (pun).
  • Vp
+1
Aug 12 '13 at 9:29
source share

I often have to use one line and replace it in several places, each of which has a different meaning (which means that I can not do the regular expression).

Y to take out the desired source string

and then on each line you want to replace, V p zero Y

+1
Oct 21 '14 at 17:25
source share

I find it easier to use the Ex command for this; ex. to move the line from 9 to 46:

  :46|9m.|-1d 

This will move the cursor to line 46, move line 9 below the current one, then delete the previous line (since the moved line is current).

Or using tags (tags) using the 'a' sign:

 :46ma a|9m'a|'ad 
0
Feb 20 '14 at 18:52
source share

I would just use the Black Hole register:

Given:

nnoremap <Cd> "_dd

the solution will be:

<Cd> yy

0
Mar 12 '14 at 5:53
source share



All Articles