reddit / emacs

Using Emacs as my Shell

totherme · 2022-03-12 12:46
Inspired by u/awannaphasch2016's post about [living in emacs](https://www.reddit.com/r/emacs/comments/tbw7hs/for_those_who_live_inside_emacs_when_do_you_come/), I found myself writing [this blog post](https://www.kindness.city/blog/2022-03-12-using-emacs-as-my-shell.html) about using vanilla emacs to do the sort of work that you might otherwise do with a terminal window. tl;dr: I'm usually at least as happy with `M-&` and `dired` as I am with `bash` in a terminal. Keyboard macros replace bash one-liners.
_viz_ · 2022-03-12 14:04
> I was surprised to find that no-one talked about using emacs itself > instead of a terminal. I've found that for most tasks, I don't need > a terminal window. I use dired for file navigation and manipulation, > and if I want to run some program (for example sudo apt update && > sudo apt upgrade) I usually use M-& (which is bound to > async-shell-command). +1. I wonder the same thing. Opting for vterm when you have Emacs at its entirety just sounds absurd to me. Moreover, by dropping vterm, you are also embracing a much more interactive model (as you wrote). Emacs ≫ traditional terminal simply due to its interactive model. In a sense, Emacs here functions like Acme+win from Plan 9. [ C-M-% is super handy when you use the \\, construct. ]
totherme · 2022-03-12 14:11
>Emacs ≫ traditional terminal simply due to its interactive model. In a sense, Emacs here functions like Acme+win from Plan 9. Exactly! >\[ C-M-% is super handy when you use the \\, construct. \] `query-replace-regexp` (on `C-M-%`) is awesome. I'm afraid I don't know what you mean by `the \, construct` though.
_viz_ · 2022-03-12 14:14
\\, in the replace part lets you run arbitrary lisp code ;-) In interactive calls, the replacement text can contain ‘\,’ followed by a Lisp expression. Each replacement evaluates that expression to compute the replacement string. Inside of that expression, ‘\&’ is a string denoting the whole match as a string, ‘\N’ for a partial match, ‘\#&’ and ‘\#N’ for the whole or a partial match converted to a number with ‘string-to-number’, and ‘\#’ itself for the number of replacements done so far (starting with zero).
totherme · 2022-03-12 14:28
Ah yes! It is a wonderfully powerful feature. I'm afraid I've only used that once or twice though -- I think usually with something simple like `upcase`, `downcase`, or `capitalize`. Usually if I'm doing something that complex, I'll use `C-M-s` (`isearch-forward-regexp`) inside a keyboard macro, in which I can call whatever functions or lisp I want. That way I get to interactively build what I'm doing, rather than typing a complex regexp+lisp expression all in one go.
Gangsir · 2022-03-12 16:45
In many cases dired does things even better than a normal file browser - one of the huge things I love is the ability to bulk rename, which is kinda rare on gui file browsers (I believe thunar has it, but stuff like nautilus needs add-ons). Not to mention stuff like searching multiple files (no need to use grep and remember how to format stuff) or find-replacing across multiple files (yeah sed exists but can't be bothered to learn arcane syntax), etc.
totherme · 2022-03-12 16:53
Totally! For anyone looking to edit directory listings as if they were text files, check out wdired mode: https://www.gnu.org/software/emacs/manual/html_node/emacs/Wdired.html
tconfrey · 2022-03-12 18:29
Beyond what can be done with specific modes I find great value in always running my shells inside an emacs buffer (`M-x shell`). You get full text search, the ability to easily copy any output into another buffer, run macros to automate things, scrape data from command output etc. I generally have a named buffer running a shell in each of my active environments.
recencyeffect · 2022-03-12 21:10
Also let's not forget eshell - you can freaking mix elisp and shell commands in the same command! Nowadays i use it for some tasks almost exclusively, e.g. with a lot of short commands that are not worth wrapping in a function.
bigfig · 2022-03-12 22:23
I have maybe two dozen bash functions that I rely on, and eshell looks like bash, but I lose those functions. Maybe I'd consider the switch if it were trivial to re-write bash functions for eshell.
naokotani · 2022-03-12 23:49
I didn't even know about this feature. This is excellent information.
naokotani · 2022-03-12 23:51
I love the feature to automatically copy or move files between two dired buffers.
b3n · 2022-03-13 00:20
You may be interested in [Sheldon](https://github.com/Overdr0ne/shelldon#introduction) (by u/SamTheComputerSlayer). It's a wonderful little package that enhances `async-shell-command`.
nroose · 2022-03-13 03:37
I have a hybrid. I run my shells mostly within emacs. I use async shell commands for some things. I have emacs functions. I have a long list of shell scripts. I use magit for some things, vc for some things, and git shell commands for some things. It's a little crazy or a lot. But it works so very well so much of the time.
totherme · 2022-03-13 08:05
Neat! Thanks :)
totherme · 2022-03-13 08:10
Depending on where your bash functions are defined, they might be available to `M-&`. This is actually related to one of the reasons I tend to use `M-&` rather than `eshell`. I like to be able to use bash for loops and things.
totherme · 2022-03-13 08:11
Also works transparently with TRAMP!
[deleted] · 2022-03-13 21:16
[deleted]
sebhoagie · 2022-03-14 02:46
I tried vterm for a while, and while technically impressive, running a shell in this way gives all the nice features you mentioned, with less complexity. Yes, I can't run htop inside Emacs, but I do have proced, so... I am fine.
Eastern_Advertising7 · 2022-03-15 07:13
How does this work? I usually have to manually do it
spekvojbek · 2022-03-15 08:32
`(setq dired-dwim-target t)`