Apply function before pasting

15 views
Skip to first unread message

statquant2

unread,
Nov 15, 2010, 1:34:26 PM11/15/10
to v...@vim.org

Hi guys,
I am trying to find a mean to apply a function just before pasting, and then
after pasting another one.
In fact it is even simpler, I want to apply set paste before pasting and set
nopaste after it.

Sorry if this question is easy but I can't find an easy way.
Thanks for reading

Cheers
--
View this message in context: http://vim.1045645.n5.nabble.com/Apply-function-before-pasting-tp3266183p3266183.html
Sent from the Vim - General mailing list archive at Nabble.com.

sc

unread,
Nov 15, 2010, 1:56:36 PM11/15/10
to vim...@googlegroups.com
On Monday 15 November 2010 12:34:26 statquant2 wrote:

> Hi guys,
> I am trying to find a mean to apply a function just before
> pasting, and then after pasting another one.
> In fact it is even simpler, I want to apply set paste before
> pasting and set nopaste after it.

how about

nmap <S-Insert> :set paste<CR>"+gP\|:set nopaste<CR>
imap <S-Insert> <C-O>:set paste<CR><C-R>+\|<C-O>:set nopaste<CR>

the above two mappings started out as two lines, may be more
by the time you see them

sc

ZyX

unread,
Nov 15, 2010, 1:59:58 PM11/15/10
to vim...@googlegroups.com
Reply to message «Apply function before pasting»,
sent 21:34:26 15 November 2010, Monday
by statquant2:

> I am trying to find a mean to apply a function just before pasting, and
> then after pasting another one.
> In fact it is even simpler, I want to apply set paste before pasting and
> set nopaste after it.

I do not know any `PastePre' and `PastePost' events, so creating custom
mapping is probably the only way to do it:

nnoremap p :set paste \| try \| let savedureg=@" \| let @"=getreg(v:register) \| execute
'normal! p' \| finally \| let @"=savedureg \| set nopaste \| endtry<CR>

Note that I used combination with saving and restoring @" register just in order
to be able to use expression register (see ``:h quote_=''). This can be simplified
if you don't use it.

Mapping for `P' is similar, but you will need to replace `p' with `P' in {lhs} and
`normal' command.

signature.asc

statquant2

unread,
Nov 16, 2010, 8:39:43 AM11/16/10
to v...@vim.org

Hi guys,
thank you very much for the answer, however this is not working because I
forgot to mention that I paste from the mouse, basically I hit the middle
button and It paste what is in the mouse.
Your stuff are working for (from what I understand) the content of the
current buffer (what I would have yanked...)

Sorry about this, would you be able to suggest something else with this new
information ?
Thank you for yout time guys

ZyX

unread,
Nov 16, 2010, 9:50:08 AM11/16/10
to vim...@googlegroups.com
Reply to message «Re: Apply function before pasting»,
sent 16:39:43 16 November 2010, Tuesday
by statquant2:

It is not possible in terminal vim on most configurations because middle click
is handled by terminal itself. If you are using gvim, then try to replace {lhs}
of my answer with `<MiddleMouse>'.

Original message:

signature.asc

Benjamin R. Haskell

unread,
Nov 16, 2010, 12:15:56 PM11/16/10
to vim...@googlegroups.com
On Tue, 16 Nov 2010, ZyX wrote:

> Reply to message «Re: Apply function before pasting»,
> sent 16:39:43 16 November 2010, Tuesday
> by statquant2:
>

