M-x shell is the standard interface to OS's shell. On linux, it calls bash (or whatever your default), on Windows, it calls cmd.exe. M-x shell is the most frequently used.
M-x eshell is a shell written in emacs lisp entirely. It has a syntax similar to bash, but is integrated with elisp well. e.g. you can eval elisp code right there. Eshell is especially ...
comint-derived modes (like shell, ielm, ...) support filter functions, the following snippet makes the output and prompt read-only:
(setq comint-prompt-read-only t)
(defun my-comint-preoutput-turn-buffer-read-only (text)
(propertize text 'read-only t))
(add-hook 'comint-preoutput-filter-functions 'my-comint-preoutput-turn-buffer-read-only)
As for ansi-...
I think the first part of your question can be done with dirtrack-mode. First, you set your shell prompt to include the present working directory. Next, you set dirtrack-list to a regex that tells dirtrack-mode how to extract it. Example: I set PS1 in Bash to be this:
export PS1="[\h:\w] $ "
and in Emacs I set dirtrack-list and turn on dirtrack-mode:
Term mode has two input mode char mode and line mode. In char mode you would press C-x M-x (prefix your standard keyboard shortcuts with C-x ).
If you enter line mode (C-c C-j ) M-x should work as usual.
You can read more on both:
The info reader in Emacs can be accessed with C-h i. There you can jump directly into the some-node node with g (some-node) RET.
Alternatively, you can go to the some-node node by eval'ing (info "some-node").
Note that the info reader on the terminal picks man pages when info nodes cannot be found. There is no "glibc" info node, but just a man page. To ...
The bi-directional text support introduced in Emacs 24 interacts badly with term.el when large numbers of spaces or tabs are inserted, and many applications clear the screen (often drawing a new background colour) by writing WIDTHxHEIGHT spaces (which, if the terminal’s dimensions are sufficiently large, may take several seconds).
The workaround is to ...
In ansi-term's "char run mode", C-<left> & co are by default intercepted by emacs, which moves the point instead of telling the undelying terminal to do it. This leads to the situation you describe: the point moves in the emacs buffer, but the next insertion will use the real cursor position in the terminal, which hasn't changed.
You thus need to ...
C-c C-j to activate term-line-mode, then, the terminal buffer act more like a normal text-buffer. Switch back to character mode with C-c C-k.
You may be intersted by this function that will help you toggle, between the two modes:
(defun jnm/term-toggle-mode ()
"Toggles term between line mode and char mode"
Terminal.app and iTerm 2 set LANG based on the region selected in System Preferences (defaults read -g AppleLocale) by default unless the "Set locale environment variables on startup" setting is unchecked in Terminal or the "Set locale variables automatically" setting is unchecked in iTerm 2.
This changes LANG in Emacs:
(setenv "LANG" "en_US.UTF-8")
A nice package that helps with setting up the environment is exec-path-from-shell (at MELPA or https://github.com/purcell/exec-path-from-shell). As the name suggests it sets the PATH variable to a more useful value, but it is also able to set any number of environment variables. I use it like this:
(when (and (memq window-system '(mac ns))
This is a simplified version of a function in my .emacs file:
(defun ml/bash ()
"Start a terminal emulator in a new window."
(ansi-term (executable-find "bash")))
You can bind it to C-c a:
(global-set-key (kbd "C-c a") #'ml/bash)
The problem here is that Emacs is moving the cursor position in the buffer, but no input is being sent to the process running in the terminal, meaning that the apparent state does not match the actual state.
This should be reported as a term-char-mode bug, but you can fix it by binding the Ctrl- and Meta-modified left and right cursor keys in term-raw-map ...
You can use "process sentinel" mentioned in @steve-lorimer's answer if it is OK for you to get color AFTER the process is finished, otherwise you should use "process filter" instead.
Take command echo -e "\e[31mHello World\e[0m" as an example, you can simply use comint-output-filter (I learnt this by reading shell-command's source code)
Given the pointer to term-handle-ansi-escape by @jch I was able to add support for CHA as follows:
(defun toolbear:term-handle-more-ansi-escapes (proc char)
"Handle additional ansi escapes."
;; \E[nG - Cursor Horizontal Absolute, e.g. move cursor to column n
((eq char ?G)
(let ((col (min term-width (max 0 term-terminal-parameter))))
Solved it by myself. Just needed to add the following lines to ~/.emacs.d/init.el:
;; ensure that scrolling doesn't break on output
(setq term-scroll-to-bottom-on-output t)))
If you run bash in a term buffer, bash will do all of its normal bash initialisation things, just as it would when you run it inside any other terminal emulator.
So you can set your prompt in your ~/.bashrc file, no differently to the way you do it without Emacs.
If you particularly wish to detect that the shell is running inside Emacs (in order to set ...
From my point of view you should accept Cyberbisson's most useful answer. I'll give you a formal one and the way to determine that information for yourself.
Why can't I use this key
(global-set-key (kbd "M-o") 'other-window)
defined in my emacs config, in ansi-term char mode?
Are all meta keybindings simply passed raw to the terminal process?
When mixing term with shell-mode, the variable comint-use-prompt-regexp seems to get set occasionally. This causes behavior a bit like you've described: in shell-mode C-a will go to the beginning of the line, C-k will kill the whole line, including prompt.
A simple fix is to reset comint-use-prompt-regexp with C-: (setq comint-use-prompt-regexp nil), ...
Saving the last value of (point) and restoring it with goto-char should do the trick:
(defun toggle-maximize-buffer ()
"Toggle maximize buffer"
(if (= 1 (length (window-list)))
(setq my-saved-window-configuration (current-...
Terminal.app starts an interactive login shell, while Emacs starts interactive non-login shells. The Bash manual's section on startup files explains that for login shells, Bash will first source /etc/profile (and on Mac OS this file instructs Bash to source /etc/bashrc, which sets PS1) and then goes on to look for user-specific config files. Interactive non-...
Disclaimer: I assume this is in no way related to Spacemacs.
The evil-emacs-state-modes variable is initialized with a list of modes to start in Emacs state. This is a mishap as it makes it harder to customize it properly, any successful change to the variable will not take this value into account and Evil's code will not change it either because the ...
So, after reading excellent commentary for the original question, I was able to solve my problem in this way:
(defun my-term-hook ()
(define-key term-raw-map (kbd "<M-left>")
(lambda () (interactive) (term-send-raw-string "\e[1;3D")))
(define-key term-raw-map (kbd "<M-right>")
(lambda () (interactive) (term-send-raw-string "\e[1;3C"))...