Yanking and pasting from system's buffer within bash's vi mode

polendina

New Member
Joined
Dec 3, 2021
Messages
5
Reaction score
0
Credits
51
I've recently switched to bash's vi mode after being reluctant and i'm currently facing two issues , namely not being able to use the system register for my cut,copy and pate operations with text between the command line and other applications . The only way to use the system register is while i'm at the insert mode with the good old ctrl+shift+v which seems to be defeating the purpose of using using vi mode for the sole purpose of its bindings being centered around the home row .
Is there a way to paste using p or P within the command mode at bash GNU readline ?
 
Last edited:


Not exactly sure what you mean, but try this..

#> cat mytext.file
line1
line2
line3
line4
line5

vi mytext.file

go down to line 2. Press ESC, then 2 (this "grabs" 2 lines of text, you could use a 3 or a 4 )
then type "yy" (without the quotes) this "yanks" whatever is on the 2 lines below the cursor.
Now use the arrow key to move the cursor down to line 5.
type "p" (without the quotes) you should see lines 3 and 4 copied below line 5 now.

Is this what you mean?
 
Not exactly sure what you mean, but try this..

#> cat mytext.file
line1
line2
line3
line4
line5

vi mytext.file

go down to line 2. Press ESC, then 2 (this "grabs" 2 lines of text, you could use a 3 or a 4 )
then type "yy" (without the quotes) this "yanks" whatever is on the 2 lines below the cursor.
Now use the arrow key to move the cursor down to line 5.
type "p" (without the quotes) you should see lines 3 and 4 copied below line 5 now.

Is this what you mean?
No, the op is talking about using vi keybinds in readline, which is used by bash to get input from users. The default setting uses emacs style bindings.
But if you use the following in your .bashrc:
Code:
set -o vi
It switches to use vi style keybinds.

I’m about to head to work, so I’ll get back to the op with more info later!
 
Typically in vi/vim, to paste from the system copy/paste buffer you paste from a register like + or *.
e.g.
In command mode, you paste from the system buffer like this: "+p
In edit mode, like this: <c-r> +
Where <c-r> is the ctrl and r keybind and + is the + key.

Unfortunately in bash, when you switch to vi mode, the default keybind to paste from the system copy/paste buffer is shift and insert.
e.g. hold the <shift> key and press the <insert> key.

I've been using vi mode in bash for many years, but I've always mostly stuck with the default key-binds.
It IS possible to customise the keybinds. The only key-bind I currently override is ctrl and l to clear the screen (which is the default binding to clear the screen in emacs mode).
So in other words, in order to use ctrl+l to clear the screen in vi-edit mode, in bash - I add the following setting to my .bashrc:
bind -m vi-insert "\C-l":clear-screen
The above tells the shell to use ctrl + l as the keybind for the clear-screen functionality in readline's vi-insert mode.

But I haven't ever looked into modifying the keybind for pasting from the system buffer. I'm not sure offhand what the actual function for pasting from the system buffer is called.

I'll do a bit of digging later and will see if I can work out how to do it! You'd probably have to set the override for vi-command mode to use p as the paste keybind.
 
Hmm, I've had a look at the help files for bash's bind builtin.
e.g.
Bash:
help bind
Which lists various parameters that will show you which functions are available and what keys they are bound to.
I had a bit of a play with some of the options, to see if I could get anything working.
Had no luck so far. However, a quick bit of duckduckgo-fu yielded this:

Which looks like it might be an acceptable solution to your problem.

Personally, when I'm working in the terminal, I'm usually working inside a tmux session, so I use the tmux-yank plugin, which allows me to copy/paste to/from the system buffer.

I have <C-a> (Ctrl+a) set as my leader key in tmux (instead of the default <C-b>).
So to enter tmux's text selection mode, I use <C-a>{ to enter text navigation mode. I have tmux set to use vi keybinds, but there are a few anomalous ones. Like using <space> to go into selection mode and start selecting a block of text, but the movement keys are still vim ones (h,j,k,l) and I yank to the global system buffer using y.
And to paste, I use tmux's paste keybind, which is <C-a>}.
For any tmux users reading this, the tmux-yank plugin can be found on github here:

Clone it locally on your PC, to wherever you want using:
Bash:
git clone https://github.com/tmux-plugins/tmux-yank.git
Then to use it, simply source it in your .tmux.conf.
e.g. add the following line:
Bash:
source /path/to/tmux-yank/yank.tmux
Where /path/to/tmux-yank/ is the path where you cloned the tmux-yank to.
To update the plugin, all you have to do is periodically run:
Bash:
git pull
Then the next time you run tmux, the latest version of the plugin will be loaded.
 
Last edited:
The first background info was helpful , but the referred stackexchange's answer is exactly what i was looking for . I've just converted its functions' form to oneliners to make it more compact as i didn't want such a miniature change to span many lines .
 

Staff online


Top