> Original message:
>> Hi guys,
>> thank you very much for the answer, however this is not working because I
>> forgot to mention that I paste from the mouse, basically I hit the middle
>> button and It paste what is in the mouse.
>> Your stuff are working for (from what I understand) the content of the
>> current buffer (what I would have yanked...)
>>
>> Sorry about this, would you be able to suggest something else with
>> this new information ?
>> Thank you for yout time guys
>
> It is not possible in terminal vim on most configurations because
> middle click is handled by terminal itself. If you are using gvim,
> then try to replace {lhs} of my answer with `<MiddleMouse>'.

Generally, only shift+mouse-button is handled by the terminal (this lets
the user prevent the application running in the terminal from hiding
useful functionality). In XTerm and derivatives or act-alikes, Vim
should receive middle mouse clicks.

If you set up your mouse mode properly, you shouldn't have to bother
with complicated mappings for pasting. See the help for:

:help 'mouse'
:help 'ttymouse'

In recent uxterm and rxvt-unicode:

:set mouse=a ttymouse=xterm2

allows non-indentation-mangling middle-click pasting for me. (middle
click alone, not shift+middle)

Certain patchlevels of XTerm proper have issues surrounding "bracketed
paste mode"[1], where the default settings with some distributions set
allowWindowOps to false (which has the side effect of preventing
bracketed paste mode from working). Fuller explanation starting at "Due
to the long history..." in my response in the thread "paste in vim
creates stupid result"[2].

[1] http://www.xfree86.org/current/ctlseqs.html#Bracketed%20Paste%20Mode
[2] http://groups.google.com/group/vim_use/browse_thread/thread/876dcfc818c78f/f101dc3d6b35f33c#f101dc3d6b35f33c

If you're using XTerm proper, try the settings above (mouse,ttymouse),
and if that still doesn't work, Ctrl+right-click on the xterm and choose
"Allow Window Ops" and try again.

If you're not using a terminal emulator that uses xterm-like mouse
handling, you might be out of luck (but most do).

--
Best,
Ben

ZyX

unread,
Nov 16, 2010, 1:01:07 PM11/16/10
to vim...@googlegroups.com
Reply to message «Re: Apply function before pasting»,
sent 20:15:56 16 November 2010, Tuesday
by Benjamin R. Haskell:

> If you set up your mouse mode properly, you shouldn't have to bother
>
> with complicated mappings for pasting. See the help for:
> :help 'mouse'
> :help 'ttymouse'
>
> In recent uxterm and rxvt-unicode:
> :set mouse=a ttymouse=xterm2
>
> allows non-indentation-mangling middle-click pasting for me. (middle
> click alone, not shift+middle)

Thanks, I thought that using mouse in terminal is more complex. Still had to add
a set of mappings:
nnoremap <MiddleMouse> "*p
vnoremap <MiddleMouse> d"*p
xnoremap <MiddleMouse> <C-g>d"*p
lnoremap <MiddleMouse> <C-r><C-r>*
inoremap <MiddleMouse> <C-\><C-o>"*p
onoremap <MiddleMouse> <Nop>
because I want to have <MiddleMouse> act like keyboard button (I do not want to
position mouse in place where I want to paste).

signature.asc

Benjamin R. Haskell

unread,
Nov 16, 2010, 1:55:49 PM11/16/10
to vim...@googlegroups.com
On Tue, 16 Nov 2010, ZyX wrote:

I also dislike that middle-click pasting moves the cursor (usually I'm
just trying to lob text from one window into another, so requiring a
precise motion is irritating). Apparently I've trained myself to always
enter insert-mode first (in insert mode, the cursor isn't moved when
click-pasting). There's a full table of when the cursor is moved, etc,
at:

:help mouse-overview

I think that makes the inoremap unnecessary, but I'll probably steal
your others there (especially the 'v' and 'o' maps).

--
Best,
Ben

Joan Miquel Torres Rigo

unread,
Nov 17, 2010, 4:27:30 AM11/17/10
to vim...@googlegroups.com
2010/11/16 statquant2 <stat...@gmail.com>:

>
> Hi guys,
> thank you very much for the answer, however this is not working because I
> forgot to mention that I paste from the mouse, basically I hit the middle
> button and It paste what is in the mouse.
> Your stuff are working for (from what I understand) the content of the
> current buffer (what I would have yanked...)
>
> Sorry about this, would you be able to suggest something else with this new
> information ?

This is not exactly what you asked for, but is useful for me:

" F11 -> Invert list markers and paste mode: {{{
noremap <F11> :set<space>invlist<enter>:set<space>invpaste<enter>
imap <F11> <esc><F11>a
" }}}


This acts as 'two-way' copy & paste mode switching between paste and
nopaste mode and list and no list (to be able to copy from vim with
mouse without listchars marks) by pressing single key (I choose F11).

Notice that second map (for insert mode) only works "first time" (when
you are in nopaste mode) because in paste mode is interpreted as 'raw'
character to insert.

I didn't thought yet about any trick to fix it.


Regards.

--
Joan Miquel Torres__________________________________
Linux Registered User #164872
http://www.mallorcaweb.net/joanmiquel
BULMA: http://bulma.net http://breu.bulma.net/?l2301

Reply all
Reply to author
Forward
0 new messages