开发者

VIM how to select/copy column n from line x to y

I use visual block to copy a specific column in some ranges (say lines x to y). I wonder if there is a simpler way to accomplish the same task.

Also, after I copy those lines, I would like to cut and paste them horizontally. Is it possible? It's sort of annoying to do J, especially if the number of lines is large.

For example, File A (btw both files are open in the split window) is

............
开发者_如何转开发sadsa asds  dog 
sadsa asds  cat
sadsa asds  me
sadsa asds  you
...........

And I want to copy 4 rows in the 3rd column to File B, which may already contain data. Say I want to copy this data on line 30, this way (line numbers are hypothetical):

   ...............
   29.  other data
   30.  dog cat me you
   31.  other data
   ................


Copy the text from file A with visual bock mode by hitting Ctrl-V at the top left corner of the text chunk then moving the cursor to opposite corner of the text chunk then hit y. Move to file B where you want to paste your chunk of text, then execute the following:

:put
:'[,']join

:put will paste text from a register in line mode, including visual block yanked text.

:join will join lines together just like the J command.

'[ and '] are marks set at the beginning and ending of a change.

:'[,']join will join from the beginning of pasted text to the end of the pasted text.

For more information see:

:h :put
:h :j


I would paste it and use 4J, 5J, 20J etc. depending on how many lines there are. Is this still too annoying to do?

EDIT: Ahh, I see how this would be pretty painful. I played around with this a bit and made a macro that eases the pain quite a bit. It essentially automates what MBO suggests below.

moGo^[pVGJdd'op

Paste that into a vim buffer, replace ^[ with an actual escape character (With default keybindings, go in insert mode, press CTRL-v, then press ESC.

Now highlight that macro, and press "py

Now you can play the macro with @p

Copy the vertical 'dog cat me you' string from one file, go into the other and position the cursor where you want it, then push @p and it should work!

RE-EDIT: Peter Rincker's answer is far better than mine, and should be the accepted answer.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