3

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.

codecompleting
  • 9,251
  • 13
  • 61
  • 102
  • Is this a vim question or a visual studio question? – Oded Oct 21 '11 at 19:47
  • @Oded a [viemu](http://www.viemu.com/) question ;-) Not all vi[m] are equal. –  Oct 21 '11 at 19:48
  • @Oded viemu is a commercial port of vim to the VS environment. In some cases, it behaves differently from vim, which is likely why the original poster thought to include it. – Andrew Oct 21 '11 at 19:48
  • @pst - Fair enough. I would never use vim binding in VS... but to each his own. – Oded Oct 21 '11 at 19:49
  • You want to be able to paste the XXX from a register into :s command? – David Poole Oct 21 '11 at 19:52

4 Answers4

4

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;
Magnun Leno
  • 2,728
  • 20
  • 29
1

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

Despo
  • 181
  • 10
  • This is why I prefer emacs ;-) –  Oct 21 '11 at 20:37
  • @pst Never tried emacs but I'm starting to think I should give them a shot. – Despo Oct 21 '11 at 20:49
  • It's just an old lisp dialect with some text editing function/display thrown in -- but it sounds like you are already on the [vi/vim learning curve plateu](http://blogs.msdn.com/b/steverowe/archive/2004/11/17/code-editor-learning-curves.aspx) so no reason to change ;-) Original comment for my "what is that supposed to do!?!?!" reaction. (I use enough vi/elvis to edit configuration files, and that's about it.) –  Oct 21 '11 at 21:41
0

Highlight the four lines in visual mode, then

:'<,'>s/X\+/HELLOWORLD/g
Andrew
  • 14,325
  • 4
  • 43
  • 64
0

Via this question: How do I use vim registers? I found ^R in command mode will paste from a register.

For example, with XXXX highlighted then yanked into the " register: :s/^R"/HELLOWORLD/g

Community
  • 1
  • 1
David Poole
  • 3,432
  • 5
  • 34
  • 34