6.2 How do I get rid of ‘^M’ or echoed commands in my shell buffer?

Try typing M-x comint-strip-ctrl-m RET while in shell-mode to make them go away. If that doesn’t work, you have several options:

For tcsh, put this in your .cshrc (or .tcshrc) file:

if ($?INSIDE_EMACS && $?tcsh)
    unset edit
    stty -icrnl -onlcr -echo susp ^Z

Or put this in your .emacs_tcsh or ~/.emacs.d/init_tcsh.sh file:

unset edit
stty -icrnl -onlcr -echo susp ^Z

Alternatively, use csh in your shell buffers instead of tcsh. One way is:

(setq explicit-shell-file-name "/bin/csh")

and another is to do this in your .cshrc (or .tcshrc) file:

setenv ESHELL /bin/csh

(You must start Emacs over again with the environment variable properly set for this to take effect.)

You can also set the ESHELL environment variable in Emacs Lisp with the following Lisp form,

(setenv "ESHELL" "/bin/csh")

The above solutions try to prevent the shell from producing the ‘^M’ characters in the first place. If this is not possible (e.g., if you use a Windows shell), you can get Emacs to remove these characters from the buffer by adding this to your init file (see How do I set up an init file properly?):

(add-hook 'comint-output-filter-functions #'comint-strip-ctrl-m)

On a related note: if your shell is echoing your input line in the shell buffer, you might want to customize the comint-process-echoes variable in your shell buffers, or try the following command in your shell start-up file:

stty -icrnl -onlcr -echo susp ^Z