William Vaughn

Emacs shell-command

When I used to use vim (or evil), I was able to use the :read command to execute any random shell command and plop its output into my current buffer. My main use case was in using base64 to encode secret values into Kubernetes Secret YAML manifests. It went something like this:

Go to the place in the buffer that I want to input a secret value.

:read ! echo -n 'Sup3rS3cr3t' | base64

And that would place U3VwM3JTM2NyM3Q= at the cursor, writing the base64 encoded value that was output from my shell command, executed from within vim.

Recently, I learned how to do this in emacs. It is M-!, or=shell-command=, to type in a command. And it is M-|, or shell-command-on-region, to run the shell command on the content of region (as STDIN).

You can use the C-u prefix to output the text resulting from the command to the buffer you run the command from.

Example, decoding a value.

C-u M-! enter echo 'Sup3rS3cr3T' | base64 the output is, U3VwM3JTM2NyM3Q=.

Now if you highlight that region, you can run C-u M-| input cat | base64 -d and it’s replaced with Sup3rS3cr3t again.

Thanks for reading, hope you enjoy this one neat trick you might not be using in emacs. All the best!