DEV Community

Sérgio Araújo
Sérgio Araújo

Posted on Edited on

ZSH/VIM - Editing clipboard content

A very common task on our daily routine is editing last command or the clipboard content, so I have decided to create some zsh widgets and aliases to make this easier.

Edit clipboard content on vim

alias vcb='xclip -i -selection clipboard -o | vim -' 
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

If you want a vim scratch buffer, so you don't have to worry to save it before leaving it.

alias vcb="xclip -i -selection clipboard -o | vim -c 'setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile' -"
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

Now if your are on the terminal and want to edit some content you've copied on your browser just type:

vcb
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

It can be more concise...

alias pbpaste='xclip -i -selection clipboard -o'
alias pbcopy='xclip -selection clipboard'
alias vimscratch="vim -c 'setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile'"

alias vcb="pbpaste | vimscratch -"

# copy last command to clipboard
alias last2cb='fc -ln -1 | pbcopy'
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

ZSH widgets

Put these widgets on your ~/.zshrc and don't forget to also add the related aliases

# Copy the most recent command to the clipboard
function _pbcopy_last_command(){
        fc -ln -1 | pbcopy
    }
zle -N pbcopy-last-command _pbcopy_last_command
bindkey '^x^y' pbcopy-last-command

# Edit content of clipboard on vim
function _edit_clipboard(){
    pbpaste | vimscratch -
}
zle -N edit-clipboard _edit_clipboard
bindkey '^x^v' edit-clipboard
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

Now you can use:

 Ctrl-x Ctrl-y  .............. copy the last command to the clipboard
 Ctrl-x Ctrl-v  .............. edit clipboard content on vim
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

OBS: I have changed the short

vim - 
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

to

vim -c 'setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile' - 
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

The advantage is the vim will use a scratch buffer, withou need to worry on saving it if you don't want to. But if you have the "vimscratch" alias, don't bother yourself with this.

If by any chance your xclip -i -selection clipboard -o command does not work you can use: vim -c '0pu+' instead.

Copy current vim buffer to the clipboard

:%y+

: ................. command
% ................. current file (buffer | file in memory)
y ................. yank
+ ................. clipboard register
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

Once you are on vim insert mode you can insert the clipboard register typing:

Ctrl-r +
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

There is a problem using this shortcut, but we can solve by adding these lines to your ~/.vimrc or init.vim

" avoid clipboard hacking security issue
" http://thejh.net/misc/website-terminal-copy-paste
inoremap <C-R>+ <C-R><C-R>+
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

Copy last vim command to the clipboard

:let @+ = @:

let ............. set a variable (in this case the clipboard)
@+ .............. clipboard register
=  .............. attribuition
@: .............. last command (register)
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

Run the clipboard content as a vim command

:@+
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

The command above is particularly useful when you want to test a vim function you see on the Internet, just copy it and run it. For example, the following function aims to close all buffers but the current one.

function! CloseAllBuffersButCurrent()
    let curr = bufnr("%")
    let last = bufnr("$")
    if curr > 1    | silent! execute "1,".(curr-1)."bd"     | endif
    if curr < last | silent! execute (curr+1).",".last."bd" | endif
    echom "All other buffers unloaded!"
endfunction
command! -nargs=0 Bonly :call CloseAllBuffersButCurrent()
command! -nargs=0 Bo :call CloseAllBuffersButCurrent()
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

You can run :@+ and call the function before deciding if it is what you want

A final gift

This comes from StackExchange. Improving backward-kill-word

What about Ctrl-w deleting words between quotes even if they have space?

autoload -U select-word-style
select-word-style normal

zle -N backward-kill-shell-word backward-kill-word-match
zstyle :zle:backward-kill-shell-word word-style shell

bindkey '^w'  backward-kill-shell-word
⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน ⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน

Top comments (1)

⭐ Must Read: voyegr/zshvim editing clipboard content - ประเด็นร้อน
 
voyeg3r profile image
Sérgio Araújo •

Feel free to comment!