Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

I'd like to be able to paste from the system clipboard (or text selection) into my "vi-like" shell prompt using the keyboard. I normally use zsh and sometimes bash. In both cases, I have the shell set up with vi-like behaviour (bindkey -v / set -o vi).

In vim, the behaviour I'm looking for is available with the key sequence "+p. This particular key sequence doesn't work as expected in a vi-enabled shell prompt, though. Is there any way to either enable this or a similar behaviour, using keyboard only, while remaining with vi-like keybindings in a zsh or bash shell prompt?

-- edit --

Usage case: I often navigate between Firefox with the Pentadactyl addon, a terminal emulator and vim itself - using the Xmonad window manager with custom keys to move around. All three programs have vi-like keybindings, which is very efficient (for "finger memory") so it would be ideal communicate text between them using vim syntax (or a very similar syntax) only.

share|improve this question

2 Answers 2

up vote 3 down vote accepted

Zsh doesn't support anything but internal registers, and bash doesn't support register at all as far as I know. By and large, shells support vi commands, not vim commands.

In zsh, here's a proof-of-concept for accessing the X selection from command mode. For real use you'd want to elaborate on these techniques. I use the xsel program, you can use xclip instead; see How to copy from one vim instance to another using registers. You'll find the features I used in the zle manual.

vi-append-x-selection () { RBUFFER=$(xsel -o -p </dev/null)$RBUFFER; }
zle -N vi-append-x-selection
bindkey -a '^X' vi-append-x-selection
vi-yank-x-selection () { print -rn -- $CUTBUFFER | xsel -i -p; }
zle -N vi-yank-x-selection
bindkey -a '^Y' vi-yank-x-selection

The function vi-append-x-selection inserts the current X selection after the cursor (similar to p or P). The function vi-yank-x-selection copies the last killed or yanked text to the X selection. zle -N declares the functions as zle widgets (i.e. edition commands). bindkey -a sets bindings for vi command mode.

share|improve this answer
    
This worked perfectly after installing xsel and copying your suggested code with a slight modification to my .zshrc (I chose '^P' instead of '^X', overwriting the original behaviour of '^P') –  Dalker Dec 1 '11 at 6:23

Selection and clipboard are different things under X Window, and IMHO "desktop environments" tend to make the issue even more murky than it was.

Does shift-insert work for you? On bare X applications, it is bound to pasting the selection when such a binding is made.

share|improve this answer
    
Selection and clipboard being different is not really relevant here, a solution that works with one would be easy to adapt to the other. Desktop environments don't change a thing; their spread has accompanied a shift in the balance of power away from selection and towards the clipboard. –  Gilles Dec 1 '11 at 1:41
    
shift-insert does work in my environment, and it's definitely good to know, for use when in a different system. I'm looking however for something closer to actual vim keybindings for yank and/or paste between terminals, vim itself and other programs –  Dalker Dec 1 '11 at 6:20

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.