Vim Tips Wiki
(Move categories to tip template)
No edit summary
(6 intermediate revisions by 5 users not shown)
Line 1: Line 1:
{{review}}
 
 
{{TipImported
 
{{TipImported
 
|id=829
 
|id=829
 
|previous=828
 
|previous=828
 
|next=830
 
|next=830
|created=December 6, 2004
+
|created=2004
 
|complexity=basic
 
|complexity=basic
 
|author=Shin Seung Woo
 
|author=Shin Seung Woo
Line 12: Line 11:
 
|category2=
 
|category2=
 
}}
 
}}
  +
{{Dodgy|The normal way of [[Accessing the system clipboard|accessing the system clipboard]] should work fine for most users under most situations. Perhaps not this situation, but it should be made clear that this tip is a workaround for an abnormal use case.}}
 
I use putty + vim + screen on web application developing. I use more than ten working directories, so I couldn't find an easy way to develop in only one vim session.
 
I use putty + vim + screen on web application developing. I use more than ten working directories, so I couldn't find an easy way to develop in only one vim session.
   
So, copy & paste between session, usually used Window's clipboard. but, to copy longer than one screen, It's really hard. yes, there are auto-indenting problem too.
+
So, copy & paste between session, usually used Window's clipboard. but, to copy longer than one screen, It's really hard. yes, there are auto-indenting problem too.
   
 
And sometimes, I just want copy function definition or long variable names. between vim buffers I simply use visual selection. or cw/ciw. in this case, yanking by line is worse than clipboard.
 
And sometimes, I just want copy function definition or long variable names. between vim buffers I simply use visual selection. or cw/ciw. in this case, yanking by line is worse than clipboard.
Line 22: Line 22:
 
* ,p / ,P read from ~/reg.txt and paste using p/P.
 
* ,p / ,P read from ~/reg.txt and paste using p/P.
   
In visual mode, <tt>,y</tt> is yank and save.
+
In visual mode, <code>,y</code> is yank and save.
   
 
<pre>
 
<pre>
vmap &lt;silent&gt; ,y y:new&lt;cr&gt;:call setline(1,getregtype())&lt;cr&gt;o&lt;esc&gt;P:wq! ~/reg.txt&lt;cr&gt;
+
vmap <silent> ,y y:new<CR>:call setline(1,getregtype())<CR>o<Esc>P:wq! ~/reg.txt<CR>
nmap &lt;silent&gt; ,y :new&lt;cr&gt;:call setline(1,getregtype())&lt;cr&gt;o&lt;esc&gt;P:wq! ~/reg.txt&lt;cr&gt;
+
nmap <silent> ,y :new<CR>:call setline(1,getregtype())<CR>o<Esc>P:wq! ~/reg.txt<CR>
map &lt;silent&gt; ,p :sview ~/reg.txt&lt;cr&gt;"zdddG:q!&lt;cr&gt;:call setreg('"', @", @z)&lt;cr&gt;p
+
map <silent> ,p :sview ~/reg.txt<CR>"zdddG:q!<CR>:call setreg('"', @", @z)<CR>p
map &lt;silent&gt; ,P :sview ~/reg.txt&lt;cr&gt;"zdddG:q!&lt;cr&gt;:call setreg('"', @", @z)&lt;cr&gt;P
+
map <silent> ,P :sview ~/reg.txt<CR>"zdddG:q!<CR>:call setreg('"', @", @z)<CR>P
 
</pre>
 
</pre>
   
Line 34: Line 34:
 
You can use the selection and drop registers to copy between sessions.
 
You can use the selection and drop registers to copy between sessions.
   
For example, <tt>"*yy</tt> will copy the current line to the clipboard, then <tt>"*p</tt> will put whatever is on the clipboard in the document.
+
For example, <code>"*yy</code> will copy the current line to the clipboard, then <code>"*p</code> will put whatever is on the clipboard in the document.
   
 
You can use any normal motion command or visual selection when yanking. See {{help|registers}}.
 
You can use any normal motion command or visual selection when yanking. See {{help|registers}}.
  +
----
 
  +
You can use <code>:set paste</code> and <code>:set nopaste</code> to tell vim to not autoindent or otherwise screw with your pasted text.
 
----
 
----
 
I'm using Vim on local computer and * register somehow des not work for me. Neither was this mapping, probably because of my vim settings. After some reworking I got something what works for me.
 
I'm using Vim on local computer and * register somehow des not work for me. Neither was this mapping, probably because of my vim settings. After some reworking I got something what works for me.
Line 45: Line 46:
 
<pre>
 
<pre>
 
let g:session_yank_file="~/.vim_yank"
 
