Nathaniel Flath
Nathaniel Flath

Reputation: 16015

Emacs remote shell

I tend to run my shell in emacs, but when I use it to ssh into another computer this breaks tab-completion. Is there a way to fix this?

Upvotes: 10

Views: 5629

Answers (7)

mandark
mandark

Reputation: 782

I use dired to access the remote machine and open a shell there.

Here is the function I use, taken and modified from Tikhon Jelviss' emacs configuration:

(defun anr-shell (buffer)
  "Opens a new shell buffer where the given buffer is located."
  (interactive "sBuffer: ")
  (pop-to-buffer (concat "*" buffer "*"))
  (unless (eq major-mode 'shell-mode)
    (dired buffer)
    (shell buffer)
    (sleep-for 0 200)
    (delete-region (point-min) (point-max))
    (comint-simple-send (get-buffer-process (current-buffer)) 
                        (concat "export PS1=\"\033[33m" buffer "\033[0m:\033[35m\\W\033[0m>\""))))

Example:

(anr-shell "/vagrant@localhost#2222:/vagrant/")

Upvotes: 0

Ben Hyde
Ben Hyde

Reputation: 1541

M-x shell invoked in buffer A will switch to a shell buffer B; usually shell. Unsurprisingly it creates B if necessary. A prefix arg will cause it to ask for the name of B. If A is viewing something on a remote host then the shell will be run on the remote host. But only if it can't find an existing B. At that point file name completion will work.

There are some notes about how to tinker with this in the emacs wiki. See for example the function my-shell in this section, which will extend the default name for B so remote files get remote shells. I do that same thing by advising the shell function.

The filename auto completion will work fine. Command autocomplete? Less so. For me it blocks emacs and then doesn't actually work.

Upvotes: 0

Mikey Boldt
Mikey Boldt

Reputation: 170

I just wrote a little function to open a shell on a remote host. The cd call before shell gets the tab completion working.

This may be different than you want, since it opens a new shell instead of ssh'ing in a local shell. Beyond that, you could look into hacking emacs Directory Tracking (or see who else has).

(defun remote-shell (&optional host)
  "Open a remote shell to a host."
  (interactive)
  (with-temp-buffer
    (let ((host (if host host (read-string "Host: "))))
      (cd (concat "/scp:" host ":"))
      (shell (concat "*" host "*")))))

(defun myserver-shell () (interactive) (remote-shell "myserver"))

Upvotes: 0

r giar
r giar

Reputation: 21

in another thread, someone mentioned eshell which I've never used but I tried it with SSH and all sorts of nice integration is happening. Time to learn eshell.

Upvotes: 2

cb0
cb0

Reputation: 8613

I had a similar problem I think and solved it by editing my ~/.bash_login on the remove machine and append

export TERM=xterm

I use OS X and had problems when connecting to a Linux (Debian Lenny)

Upvotes: 0

stsquad
stsquad

Reputation: 6032

You could try M-x ansi-term to host your shell if your getting unexpected behavior with key mappings. Having said that I couldn't re-produce the problem your describing on your set-up.

Upvotes: 1

Trey Jackson
Trey Jackson

Reputation: 74430

Try:

M-x cd /hostname:/current/path/in/the/shell

That should set up ange-ftp (or tramp), and then TAB completion for paths should work properly for that shell - until you log into a different machine.

You could set up a comint process filter to recognize when you type ssh to do that for you automatically, but that's difficult to get right as it should revert when you exit the ssh session, but not be tricked by other uses of exit.

For an automated solution, I'd suggest augmenting the approach I personally use to keep Emacs synchronized with the current working directory of the shell buffer. Just add an an extra bit of information with the hostname, and use that to set the hostname and path like shown above.

Upvotes: 9

Related Questions