Silently Redirecting to the Vim Clipboard

Posted by Derek Wyatt on September 30, 2010
How to redirect Vim messages to the clipboard.

In an upcoming video tutorial, I’ll be doing a bit of work with the :redir command. This is a great Vim facility but it can be helped with a function wrapper. I’m including that wrapper here:

function! RedirToClipboardFunction(cmd, ...)
    let cmd = a:cmd . " " . join(a:000, " ")
    redir @*>
    exe cmd
    redir END

command! -complete=command -nargs=+ RedirToClipboard
    \ silent! call RedirToClipbaordFunction(<f -args>)

This function allows you to run a command (such as :history, hint hint) and have the contents placed to the clipboard that you can then paste somewhere else. The command that is created just makes it easier to use. For example:

:RedirToClipboard history : -50,