let g:session_yank_file="~/.vim_yank"
map &lt;silent&gt; &lt;Leader&gt;y :call Session_yank()&lt;cr&gt;
+
map <silent> <Leader>y :call Session_yank()<CR>
vmap &lt;silent&gt; &lt;Leader&gt;y y:call Session_yank()&lt;cr&gt;
+
vmap <silent> <Leader>y y:call Session_yank()<CR>
vmap &lt;silent&gt; &lt;Leader&gt;Y Y:call Session_yank()&lt;cr&gt;
+
vmap <silent> <Leader>Y Y:call Session_yank()<CR>
nmap &lt;silent&gt; &lt;Leader&gt;p :call Session_paste("p")&lt;cr&gt;
+
nmap <silent> <Leader>p :call Session_paste("p")<CR>
nmap &lt;silent&gt; &lt;Leader&gt;P :call Session_paste("P")&lt;cr&gt;
+
nmap <silent> <Leader>P :call Session_paste("P")<CR>
   
 
function Session_yank()
 
function Session_yank()
Line 72: Line 73:
 
endfunction
 
endfunction
 
</pre>
 
</pre>
  +
  +
---
  +
  +
It is ''much'' easier to do this. It has limitations, like hogging the x register and only yanks in visual mode, but it'll work.
  +
<pre>
  +
:vmap <silent> ,y "xy<CR>:wviminfo! ~/.viminfo
  +
:nmap <silent> ,p :rviminfo! ~/.viminfo<CR>"xp
  +
</pre>
  +
  +
---
  +
  +
If you're super lazy (like me) and you're working in bash or some such nice terminal...just select and copy/paste using the terminal commands (select with mouse, edit-copy, edit-paste). I know, its a cop-out for command line editing, but it's freaking easy and it works.

Revision as of 03:44, 23 April 2014

Tip 829 Printable Monobook Previous Next

created 2004 · complexity basic · author Shin Seung Woo · version 6.0


I use putty + vim + screen on web application developing. I use more than ten working directories, so I couldn't find an easy way to develop in only one vim session.

So, copy & paste between session, usually used Window's clipboard. but, to copy longer than one screen, It's really hard. yes, there are auto-indenting problem too.

And sometimes, I just want copy function definition or long variable names. between vim buffers I simply use visual selection. or cw/ciw. in this case, yanking by line is worse than clipboard.

Last night I made this script saving current register to file supporting visual selection too.

  • ,y saves current unnamed buffer to ~/reg.txt file.
  • ,p / ,P read from ~/reg.txt and paste using p/P.

In visual mode, ,y is yank and save.

vmap <silent> ,y y:new<CR>:call setline(1,getregtype())<CR>o<Esc>P:wq! ~/reg.txt<CR>
nmap <silent> ,y :new<CR>:call setline(1,getregtype())<CR>o<Esc>P:wq! ~/reg.txt<CR>
map <silent> ,p :sview ~/reg.txt<CR>"zdddG:q!<CR>:call setreg('"', @", @z)<CR>p
map <silent> ,P :sview ~/reg.txt<CR>"zdddG:q!<CR>:call setreg('"', @", @z)<CR>P

Comments

You can use the selection and drop registers to copy between sessions.

For example, "*yy will copy the current line to the clipboard, then "*p will put whatever is on the clipboard in the document.

You can use any normal motion command or visual selection when yanking. See :help registers.


You can use :set paste and :set nopaste to tell vim to not autoindent or otherwise screw with your pasted text.


I'm using Vim on local computer and * register somehow des not work for me. Neither was this mapping, probably because of my vim settings. After some reworking I got something what works for me.

Moreover, it should have the filename defined only on one place, it should not modify any registers beyond ", and it should delete the temporary buffer created.

let g:session_yank_file="~/.vim_yank"
map <silent> <Leader>y :call Session_yank()<CR>
vmap <silent> <Leader>y y:call Session_yank()<CR>
vmap <silent> <Leader>Y Y:call Session_yank()<CR>
nmap <silent> <Leader>p :call Session_paste("p")<CR>
nmap <silent> <Leader>P :call Session_paste("P")<CR>

function Session_yank()
  new
  call setline(1,getregtype())
  put
  silent exec 'wq! ' . g:session_yank_file
  exec 'bdelete ' . g:session_yank_file
endfunction

function Session_paste(command)
  silent exec 'sview ' . g:session_yank_file
  let l:opt=getline(1)
  silent 2,$yank
  if (l:opt == 'v')
    call setreg('"', strpart(@",0,strlen(@")-1), l:opt) " strip trailing endline ?
  else
    call setreg('"', @", l:opt)
  endif
  exec 'bdelete ' . g:session_yank_file
  exec 'normal ' . a:command
endfunction

---

It is much easier to do this. It has limitations, like hogging the x register and only yanks in visual mode, but it'll work.

:vmap <silent> ,y "xy<CR>:wviminfo! ~/.viminfo
:nmap <silent> ,p :rviminfo! ~/.viminfo<CR>"xp

---

If you're super lazy (like me) and you're working in bash or some such nice terminal...just select and copy/paste using the terminal commands (select with mouse, edit-copy, edit-paste). I know, its a cop-out for command line editing, but it's freaking easy and it works.