Emacs uses local emacsserver mode to Open remote files

Source: Internet
Author: User

All users who use emacs know that the TrampMode mode is used to open files on a remote machine. The call method is as follows:

C-x C-f /remotehost:filename  RET (or /method:user@remotehost:filename)
However, it is a little troublesome to open it. You need to enter the user name and machine ip address.

Emacs provides a client/server mode. When an emacs is opened locally, it can be run as a server.

Then, when other local machines use emacs, for example, to call emacs in terminal, you only need to use emacsclient.

I hope the situation is as follows:

1. Use the emacs of the local machine as a server

2. log on to a remote machine using ssh

3. Use emacsclient on a remote machine to call the local emacs server

4. Then, we can edit the files on the remote machine on the local machine.

I found a solution provided by a person on the Internet. The steps are as follows:

Note: here, the remote HOST name is: lisp.

1. Set Port Forwarding (or SSH Tunneling) and edit the local hosts ~ /. Add the following content to ssh/config:

Host yoksUser lispControlMaster autoControlPath ~/.ssh/yoks.sockRemoteForward 9999 localhost:9999
The local port 9999 and remote port 9999 are reflected here (map)

2. Prepare the emacs of the Local Machine and add the following code to your emacs configuration file (. emacs or init. el ).

(setq server-use-tcp t      server-port 9999)(require 'server)(defun server-start-and-copy ()  "start server and copy auth files"  (when (and (fboundp 'server-running-p)   ; function exists.             (not (server-running-p)))    (server-start)                       ; emacs as server mode    (when (file-exists-p "/lisp@yoks:~/.emacs.d/server/server")      (delete-file "/lisp@yoks:~/.emacs.d/server/server"))    (copy-file "~/.emacs.d/server/server" "/lisp@yoks:~/.emacs.d/server/server")    ))(add-hook 'emacs-startup-hook 'server-start-and-copy)
On the one hand, start emacs server mode, set its tcp port to 9999, and then copy the server file to a remote machine (lisp @ yoks ).

At the same time, restart emacs on the local machine.

3. Edit the following script on the remote machine named ec and add the executable permission.

#!/bin/bashparams=()for p in "$@"; doif [ "$p" == "-n" ]; thenparams+=( "$p" )elif [ "${p:0:1}" == "+" ]; thenparams+=( "$p" )elseparams+=( "/ssh:yoks:"$(readlink -f $p) )fidoneemacsclient "${params[@]}"

4. log on to the remote machine using ssh, and then open a file with ec. We found that the file content appeared in the emacs of the local machine, as shown below:

 

The local emacs is as follows, so that the file can be edited on the local machine.

The address is/ssh: yoks:/home/lisp, which is exactly the same as the address opened in TRAMP mode.

Note: My machine environment is Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.