Skip to main content
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
  • 51.3k
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.4k
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
  • 33.2k
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
  • 51.3k
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,899
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,812
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.6k
5 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
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
  • 948
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,669
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
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.4k
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
  • 51.3k
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.6k
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
  • 51.3k
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
  • 51.3k
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.9k
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
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,341
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

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,661
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
  • 18.1k
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
3 votes
Accepted

At the time of prompting for a password (from a subordinate shell), sometimes Emacs hides the password, sometimes not. Why the inconsistency?

comint uses a regular expression to recognize password prompts: comint-password-prompt-regexp. It's fairly complicated in an attempt to catch as many variations as possible, but it is conceivable (and ...
NickD's user avatar
  • 29.9k
3 votes

On tramp shell, commands like `head`, `tail`, `man`, `git log`, etc... output all text at once instead of prompting

No, this isn't supported by Tramp. It suppresses all pagers when calling interactive commands like man. See "PAGER=cat" in tramp-remote-process-environment. And even if you change this to &...
Michael Albinus's user avatar
2 votes
Accepted

zsh expansion modifiers are treated as comments

You want to look at sh-syntax-propertize-function where it currently uses the regexp "\\(?:[^|&;<>()``\\\"' \t\n]\\|\\${\\)\\(#+\\)" to match those # chars which should not be considered as ...
Stefan's user avatar
  • 26.4k

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