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 -' 
Enter fullscreen mode Exit fullscreen mode

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' -" 
Enter fullscreen mode Exit fullscreen mode

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

vcb 
Enter fullscreen mode Exit fullscreen mode

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' 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Now you can use:

 Ctrl-x Ctrl-y .............. copy the last command to the clipboard Ctrl-x Ctrl-v .............. edit clipboard content on vim 
Enter fullscreen mode Exit fullscreen mode

OBS: I have changed the short

vim - 
Enter fullscreen mode Exit fullscreen mode

to

vim -c 'setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile' - 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

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

Ctrl-r + 
Enter fullscreen mode Exit fullscreen mode

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>+ 
Enter fullscreen mode Exit fullscreen mode

Copy last vim command to the clipboard

:let @+ = @: let ............. set a variable (in this case the clipboard) @+ .............. clipboard register = .............. attribuition @: .............. last command (register) 
Enter fullscreen mode Exit fullscreen mode

Run the clipboard content as a vim command

:@+ 
Enter fullscreen mode Exit fullscreen mode

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() 
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode

Top comments (1)

Collapse
 
voyeg3r profile image
Sérgio Araújo

Feel free to comment!