2

Ok, I have rather simple question: How can one bind vim command/hotkey to execute some complicated shell-script?

E.g. I want to optimize base64-inlined images inside css files. I know, that in shell it would be something like:

echo `selection` > /tmp/img.png.b64
base64 -d /tmp/img.png.b64 > /tmp/img.png
optipng -o7 /tmp/img.png
base64 -w 0 /tmp/img.png > `selection`

I want to put selection into the script and then get result of script execution and replace selected content with that result. I see the workflow as selecting base64 part in visual block mode and type e.g. :'<,'>optipng or press some hotkey. The question is how to setup vim to do that.

1 Answer 1

1

Vim allows to filter line(s) through an external command with :[range]!{cmd}. If your optipng command can take input from stdin and print to stdout, you can use it directly; else, with the help of a small shell script wrapper. See :help :range! for details.

One limitation is that this only works for whole lines, not parts, even when visually selected. You can get around this with the vis plugin; it would then be something like:

:'<,'>B !optipng -o7
Sign up to request clarification or add additional context in comments.

1 Comment

I suppose, I'd prefer parsing base64 part inside css rule (to vis plugin). Thank you for solution.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.