I know how to run external commands and send the output to new files and such, but I'm wondering if a Vim scripter can help me do something that has to be a common task.
Assume I have a buffer open in Vim called file.markdown
I want to run my external markdown processor of choice, say pandoc, on the contents of that buffer and have it appear in a new tab called file.html.
In other words I don't want a filter to replace the markdown. I want to run the external command and have the output placed in a new appropriately named buffer in a new tab.
THANK YOU
Rick
!pandoc % -o %<.html | tabe %<.html
But I get this error
/bin/bash: tabe: command not found
shell returned 127
Looking for a bash command not a vim command, it seems.
On Jan 9, 2014 9:13 PM, "Rick Dooling" <rpdo...@gmail.com> wrote:
>
> On Thursday, January 9, 2014 10:42:47 AM UTC-6, Rick Dooling wrote:
> > Dear Vim Scripters:
> >
> > I know how to run external commands and send the output to new files and such, but I'm wondering if a Vim scripter can help me do something that has to be a common task.
> I have tried this:
>
> !pandoc % -o %<.html | tabe %<.html
>
> But I get this error
>
> /bin/bash: tabe: command not found
>
> shell returned 127
>
> Looking for a bash command not a vim command, it seems.
Not exactly. :! takes pipe symbol as its argument. Vim is not ever looking for any shell commands (except for the case when you use executable()), it is just passing everything to shell. As pipe and following characters were assumed to be :! argument bash got tabe. You need to use :execute to avoid such problems.
>
> >
> > Assume I have a buffer open in Vim called file.markdown
> >
> > I want to run my external markdown processor of choice, say pandoc, on the contents of that buffer and have it appear in a new tab called file.html.
> >
> > In other words I don't want a filter to replace the markdown. I want to run the external command and have the output placed in a new appropriately named buffer in a new tab.
> >
> > THANK YOU
> >
> > Rick
>
> --
> --
> You received this message from the "vim_use" maillist.
> Do not top-post! Type your reply below the text you are replying to.
> For more information, visit http://www.vim.org/maillist.php
>
> ---
> You received this message because you are subscribed to the Google Groups "vim_use" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to vim_use+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
Thank you,
I shall investigate execute!
Quite an education! Still learning Vim. I mainly just write in it. Very little vim scripting.
This works inside Vim on the command line
:execute "!pandoc % -o html" | :tabe %:t:r.html
But I could not map it. I would get weird errors about using :p:h.
So I did this instead.
function! MD()
exe "!pandoc % -o html"
exe ":tabe %:t:r.html"
endfunction
You do not need any :exe here in the current state. But you need :exe in the first statement: do not ever use % in shell commands as it is not doing any escaping. E.g. if name of currently edited file contains space first line of function MD will not do its job.
You should use
execute '!pandoc" shellescape(@%, 1) '-o ' shellescape(expand('%:t:r')) '.html'
instead. This does not apply to vim commands: :tabe %:t:r.html is fine.
Sorry, but I had a typo: both shellescape() calls should have second argument.
By the way, colon before tabe is optional. In mapping it is used to enter command mode, here it is just ignored (I usually use it to mark start of the body of :autocmd and :command: command -nargs=1 Foo :echo 'foo'). It does not harm in any case.
It does not. Just run in *terminal* vim.
Mmm. I'm on a MacBook Pro using MacVim?
Don't know if that would do it.
Just tried it again to be sure.
if no file is specified with -o in pandoc it goes to STDOUT, and you are using redir to redirect STOUT to the clipboard. At least that's how I understand it.
Another scripting angle, esp. using Python or Ruby is just run the commands, save the file, and tell vim to open the file in a new buffer.
Now that I've been playing, the redirect to the clipboard is actually even more useful, because you can use Marked2 or some other html converter/viewer to check things before loading your html up to go paste.
Rick
If it is terminal vim output is not captured. In fact command executed with :! gets direct access to the terminal. For capturing output there is system().
>
> Say you create a new file, then write some markdown, and, before
> saving, you run that function. You'd get nothing, because pandoc
> receives, as input, an empty file. Pandoc, and many other programs,
> however, support getting their input from stdin. In vim you can send
> the contents of the current buffer to a process' stdin, and then the
> process' stdout replaces those contents, just like when one does:
>
> :%!sort
>
> What I was trying to ask is. How does one send those unsaved contents
> to a program without getting them replaced with the output of such
> program, but instead get the output in a separate tab? The function
> you sent provides a solution for the second part, since it will
> redirect output to a variable or a register. Now we are only missing
> the first part, which we nearly have, but it uses files, rather than
> buffer contents.
>
> Hope that clarifies my question a little.
>
> Regards,
>
> --
> Jacobo de Vera
> http://www.jacobodevera.com
> @jovianjake
>
I think I am not the person to answer. I have never tried to do anything with buffer contents before saving them as a file. In fact, my maps often begin with :w<CR> to make sure that the save happens first, or I guess :up<CR>. I think it would be a bad idea to attempt anything with a filter or external program on an unsaved file. One error and poof.
:tabnew | read !pandoc #
But, just as '%' runs into trouble with escapes, so does '#'. So this is better:
:tabnew | exe "read !pandoc " . shellescape(@#, 1)
And if you want to map it, you'll need to escape the '|', so:
nmap <leader>h :tabnew \| exe "read !pandoc " . shellescape{@#, 1)
(see http://stackoverflow.com/a/8292838/1342986 and the surrounding comments.)
--
--
You received this message from the "vim_use" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
---
You received this message because you are subscribed to a topic in the Google Groups "vim_use" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/vim_use/i1mOvKCPuD4/unsubscribe.
To unsubscribe from this group and all its topics, send an email to vim_use+u...@googlegroups.com.