0

Intro

In gVim under Windows i have to replace insert and mod in each string of file some character in a specifics position.

Example

QWAER;PA 0X000055;123WAAAA
TYUIO;PA 0Y000056;123VAAAA

need to become

QWAE@;PAX000055;123;WAAAA
TYUI@;PAY000056;123;VAAAA

modify char 5 in @ delete char 9,10 insert ; in original pos 22 or after delete in pos 20

More Info

Usually I do

Put cursor and beginning of text to select Press CTRL-V (CTRL-Q in gVim) to begin select of the column keep press SHIFT and selecte the interested area the go at the end of file then do the replace insert or modification. (This is where I learn about Keyboard-only column block selection in GVim Win32, or why does Ctrl-Q not emulate Ctrl-V when mswin.vim is included? and here i learn how to do the insert(http://pivotallabs.com/column-edit-mode-in-vi/)

It's not a correct way to do the things.

In vim i can do the replaceof a fange of rows, and so on using commands.

I think that should be possible to use commands to replace a portion of each string but i have no Knoledge about those command.

this is the main idea

Replacing alternate line in vi text file with a particular string

Question

Is there a way to do the operations using commands with absolute position and not selection.

Thanks

Community
  • 1
  • 1
user1594895
  • 587
  • 1
  • 10
  • 31

1 Answers1

5
:{range}normal 5|r@9|2x20|i;

Does what you want on the lines covered by {range}:

5|r@    " go to column 5 and replace the character with @
9|2x    " go to column 9 and cut 2 characters
20|i;   " go to column 20 and insert a ; to the right

So…

  • :5,25norm 5|r@9|2x20|i; would apply that transformation to lines 5 to 25,

  • :.,$norm 5|r@9|2x20|i; would apply that transformation from the current line to the last,

  • :'<,'>norm 5|r@9|2x20|i; would apply that transformation to the current (or last) visual selection,

  • :'{,'}norm 5|r@9|2x20|i; would apply that transformation to the current "paragraph",

  • :%norm 5|r@9|2x20|i; would apply that transformation to the whole buffer,

  • and so on…

You could also record it, let's say in register q:

qq
5|r@
9|2x
20|i;<Esc>
q

and play it back on {range}:

:{range}@q

Or spend 30 minutes trying to come up with the right :s// command…


Reference:

:help range
:help :normal
:help |
:help r
:help x
:help i
:h recording
romainl
  • 186,200
  • 21
  • 280
  • 313