sponsor Vim development Vim logo Vim Book Ad

basic Tip #66: Transfer text between two Vim 'sessions',

 tip karma   Rating 123/39, Viewed by 4356 

Read and edit this tip on the Vim tip wiki. The wiki may have a more recent version of this tip.

created:   April 20, 2001 13:46      complexity:   basic
author:   Douglas Potts      as of Vim:   5.7

This one is a one of my favorites from Dr. Chip, and I haven't seen it come
across vim tips yet...

Can use either visual, or marking to denote the text.

" transfer/read and write one block of text between vim sessions
" Usage:
" `from' session:
"     ma
"     move to end-of-block
"     xw
"
" `to' session:
"     move to where I want block inserted
"     xr
"
if has("unix")
  nmap xr   :r $HOME/.vimxfer<CR>
  nmap xw   :'a,.w! $HOME/.vimxfer<CR>
  vmap xr   c<esc>:r $HOME/.vimxfer<CR>
  vmap xw   :w! $HOME/.vimxfer<CR>
else
  nmap xr   :r c:/.vimxfer<CR>
  nmap xw   :'a,.w! c:/.vimxfer<CR>
  vmap xr   c<esc>:r c:/.vimxfer<cr>
  vmap xw   :w! c:/.vimxfer<CR>
endif

 rate this tip  Life Changing Helpful Unfulfilling 

<< Insert line number into the actuall text of the file. | Ascii Value >>

Additional Notes

Alexey Marinichev, June 4, 2001 11:17
See also Tip #71.
[email protected], June 4, 2001 21:56



It would be good if this could include a command to remove the .vimxfer file from the buffer list, otherwise it polutes the list and alternative file.  This perhaps negates the benefit since you could just the * buffer as well with only a few extra keystrokes:


(highlight text)
"*y

Then to paste in another file or application:

"*p


supergrass@gmx.net, February 9, 2002 5:43
here is much simple version of it.. however tested on *nix only

"copy and paste betweeen different vim sessions
nmap    _Y      :!echo ""> ~/.vi_tmp<CR><CR>:w! ~/.vi_tmp<CR>
vmap    _Y      :w! ~/.vi_tmp<CR>  
nmap    _P      :r ~/.vi_tmp<CR>


select your text, press CTRL+_Y to copy, then in another (or even the same) vim session press CTRL+_P to paste
[email protected], July 21, 2004 6:38
Hello,

It is specified about how to copy text from one file to another, but what if we want to move a selected piece of text from code or particular text typed at the prompt i.e after :<Some Text>, then how will the process is done. I mean if we want to send the so called <Some Text> into a file for storage then how to we do it?
[email protected], July 25, 2004 6:58
This tip is extremely helpful in Unix shells like bash where you can use fix command 'fc'
stephenm, January 7, 2006 14:04
vimtip #21 seems to solve all of these issues
If you have questions or remarks about this site, visit the vimonline development pages. Please use this site responsibly.
Questions about Vim should go to the maillist. Help Bram help Uganda.
   
SourceForge.net Logo