1

Here's what I want bash to do, first in pseudocode, then in bash as far as I've gotten, then the error:

"What I want" pseudocode

If a file is saved in the "Cover Letters" directory with a filename ending in "md":

    run a Bash command like:

    pandoc [FILENAME] -o [FILENAME].replace(".md",".pdf") -s -S

What I've Got So Far

 :autocmd BufWritePost ~/Dropbox/Professional/Covers/*.md pandoc % -o ${%/md/pdf} -s -S

This code correctly identifies when a "md" file is being saved in the Dropbox folder, and then runs a command incorporating the filename. However, the command errors out with:

"test.md" 1L, 6C written
:!pandoc test.md -o ${test.md/md/pdf} -s -S
/bin/bash: ${test.md/md/pdf}: bad substitution

shell returned 1

What's wrong with my substitution? I feel like I'm so close...

Edit: current version:

autocmd BufWritePost ~/Dropbox/Professional/Covers/*.md #!star='~/Dropbox/Professional/Covers/'% && fin=${star/md/pdf} && p    andoc star -o fin -s -S
Ben Quigley
  • 727
  • 4
  • 18
  • I have answered a similar question [\[ here \]](http://stackoverflow.com/a/38671667/1620779). See the substitution part in the function which is exactly what you need. – sjsam Jul 30 '16 at 16:21
  • Thanks, is it that replacement only works on variables rather than strings? I'm not sure how to get Vim to execute a multi-line bash script (rather than a one-liner) yet. – Ben Quigley Jul 30 '16 at 16:27
  • What's wrong with ${test.md/md/pdf}? – Ben Quigley Jul 30 '16 at 16:29
  • Is there a specific reason you used vim? – sjsam Jul 30 '16 at 16:33
  • Yeah, the purpose of what I'm doing is to let me write cover letters in markdown using vim, that when I save them, automatically makes pandoc convert them to pdf. So for example the final command that I want bash to execute would look like: pandoc example.md -o example.pdf -s -S – Ben Quigley Jul 30 '16 at 16:38

2 Answers2

1

Finally got a version that works:

autocmd BufWritePost ~/Dropbox/Professional/Covers/*.md !star='%' && fin=${star/md/pdf} && pandoc $star -o $fin -s -S

Very basic bash errors, looks like. Thanks sjsam for your help!

Ben Quigley
  • 727
  • 4
  • 18
  • `fin=${star/md/pdf}` => `fin=${star/%.md/.pdf}` coz you are looking for an extension `md` and not something like `mdfile.ext` and `pandoc $star -o $fin` => `pandoc "$star" -o "$fin"` (Here, no harm in double quoting the variables which will also help you accommodate non-standard filenames) – sjsam Jul 30 '16 at 17:33
0

This is what you're looking for :

#!/bin/bash
function pandocfn()
{
pandoc "${1}" -o  "${1/%\.md/.pdf}"
}
export -f pandocfn
find ~/Dropbox/Professional/Covers  -type f -iname "*.md" -exec \
     bash -c 'pandocfn "$1"' _ {} \;

Save the script as script as say script.sh inside ~/Dropbox/Professional/Covers, do chmod +x script.sh and from within vim do

: ! ./script.sh

If you're saving the script in some location other than ~/Dropbox/Professional/Covers, then do

: ! full/path/to/script.sh

Please see shell parameter [ expansion ].

sjsam
  • 21,411
  • 5
  • 55
  • 102
  • This is a real noobie question, but where do I save that .sh file in order for it to be invokable by vim? I've tried writing my own .sh script and letting vim try to execute it, but I couldn't figure out where to save it. – Ben Quigley Jul 30 '16 at 16:43
  • @Iroh : Please see the update on how to run the script from within vim which may save you a lot of time and do accept the answer if it works :) – sjsam Jul 30 '16 at 17:24