~willvaughn/willvaughn.org

5ed3822f0f6aa5953cb3fa0f8f291c38214a1ffa — William Vaughn 2 months ago 329ab17 master
add emacs shell command blog
2 files changed, 57 insertions(+), 0 deletions(-)

A articles/emacs-shell-command.org
A content/articles/emacs-shell-command.md
A articles/emacs-shell-command.org => articles/emacs-shell-command.org +28 -0
@@ 0,0 1,28 @@
#+HUGO_BASE_DIR: ../
#+HUGO_SECTION: articles
#+TITLE: Emacs shell-command
#+DATE: [2022-07-07 Thu 00:07]
#+HUGO_DRAFT: false
#+HUGO_TAGS: writings emacs

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.

#+begin_src
:read ! echo -n 'Sup3rS3cr3t' | base64
#+end_src

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!

A content/articles/emacs-shell-command.md => content/articles/emacs-shell-command.md +29 -0
@@ 0,0 1,29 @@
+++
title = "Emacs shell-command"
author = ["William Vaughn"]
date = 2022-07-07T00:07:00-07:00
tags = ["writings", "emacs"]
draft = false
+++

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.

```nil
: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!
\ No newline at end of file