Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How would you do a cut and paste with this in VIM?

Say you had this text:

SOMETHING_XXXXXXXXXXXXXX_ELSE
SOMETHING_XXXXXXXXXXXXXX_ELSE2
SOMETHING_XXXXXXXXXXXXXX_ELSE3
SOMETHING_XXXXXXXXXXXXXX_ELSE4

And you wanted to replace all XXX..XXX with this word:

HELLOWORLD

If I go into visual mode, then yank the word, how could I then replace the XXX..XXX in the 4 lines above using cut and paste?

If I try, what happens is the X gets into my 'clipboard' and then I'm stuck to just typing it out manually.

like image 643
codecompleting Avatar asked Dec 05 '25 14:12

codecompleting


2 Answers

I'm not sure if it will work in viemu, but in VIM you can do the following...

Using Yank and Paste

Yank the text to a specific register. Select the text in visual mode and use the command "ay to yank the text to the register a. Then when pasting call the command "ap, which pastes the contents of the a register.

Using Normal Command

But I would strongly prefer to use the normal command. Just select the lines

SOMETHING_XXXXXXXXXXXXXX_ELSE
SOMETHING_XXXXXXXXXXXXXX_ELSE2
SOMETHING_XXXXXXXXXXXXXX_ELSE3
SOMETHING_XXXXXXXXXXXXXX_ELSE4

using line visual mode (<C-v>) and then issue this command: :'<,'>normal fXct_HELLOWORLD. Then you'll have

SOMETHING_HELLOWORLD_ELSE
SOMETHING_HELLOWORLD_ELSE2
SOMETHING_HELLOWORLD_ELSE3
SOMETHING_HELLOWORLD_ELSE4

This means that it will run the command fXct_HELLOWORLD for each line. Let me explain the command:

  • fX - moves the cursor until the first X;
  • ct_ - deletes everything untill _ and puts you in insert mode;
  • HELLOWORLD - the word which will substitute XXXXXXXXXXXXXX;
like image 193
Magnun Leno Avatar answered Dec 07 '25 03:12

Magnun Leno


One way would be to visually select all the code you want to replace and change it at once

Ctrl+v 3jt_cHELLOWORLD[Esc]

Note: it takes a couple of seconds for all lines to be updated

Another way to be by creating a macro:

record macro:

q10fXct_HELLOWORLD[esc]q

run macro on other lines:

j@1j@1j@1

q1 records a macro on character 1

@1 replays macro

But search and replace is a good alternative for your question

like image 21
Despo Avatar answered Dec 07 '25 04:12

Despo



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!