Vim Tips Wiki
Register
m (Tracking Clipboard Changes moved to Tracking clipboard changes: Page moved by JohnBot to improve title)
(Change <tt> to <code>, perhaps also minor tweak.)
 
(6 intermediate revisions by 2 users not shown)
Line 1: Line 1:
  +
{{TipImported
{{review}}
 
{{Tip
 
 
|id=1370
 
|id=1370
  +
|previous=1369
|title=Tracking Clipboard Changes
 
  +
|next=1373
|created=October 26, 2006 4:46
+
|created=2006
 
|complexity=intermediate
 
|complexity=intermediate
 
|author=Marian Csontos
 
|author=Marian Csontos
 
|version=5.7
 
|version=5.7
 
|rating=93/24
 
|rating=93/24
  +
|category1=
|text=
 
  +
|category2=
Do you ever wanted to append more lines from various sources (or to copy data from some dull application without any reasonable export method) to one file?
 
 
 
 
Here is pretty simple solution for Vim. All you need is to copy all you need to clipboard and Vim do the rest...
 
 
Of course first you will need to start vim, open desired buffer (named buffer required as script do autosave) and run TrackClipboard function
 
 
:call TrackClipboard('==============================================================')
 
 
 
 
Function monitors clipboard and on its change add data to clipboard (or do whatever you want).
 
 
 
 
function! TrackClipboard(delim)
 
 
let timer1 = 0
 
 
let temp = @*
 
 
while @*!='EOF'
 
 
if temp != @*
 
 
let temp = @*
 
 
$put =a:delim
 
 
$put =temp
 
 
else
 
 
" autowrite and redraw every cca 5s
 
 
let timer1 += 1
 
 
if timer1 &gt;= 100
 
 
if &amp;modified
 
 
write
 
 
redraw
 
 
endif
 
 
let timer1 = 0
 
 
else
 
 
sleep 50ms
 
 
endif
 
 
endif
 
 
endwhile
 
 
endfunction
 
 
 
 
}}
 
}}
 
Do you ever wanted to append lines from various sources (or to copy data from some application without any reasonable export method) to one file?
   
 
Here is a pretty simple solution for Vim. Just copy all you need to the clipboard and Vim can do the rest.
== Comments ==
 
Why when I inserting text from X11 selection into vim, each line gets extra tabs and spaces?
 
For example,
 
   
 
First you need to start Vim, open desired buffer (named buffer required as script do autosave) and run TrackClipboard function:
static int matcher(char* s, int n, regex_t* r ) {
 
int i = 0;
 
for ( i = 0; i &lt; n ; i++ )
 
if ( !regexec(&amp;(r[i]), s, 0, NULL, 0) ) return 1;
 
return 0;
 
}
 
   
  +
<pre>
Becomes:
 
 
:call TrackClipboard('==============================================================')
static int matcher(char* s, int n, regex_t* r ) {
 
  +
</pre>
int i = 0;
 
for ( i = 0; i &lt; n ; i++ )
 
if ( !regexec(&amp;(r[i]), s, 0, NULL, 0) ) return 1;
 
return 0;
 
}
 
   
 
The function monitors the clipboard; when it is changed, add data to clipboard (or do whatever you want).
Any ideas what causes this?
 
   
  +
<pre>
a--AT--a
 
 
function! TrackClipboard(delim)
, October 27, 2006 6:56
 
 
let timer1 = 0
----
 
 
let temp = @*
Looks like it's autoindenting. Maybe you want something like ":set paste" in there somewhere?
 
 
while @*!='EOF'
 
if temp != @*
 
let temp = @*
 
$put =a:delim
 
$put =temp
  +
else
 
" autowrite and redraw every cca 5s
 
let timer1 += 1
 
if timer1 >= 100
 
if &modified
  +
write
  +
redraw
  +
endif
 
let timer1 = 0
  +
else
 
sleep 50ms
  +
