ZSH/VIM - Editing clipboard content
Tags: zsh, vim
Published: 2018-12-15 13:07:44

---
title: ZSH/VIM - Editing clipboard content
published: true
description: Faster way to edit clipboard content
tags: zsh, vim
---

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 -' 

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' -"

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

    vcb

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'

## 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

Now you can use:

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

OBS: I have changed the short 

    vim - 
to 

    vim -c 'setlocal buftype=nofile bufhidden=wipe nobuflisted noswapfile' - 

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

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

    Ctrl-r +

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>+

### Copy last vim command to the clipboard

    :let @+ = @:

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

### Run the clipboard content as a vim command

    :@+
    
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()

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