Is there a way to apply the conversion of sed to a vim clipboard before copying it to the clipboard?

I use the following command to copy all lines of text in a document to the system clipboard:

%y+ 

Usually, in order to copy the code to StackOverflow;), I apply the sed conversion to my buffer to make pasting easier with MarkDown:

 %s:^:\t:g 

Is there a way to link commands without actually applying it in my buffer, only for copied text?

+4
source share
3 answers

I suggest using the CLI utility to put it on the clipboard: there are several that I found earlier, but here is one:

So you would do

 :%!sed 's:^:\t:g`|xclip 

or

 :%!sed 's:^:\t:g`|xclip -selection c 

the latter uses the X clipboard instead of the main clipboard (assuming UNIX).

The windows have similar similar utilities.

Edit

A clean vim solution would be:

 :let @+=substitute(join(getbufline("%", 1, "$"), "\r\n"), "^\\|\n", "\&\t", "g") 

Notes:

  • it's not very efficient (but you use its SO messaging ... so it's not Homer Oddyssee)
  • I assume that you want windows strings to end (which is what I get when copying from SO anyway).
+5
source

If you do not mind adding an entry to the cancellation list (which means that you are actually editing the contents of the buffer), you can perform the replacement, unload the text, and cancel this replacement in one command.

 :%s/^/\t/|%y+|u 

Another solution would be to make the correct replacement in the contents of register + immediately after copying.

 :%y+| let@ +=substitute(@+,'^\|\n\zs','\t','g') 
+3
source

If shiftwidth is 4 and expandtab set, I would do:

 :set guioptions+=a ggVG>gv 

7 keystrokes are not so bad. Of course there is no ex command. If you want ex commands you can do:

 function! ToSo() %y + let @+ = " " . substitute(@+, '\n', "\n ", 'g') endfunction command! -nargs=0 ToSo :call ToSo()<Enter> 

And then:

 :ToSo 

will put everything you want on the clipboard

+2
source

All Articles