endif
  +
endif
  +
endwhile
 
endfunction
  +
</pre>
   
  +
==Related scripts==
'''Anonymous'''
 
  +
*{{script|id=3246|text=CaptureClipboard}} contains a greatly enhanced plugin that is based on this tip. It offers feedback on the number of captures, trimming of white space, append as well as prepend not just to the end of the buffer, arbitrary delimiters and many configurable defaults.
, October 27, 2006 7:18
 
----
 
Great ":set paste" worked, thank you! Another thing why tabs get replaced with spaces? Is this something X11 does or VIM?
 
   
 
==Comments==
  +
<pre>
 
" to automatically save modified buffer, and simplify your function.
  +
:set autosave
  +
</pre>
  +
:The 'autosave' option is in the todo list, but as of Vim 7.3.000 not yet implemented. -- [[User:Inkarkat|Inkarkat]] 13:19, September 20, 2010 (UTC)
   
'''Anonymous'''
 
, October 27, 2006 7:31
 
 
----
 
----
  +
Use <code>:set paste</code> to avoid autoindent applying to pasted text, in a terminal.
Just checked in another editor, if I do X11 selection for another editor and paste in VIM tabs stay tabs, but from VIM to VIM they get replaced with spaces.
 
   
  +
Also, you may need <code>:set noexpandtab</code> to stop Vim from expanding tabs to spaces.
'''Anonymous'''
 
, October 27, 2006 7:34
 
----
 
Try :set noexpandtab to stop vim from expanding tabs to spaces. If you want it to expand tabs (which I do for most coding) you can force a tab by using 'CTRL-V tab'
 
   
  +
:This is not necessary; the :put command that is used in the function already inserts the text literally. -- [[User:Inkarkat|Inkarkat]] 13:19, September 20, 2010 (UTC)
'''Anonymous'''
 
, October 30, 2006 3:48
 
----
 
:set autosave " to automatically save modified buffer, and simplify your function.
 
   
Indira
 
, October 30, 2006 16:52
 
 
----
 
----
<!-- parsed by vimtips.py in 0.617637 seconds-->
 

Latest revision as of 06:23, 13 July 2012

Tip 1370 Printable Monobook Previous Next

created 2006 · complexity intermediate · author Marian Csontos · version 5.7


Do you ever wanted to append lines from various sources (or to copy data from some application without any reasonable export method) to one file?

Here is a pretty simple solution for Vim. Just copy all you need to the clipboard and Vim can do the rest.

First you need to start Vim, open desired buffer (named buffer required as script do autosave) and run TrackClipboard function:

:call TrackClipboard('==============================================================')

The function monitors the clipboard; when it is changed, add data to clipboard (or do whatever you want).

function! TrackClipboard(delim)
  let timer1 = 0
  let temp = @*
  while @*!='EOF'
    if temp != @*
      let temp = @*
      $put =a:delim
      $put =temp
    else
      " autowrite and redraw every cca 5s
      let timer1 += 1
      if timer1 >= 100
        if &modified
          write
          redraw
        endif
        let timer1 = 0
      else
        sleep 50ms
      endif
    endif
  endwhile
endfunction

Related scripts[]

  • CaptureClipboard contains a greatly enhanced plugin that is based on this tip. It offers feedback on the number of captures, trimming of white space, append as well as prepend not just to the end of the buffer, arbitrary delimiters and many configurable defaults.

Comments[]

" to automatically save modified buffer, and simplify your function.
:set autosave
The 'autosave' option is in the todo list, but as of Vim 7.3.000 not yet implemented. -- Inkarkat 13:19, September 20, 2010 (UTC)

Use :set paste to avoid autoindent applying to pasted text, in a terminal.

Also, you may need :set noexpandtab to stop Vim from expanding tabs to spaces.

This is not necessary; the :put command that is used in the function already inserts the text literally. -- Inkarkat 13:19, September 20, 2010 (UTC)