2

First of, I am sure there are plugins that will do what I am trying to achieve but I am using this as an exercise to learn vim as I am still quite new so I am not really interested in plugin suggestions.

I am trying to build a command that will pope the current line of my buffer through Pandoc and replace the line with the output. This would allow me to, for example, have the line:

# Title

turn into

<h1>Title</h1>

in an html document. In time I would like it to output different text based on the buffer's file type but for now I am focusing on html. This is what I have so far:

exe "read !echo " . getline('.') " | pandoc -t html"

I am aware that at the moment this won't replace the current line but that is intentional whilst I'm debugging

With my cursor on the line #test, I got the following added to the line below it:

<p>exetest</p>

It appears to me that the # is being converted to exe before it is piped into pandoc. Could you please tell me why this is and how to fix it.

Thanks in advance

1 Answer 1

5

The problem with your command is that you're constructing a piece of Vimscript with constructs a piece of shell script. Both require some careful handling to take care of "special" characters such as #. For example:

:exe "read !echo " . shellescape(escape(getline('.'), '#%')) " | pandoc -t html"

This still won't cover all the cases, though, and I wouldn't recommend using it. If you want to add a new line with the output below the current one, then a better way is probably:

:call append('.', systemlist('pandoc -t html', getline('.')))

This avoids having to escape Vimscript because you're not building the command with exe, and the second argument to systemlist() is passed as the command's stdin, so you avoid shell escaping with that as well!


You can also use a motion with !; the . motion is the current line so this:

:.!pandoc -t html

will replace the current line with the output of pandoc.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.