0

In the middle of a line, Press v and select multiple lines.
How to yank the word at current cursor from all selected lines and add spaces if the word is shorter than the longest one to make copied columns aligned.
For example, current cursor is in the beginning of second data_out from first line.
Please note below example is in the middle of a file.

     ...
     data_out => data_out,
     read     => read,
     write    => write,
     full     => full,
     notempty => notempty,
     ...

After "copy" I can paste below columns.

data_in ,
data_out,
read ,
write ,
full ,
notempty,

  • Do you really mean yank? Or do you just want to edit the text? – Rich Jan 26 '18 at 15:59
  • @Rich Seems hard to demonstrate tailing spaces, so I add a ','. – Fisher Jan 26 '18 at 17:04
2

You can do it with using block selection (:help 04.4) and A (:help v_b_A_example):

  • <c-q>Gx: use visual block to delete leading space
  • f <c-q>G$x: delete the block from the space to the end of line
  • <c-q>G$A,<esc>: add the comma to the end of each line

The Ctrl+Q above is intended to start block selection -- as it was noted in the comments, the default is Ctrl+V; I'm using the alternative mentioned in :help CTRL-Q.

You could also do it through substitute(:help :su):

  • %s/^\s*//
  • %s/\s=.*$/,/

Edit:

I want it works in visual mode because sometimes the code is not perfectly column aligned. So hope there is an easy way to column copy in the visual mode instead of manually align the code first.

You can do this without changing the code visually; what you really want is to retrieve the number of the lines currently selected:

function! MyIndent()
    silent s/^\s*//
    silent s/\s=.*$/,/
endfunction

vnoremap <leader>i :call MyIndent()<CR>

With this you can select the lines and hit leader (usually \)+i to perform the changes.

  • visual block is normally <c-v> but sometimes <c-q> on windows – Mass Jan 26 '18 at 13:12
  • @Mass nice catch; I've updated the answer. – mMontu Jan 26 '18 at 13:25
  • I want to find a way in visual mode not visual block mode. Gx is selecting all lines. The example I show is a very simple case, it's inside a huge text file. – Fisher Jan 26 '18 at 15:04
  • 1
    @Fisher Why does it have to be visual mode? That's an odd and pretty arbitrary restriction. As for Gx selecting all lines, just use an appropriate movement command that works in your file (which you haven't shown us) instead of G. – Rich Jan 26 '18 at 15:47
  • 1
    @Rich indeed, I'm using behave mswin, which does change 'selection'. And actually I had misread your message -- my approach misses the spaces before the comma. I'm going to update the answer. Thanks! – mMontu Jan 26 '18 at 16:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.