vimleader

Copying current Vim buffer into clipboard


I am trying to copy Vim's buffer to clipboard and I did the following:

:!cat %|pbcopy # It works perfectly

Then I tried to map this with the leader key (its my first attempt to map something, so please excuse if something silly is found).

:map <leader>c :!cat %|pbcopy # This is not working; vim complains: Not an Editor command


Solution

  • Escape the pipe

    :map <leader>c :!cat %\|pbcopy
    

    Also consider using nnoremap to stop recursive mappings and ending the command with <CR> so the command runs automatically.

    nnoremap <leader>c :!cat %\|pbcopy<CR>
    

    You also might consider using shellescape just incase the file has spaces or other unusual characters

    nnoremap <leader>c :exec '!cat '.shellescape('%').'\|pbcopy'<CR>
    

    Or as Peter Rickner says just use

    nnoremap <leader>c :w !pbcopy<cr>