reddit / emacs
Is it possible to redirect a command that opens $EDITOR to an emacs buffer instead?
JoeKazama · 2022-09-19 19:16
Title may be confusing so let me clarify. I often use a simple tool called [sops, made by Mozilla,](https://github.com/mozilla/sops) that is used to encrypt/decrypt yaml/json/text files with your gpg key. It works from the command line so if you invoke sops on an encrypted file it will decrypt it, open the decrypted version on a temp file (located in /tmp/<random-digits>) on your $EDITOR and then re-encrypt everything and delete the temp file once you close the $EDITOR.
Currently I invoke sops on vterm which opens an new emacsclient instance where I edit my file. I thought wouldn't it be cool if emacs could auto recognize if a file is encrypted with sops and decrypt it on the same buffer.
Now the only thing I am having a hard time understanding is how you would go about redirecting sops to open a new buffer instead of whatever your $EDITOR is.
anaumann · 2022-09-19 19:21
Probably a little more complicated than you had in mind, but there's a [minor mode](https://github.com/zellio/ansible-vault-mode) to open ansible's encrypted vaults which sounds as if it works very similar to sops.
If you gave it a good, hard stare, you might adjust it to your needs and make something even cooler :)
sickofthisshit · 2022-09-19 19:22
Set `$EDITOR` to "emacsclient -t" and have a running Emacs that said `(server-start)` or used `--daemon` as a command line flag.
https://www.emacswiki.org/emacs/EmacsClient
JohnathanDee · 2022-09-19 19:25
Yes. In your shell config file (`.bashrc`, etc) just add:
export EDITOR='emacsclient -nw`
Also worth noting that Emacs can decrypt/encrypt with gpg for you, as well:
https://www.gnu.org/software/emacs/manual/html_node/epa/Encrypting_002fdecrypting-gpg-files.html
deaddyfreddy · 2022-09-19 20:06
https://github.com/magit/with-editor
but, as some already noticed, Emacs transparently works with gpg files
mtndewforbreakfast · 2022-09-19 21:48
Sops-encrypted files in certain formats are not necessarily encrypted at the top file level, i.e. YAML keys may be a mixture of encrypted and plaintext. https://github.com/mozilla/sops#48encrypting-only-parts-of-a-file
This obviously breaks compatibility with the naive/straightforward GPG support that Emacs has.
mtndewforbreakfast · 2022-09-19 21:48
See [my other reply](https://www.reddit.com/r/emacs/comments/xilrvs/is_it_possible_to_redirect_a_command_that_opens/ip4a5bh/) for why the native GPG support is not totally sufficient.
JohnathanDee · 2022-09-19 22:13
Gotcha yah I'm not familiar with it.
But .. worth mentioning that Org Mode can encrypt blocks, as well. Along with babel tangle there's some interesting literate config possibilities.
Also, if sops is popular enough, eventually someone will write a package to interface with it
deaddyfreddy · 2022-09-19 22:28
https://orgmode.org/worg/org-tutorials/encrypting-files.html
see org-crypt
mtndewforbreakfast · 2022-09-19 22:32
To be clearer - the value proposition of sops is 1. it doesn't have to be GPG, it can be a cloud KMS, which Emacs can't gracefully do and 2. there is a surrounding ecosystem of third party programs. A lot of stuff is specifically sops-aware but would not successfully read anything using a technique specific to Emacs.
For the common reasons people reach for sops, Emacs cannot be a replacement.
mtndewforbreakfast · 2022-09-19 22:34
A lot of tooling in devops/infrastructure can specifically consume the file syntax produced by sops natively, but not so with the niche formats that Emacs is capable of producing. If you only need to read it again **in Emacs**, like a private org doc? Sure, go nuts.
JohnathanDee · 2022-09-19 22:49
I understand, but since this is an Emacs forum, I'm just pointing out the possibilities. A custom org export backend could tangle to individual yaml files with encrypted secrets... If one were so inclined.
pja · 2022-09-20 06:47
emacsclient -t -c will start a fresh emacs if it there isn't one already running.
Edit, I got this wrong: it’s `emacsclient -t -a ""` you want. Or set EDITOR to `emacsclient` and ALTERNATE_EDITOR to `""`
RoninTarget · 2022-09-20 07:09
Then just emacsclient.
sickofthisshit · 2022-09-20 12:30
Yeah, I didn't want to repeat the whole wiki page of options.
dharris · 2022-09-20 12:43
As others have said, `emacsclient` is the way to go. The non-obvious thing when you’re first doing this is to tell emacs that you’re done with this $EDITOR-driven edit is that you need to hit `C-x #`.
The `-t` or `-nw` options mentioned open it in the current terminal session.
I just use `emacsclient` without options. I’m taken to my GUI emacs session, I do my edit, hit `C-x #` and then back to my terminal.
pja · 2022-09-20 13:32
I honestly think firing up a fresh emacs should be the default emacsclient setting, if no daemon process currently exists. If the user wanted something else they should probably ask for it explicitly.
sickofthisshit · 2022-09-20 13:52
Plausible, although there are two things mitigating against that.
My general use case is a GUI Emacs on my workstation and then ssh in remotely when I am WFH. Firing up a default Emacs would potentially launch one with an inconvenient config unless my `.emacs` is very carefully crafted, and I would rather just manually fix the cases where my machine rebooted or something. 95% of the time I have one running.
More generally, tweaking stuff like this gets complaints from people who have the same workflow dating from 1995, and project maintainers are often part of this subset.