22 votes
Accepted

How does emacs `shell-mode` know to prompt for sudo?

This is done via a process filter. By default comint-output-filter-functions includes comint-watch-for-password-prompt, which is the filter function that handles this. If it sees text matching ...
phils's user avatar
  • 49.5k
16 votes
Accepted

How to prevent backspace from deleting my shell prompt?

I think you're looking for comint-prompt-read-only. E.g. (setq comint-prompt-read-only t) should take care of it.
Stefan's user avatar
  • 26.3k
11 votes
Accepted

weird characters in shell mode with zsh

The control sequences ^[[?2004h and ^[[?2004l turn bracketed paste mode on and off. Shell mode doesn't support bracketed paste mode (with these or any other control sequences), so zsh shouldn't try ...
Gilles 'SO- stop being evil''s user avatar
11 votes
Accepted

Set indentation for shell script function

Setting sh-basic-offset to the required indentation is what is needed - but what I didn't realise is, it's also necessary to close and reopen all shell script files before the customisation takes ...
Robin Green's user avatar
9 votes
Accepted

Keymap that is dependent on the buffer

You set shell-command as global key-binding for F1 and you add a hook function to shell-mode-hook that sets the local key binding to mode-line-other-buffer. (global-set-key (kbd "<f1>")...
Tobias's user avatar
  • 32.8k
7 votes
Accepted

differences of shell-mode vs. shell-script-mode?

shell-mode is to run shell inside Emacs, shell-script-mode is a major mode for shell script editing. See: https://www.emacswiki.org/emacs/ShellMode https://www.emacswiki.org/emacs/ShMode
xuhdev's user avatar
  • 1,859
7 votes
Accepted

/bin/ls --color=always fails to produce colorized output in shell-mode

My Emacs (GNU Emacs 24.5.1) sets TERM=dumb in startup.el and my ls from GNU coreutils 8.24 checks TERM even with --color=always. dumb is not a terminal type recognized by dircolors (the utility used ...
Constantine's user avatar
  • 9,082
7 votes

Equivalent of pausing (CTRL-Z bindkey) in shell-mode

C-cC-z, which runs comint-stop-subjob, is the shell-mode equivalent. Use C-hm from your shell buffer to see other bindings on that prefix which may be of interest.
phils's user avatar
  • 49.5k
7 votes
Accepted

How to run a previous unique command in an Emacs shell (without repeats)?

The shell you get from M-x shell is based on comint, so you can set comint-input-ignoredups to t in order to get the behaviour you want. The help even mentions bash: comint-input-ignoredups is a ...
Omar's user avatar
  • 4,742
5 votes
Accepted

EMACS shell mode that supports annotations

Use Literate DevOps Method I recommend using the literate devops method via org-mode, that @phils suggested in the comments, because of the ease of use and adaptable workflow. Example Workflow ...
Melioratus's user avatar
  • 4,564
5 votes
Accepted

Symbol's value as variable is void: shell-mode-map

Drew is right you need to load the package, which defines shell-mode-map (require 'shell) ;; your code based on functions and variables defined in shell.el Explanation At the end of your Emacs's ...
atevm's user avatar
  • 938
5 votes
Accepted

How to send *shell* stdout directly to kill-ring?

Emacs Command Instead of select-and-copy manually, you can also write a command and let it do the work for you: ;; Adapted from `comint-delete-output' (defun comint-copy-output () "Copy all output ...
xuchunyang's user avatar
  • 14.4k
4 votes
Accepted

How can I change which shell script mode I'm in?

What is determining whether you see shell-script[fish] or shell-script[bash] is probably: The extension of the file (*.sh files will probably open with shell-script[bash], and *.fish files will ...
elethan's user avatar
  • 4,765
4 votes
Accepted

sh-script alignment issues

For the indentation after the | you should be able to get the right result by going to the incorrect line (i.e. the one that starts with grep) and then do M-x smie-config-set-indent RET before "|" RET ...
Stefan's user avatar
  • 26.3k
4 votes

Set indentation for shell script function

I set-basic-offset to 2 but that didn't seem to work, I went back pressed C-h v sh-<TAB> and realized that there was a sh-indentation I set that variable to 2 and everything seems to work like a ...
Albaro Pereyra's user avatar
4 votes

differences of shell-mode vs. shell-script-mode?

shell-mode is used by the interactive shell (command line) environment started by the shell command (spc : shell, or M-x shell in regular Emacs). It is a variant of the comint mode, which sends lines ...
Vera Johanna's user avatar
4 votes
Accepted

how to watch and cat and grep with emacs

As commented, watch needs to run in a terminal so that it can do all the fancy things that it does. In Emacs, that means running it via term. The very simplest thing is to invoke it from a shell ...
phils's user avatar
  • 49.5k
4 votes

how to watch and cat and grep with emacs

An alternative solution: (with-eval-after-load 'em-term (add-to-list 'eshell-visual-commands "watch")) To run command like watch -d date, issue: M-x eshell-command RET watch -d date RET The ...
xuchunyang's user avatar
  • 14.4k
4 votes
Accepted

How to automatically kill a shell buffer when the shell process exits

Use a process sentinel when you want to react to a process exiting. e.g.: Refer to C-hig (elisp)Sentinels In this case shell only calls shell-mode when (and after) starting the inferior process, so ...
phils's user avatar
  • 49.5k
3 votes

Get the same/map bindkeys in shell mode, in particular CTRL-r

Consider using term-mode rather than shell-mode. In term-mode you can switch between "character mode" (where commands like C-r go directly to the terminal) and "line mode" (where commands are first ...
erikstokes's user avatar
  • 12.8k
3 votes

Get the same/map bindkeys in shell mode, in particular CTRL-r

By default the binding to do this in Emacs' shell-mode is M-r which runs the command comint-history-isearch-backward-regexp. This allows you to search backwards though your input history using a ...
stevoooo's user avatar
  • 737
3 votes

How to disable show-trailing-whitespace in emacs shell?

Chances are that you'll want to do this for numerous modes, so I would suggest using an approach which makes that simpler to specify. e.g.: (defun my-hide-trailing-whitespace-maybe () "Disable `...
phils's user avatar
  • 49.5k
3 votes

How to turn off alignment-indent after line continuation in Shell-script mode?

Open a file in shell-script-mode, run M-x customize-variable RET sh-indent-after-continuation, and see if you can set it to always. (This seems to be a relatively new option, Emacs 25.1+)
user13771's user avatar
3 votes
Accepted

Directory tracking in remote shell buffer

Not the best solution, but certainly a working one: Use [dirtrack-mode]. The following preparations are needed: 1. On the remote machine: Make sure the prompt of the remote shell contains the path ...
halloleo's user avatar
  • 1,311
3 votes

Shell mode: moving through the command history

M-p and M-n let you navigate through the shell history. You can try C-up and C-down too. There are other ways, as explained in Shell History on the manual.
Manuel Uberti's user avatar
3 votes
Accepted

Emacs: How to clear a subshell screen?

You can clear the shell buffer like any other, if you get the cursor away from the current prompt, where its movement behavior is different. Here's one recipe, which feels intuitive to me but looks ...
rob's user avatar
  • 146
3 votes

Emacs: How to clear a subshell screen?

eshell-postoutput-scroll-to-bottom in the hook eshell-output-filter-functions is causing to scroll to bottom and so to remove it from the hook add this snippet to emacs config (add-hook 'eshell-mode-...
junnu's user avatar
  • 780
3 votes

Shell-mode execute script every time a shell is invoked

You can test the TERM environment variable in your bashrc file. For M-x shell it will be "dumb" as opposed to anything else. For ansi-term the TERM environment variable is set to "eterm-color".
stsquad's user avatar
  • 4,626
3 votes
Accepted

How to force filename completion in shell buffers to use quotes instead of backslashes

An interesting question. It's not configurable at all, so I recommend looking at pcomplete-insert-entry. This is where the completed text is inserted, and it's what calls comint-quote-filename. Note ...
db48x's user avatar
  • 16.7k
3 votes

Emacs unable to execute shell commands despite them being in the Emacs shell PATH

Solved it: it was because Emacs was installed as a flatpak. For some reason, this mean the Emacs shell had many differences. I wasn't able to cd into certain folders. Other folders didn't exist: /usr/...
Russ Phelan's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible