14

I can run the Bash shell that comes with Git for Windows. I put this in my .emacs:

(defun git-bash () (interactive)
  (let ((explicit-shell-file-name "D:/Program Files/git/bin/bash"))
    (call-interactively 'shell)))

Then M-x git-bash and voila, bash is running but with some weirdness:

bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
^[]0;MINGW64:/c/Users/M??rton^G
MĂĄrton@Marci MINGW64 /c/Users/MĂĄrton
$ 

and it prints that line, beginning with ^[], after every command I run. Also the encoding is messed up. How can I fix this in .emacs?

marczellm
  • 245
  • 1
  • 2
  • 8

3 Answers3

18

The ^[] noise is coming from various terminal control characters in your shell prompt. Try echo $PS1 to see the full sequence, and try e.g. export PS1='$ ' to see that a simpler prompt string removes that particular problem.

For the encoding, you might try making utf-8 your preferred encoding:

(prefer-coding-system 'utf-8)

Setting up the prompt

Emacs sets the INSIDE_EMACS variable so you could create a .bash_profile that sets PS1 only when running in Emacs.

Testing on my machine, the first line of the prompt has the problematic control characters. I created a ~/.bash_profile with this.

if [ -n "$INSIDE_EMACS" ]; then
    export PS1='\[\033[32m\]\u@\h \[\033[33m\]\w\[\033[36m\]`__git_ps1`\[\033[0m\]\n$ '
fi

This sets the prompt based on what git-bash was setting by default, but I removed the first line (up to the \n line break). I also removed the $MSYSTEM which puts MINGW64 in the prompt -- I don't need to see that. What remains is the user@host, current directory, and git repo. The control characters set colors which Emacs displays properly for me. (For details on setting up your prompt, refer to the bash manual.)

In Emacs I set the shell to git-bash:

(setq explicit-shell-file-name "C:/git-for-windows/bin/bash.exe")
(setq explicit-bash.exe-args '("--login" "-i"))

With this setup I'm still seeing the initial ioctl error message, but otherwise things work as expected and the encoding is utf-8.

glucas
  • 20,175
  • 1
  • 51
  • 83
  • That works, thanks! What does that (to me cryptic) PS1 line do? Can you help in automatically setting the shell buffer encoding to utf-8? (`MĂĄrton` should read `Márton`) – marczellm May 04 '16 at 20:11
  • The PS1 in my example was derived from the default one git-bash had set. I just deleted everything up to the line break (\n). It's using those control sequences to change the color of the various bits of information shown -- the current user, host, git repo, etc. I'd have to track down the bash PS1 doc for details. For utf-8, what is `default-process-coding-system` set to in your Emacs? – glucas May 04 '16 at 20:40
  • I found this in the Emacs manual: "To specify a coding system for the shell, you can use the command C-x RET c immediately before M-x shell. You can also change the coding system for a running subshell by typing C-x RET p in the shell buffer." (see https://www.gnu.org/software/emacs/manual/html_node/emacs/Interactive-Shell.html) – glucas May 04 '16 at 20:46
  • Its value is `(undecided-dos . undecided-unix)`. I'm looking for a way to set the buffer encoding automatically instead of having to remember another set of keystrokes every time when opening it. Maybe I should ask a new question. – marczellm May 05 '16 at 10:41
  • Mine is utf-8 dos and UNIX. I don't set it explicitly but I do have these settings: (prefer-coding-system 'utf-8) (setq-default buffer-file-coding-system 'utf-8-unix) – glucas May 05 '16 at 11:35
  • Do you see color if you run git status and git diff? All git commands seem to not have color for me. – Didier A. Aug 18 '18 at 10:03
  • Also note: Emacs and Git Bash have different ideas, how to expand `~`. In Git Bash it is `"C:/Users/me"`. In Emacs it is `"C:/Users/me/AppData/Roaming"`. – ceving Nov 30 '22 at 09:42
2

I know this is not exactly the same as the OP, but I think it's an interesting option. You can start emacs from the git-bash command-line. It will make emacs assume git-bash as the shell.

Enter something like the following from your bash prompt

bla@host MINGW64 ~/ $ emacs .&

Then, in emacs, M-x "shell"

jamars
  • 121
  • 2
1

This error points to a mismatched switch. It is probably set to -ic and you need just -c:

(setq shell-command-switch "-c")

The other option is to provide tty to git-bash with a command similar to (whatever is windows equivalent):

/sbin/getty -l bash -n 38400 tty0
Emacs User
  • 5,553
  • 18
  • 48
  • Your first suggestion does not change anything. Moreover, `M-x list-processes` shows that it did not have any effect: the process command is `D:/Program Files/git/bin/bash --noediting -i`. Your second suggestion I cannot understand. – marczellm May 04 '16 at 13:10
  • 1
    That's because you did not remove the -i switch. – Emacs User May 04 '16 at 13:18
  • Okay, so how do I remove it? – marczellm May 04 '16 at 13:20
  • 2
    You're using `explicit-shell-file-name` so I believe you would want to set e.g. `(setq explicit-bash-args '("--login" "-i"))`. – glucas May 04 '16 at 18:49