2

How can I create a Vim command and copy it's results to clipboard?

I want to convert Markdown to HTML and copy the result to the clipboard. So far I got:

nmap md :%!/bin/markdown/Markdown.pl --html4tags

But this will substitute my opened file on Vim to the result of Markdown.

Fábio Perez
  • 23,850
  • 22
  • 76
  • 100
  • Related question: http://stackoverflow.com/questions/1694392/vim-store-output-of-external-command-into-a-register – ib. Aug 14 '11 at 23:41

3 Answers3

4

You didn't say which system you're using, but generally saving it in the + register should work. You can call system():

:let @+=system("markdown --html4tags", join(getline(1,line("$")), "\n"))

The system() function takes the second parameter (optional) as input to the command, and here I'm using a chain of other functions to retrieve the contents of the current buffer. Not sure, but there should be a better way to do it (if someone knows, please let me know).

Alternatively, you can pass markdown your file name as input directly:

:let @+=system("markdown --html4tags " . shellescape(expand("%:p")))

But keep in mind that you'll need to write the file before calling this.

Two important notes:

  1. I didn't type your full path to markdown. Use it.
  2. I didn't use maps here, the final result would be something like:
nnoremap md :let @+=system(...)
sidyll
  • 57,726
  • 14
  • 108
  • 151
  • I'm using Mac OS X. Got this: `E354: Invalid register name: '+'` – Fábio Perez Aug 15 '11 at 21:27
  • I'm also on Mac OS X, but with MacVim. Are you using it from the terminal? Then I don't know if it's possible. Maybe only GUIs can interact with the clipboard, since it's a windowing system thing, and not for terminal emulators. – sidyll Aug 15 '11 at 22:18
  • I've tried on Terminal. How can I access the shortcut on MacVim? – Fábio Perez Aug 15 '11 at 22:47
  • On terminal I can type `\` then md. I can't do that on MacVim. – Fábio Perez Aug 15 '11 at 22:49
  • @Fábio: Was the map saved in your vimrc? also, you don't need to jump with `\`\`` to access it. – sidyll Aug 15 '11 at 22:52
  • Sorry, I mean the backslash \. Yes, it's on .vimrc. – Fábio Perez Aug 15 '11 at 22:56
  • Hmm, thats interesting. Some notes I can point out are: 1 in your map, you didn't use the backslash, so you might want to use md directly. 2 are you using nnoremap, like I posted instead of nmap? Sometimes your map may be remapped to something else. 3 do you have a `` in the end of it? This is to simulate pressing enter. I just hope any of these help you solving it! – sidyll Aug 15 '11 at 23:21
1

get the xsel package

and pipe stdout to xsel --clipboard

For instance:

cat /etc/passwd | xsel --clipboard

Is that what you're looking for?

Kevin
  • 1,489
  • 2
  • 20
  • 30
0

Filling in a missing piece (2+ years late). With the clarification that the user was on a Mac and since the asker's "why doesn't it work for me?" question was not answered.

To redirect the output of a command to the system clipboard from within MacVim (GUI version) you need to set the '*' to be the "clipboard register" you need to change the clipboard setting to 'unnamed':

set clipboard 'unnamed'   # 'cb' can be substituted for 'clipboard'

Then sidyll's answer should work except specify the '*' register and not the '+' register:

:let @*=system(...)

The clipboard feature is likely not compiled into the "terminal version" of MacVim and when it is available option setting is different from 'unnamed'. To see more details regarding what works where and how, see the documentation in MacVim using the Vim help command:

 :help 'clipboard'    (include the single quotes since it's a set option!)

(I'll skip the command mapping issue since it always takes me several tries and I still have to look it up; finding the help for the mapping commands should be easier than finding it for the * register.)

Zhora
  • 549
  • 3
  • 11