Search code examples
emacstramp

Automate remote access in emacs


I'm a beginner in using emacs. I would like to simplify the ssh access to a remote server via tramp. My first naive trial was recording a macro. However, calling the saved macro after a re-start of emacs I receive a "byte-code: Args out of range: [], 0" message. Instead one can also defining a environment variable

(setenv "SECRET" "/ssh:user@secret.tld:")

Is there a better solution? I would like to have a definition where I type M-x connection-name and get my pre-defined ssh-tramp-connection set up.


Solution

  • Probably the easiest solution is to configure each of your desired hosts in ~/.ssh/config, e.g.

    Host some-name               # The friendly name of this host
    HostName real-host.name.tld  # The host to connect to
    User foo                     # The user to connect as
    
    Host other
    HostName 123.45.67.89
    User bar
    

    This has the benefit that you can now ssh some-name or ssh other on the command line, and when you try to edit a file using Tramp's SSH you can type

    /ssh:
    

    and then tab-complete for some-name or other. If you use ido you'll get automatically prompted without tab completing.

    There are many other options that can be used in your ~/.ssh/config file, like setting default tunnels and tweaking authentication modes.

    If Tramp doesn't respond properly when you tab-complete, set the completion function for SSH like this (this should be the default value):

    (tramp-set-completion-function "ssh"
     '((tramp-parse-sconfig "/etc/ssh_config")
       (tramp-parse-sconfig "~/.ssh/config")))
    

    Finally, if you want not to be prompted for your password, I recommend using SSH key-based authentication.