One of my main reasons for using Emacs is to get a powerful environment after sshing into a remote machine. The first thing I do after starting Emacs is to run M-x shell to get a command line. As it takes a little while for Emacs to start it would be very convenient to have an option to ask Emacs to start a shell when I run it, after parsing my initialization file (I have some shell hooks set up to do one thing and another). Ideally I'd like to be able to run it with an additional option so I wouldn't have to add anything specific to my initialization file on the remote machines (I could set up a bash alias if I wanted to). Something like this:

emacs afile1 afile2 -startShell

In fact, given that establishing the secure connection takes about 30 seconds, what I'd really like to do is build the "start a shell" into the ssh command itself, using ssh with -t to start a screen-based program.

ssh -t me@remote emacs afile1 afile2 -startShell

I took a look at the command line arguments documentation for Emacs but I'm still starting out on Emacs and Lisp and customization and it wasn't immediately obvious how to proceed to deal with command line arguments. How would I go about doing this?

(Related question on eshell mentioned by user @Name.)

  • it can :). ssh -t me@remote 'emacs -f shell' works like a dream.
    – TooTone
    Commented Feb 12, 2015 at 14:19
  • @Name thanks I tried it, but I was saying you could add your comment as an answer and I'd accept it because it addresses my problem. Or perhaps my question should be marked as a duplicate. It would be good not to leave it open, either way.
    – TooTone
    Commented Feb 12, 2015 at 15:36
  • possible duplicate of How to use Emacs to open an eshell from Terminal at a specific path Commented Feb 12, 2015 at 15:53

1 Answer 1


You can use the -f switch to call an emacs function from the command line.

The comment shows how to do this over ssh using -t and that is one way. However, it does have some disadvantages. The main one is that your running emacs remotely, which means you will need to have all your emacs config stuff on the remote system as well as maintain some locally for your local version. You also lose some flexibility if you want to do stuff locally and remotely and perhaps move in-between.

An alternative would be to run your emacs locally and start a shell from the command line and then ssh from that shell. You can use tramp over ssh to edit files etc and then use the shell to run an interactive ssh session.

emacs -f shell

Once your comfortable with that, you could look at some of the ssh related packages out there for emacs. It should then be possible to call a function from one of those packages from the command line to automate the creation of an ssh session. You may need to write a simple wrapper function to make this easier. Then you will have only 1 emacs config setup to manage and you get the additional flexibility.

  • 1
    You're right in that the main disadvantage of using ssh -t ... 'emacs -f shell' directly from a terminal is that I have to keep a separate, remote emacs configuration up to date. I've played around with tramp, and now I've reduced the startup time to an acceptable level with ssh connection sharing it's usable and extremely helpful for, e.g., scp completion of remote filenames. However it's still not a seamless solution for running within a single session, and its disadvantages at present outweigh its advantages.
    – TooTone
    Commented Feb 17, 2015 at 20:24
  • Other ssh options which may help include getting the right level of ssh compression. What are the single session inconveniences you are experiencing?
    – Tim X
    Commented Feb 17, 2015 at 21:36
  • I had a problem yesterday where a file I opened was read-only, and also when I do ssh the prompt displays badly with some ^ prefixed characters. Today I tried again and editing files is seamless, but the prompt is still messed up. Also my suspicioun is that not everything will be seamless. E.g. running ag set off a process on the remote machine but I had less luck doing an rgrep. It certainly seems like this is worth looking into some more (github.com/ieure/ssh-el on melpa looks interesting) when I have time -- and posting back any qs to this site; thankyou for raising it.
    – TooTone
    Commented Feb 19, 2015 at 13:31
  • Depending on your use case, getting tramp exactly right can be a challenge, but it is worth the effort. The messed up prompt sounds like you may have ansi-colour escape characters. Perhaps the ansi-colour package needs loading. Also, try just using tramp without any configs - the defaults are usually spot on. Tamp is powerful, but complex to configure at times.
    – Tim X
    Commented Feb 19, 2015 at 23:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.