I have a tiny script in my path on the remote host at ~/bin/ec
, shorthand for emacsclient.
#!/bin/bash
params=()
for p in "$@"; do
if [ "$p" == "-n" ]; then
params+=( "$p" )
elif [ "${p:0:1}" == "+" ]; then
params+=( "$p" )
else
params+=( "/ssh:z:"$(readlink -f $p) )
fi
done
emacsclient --server-file=$HOME/.emacs.d/server/server "${params[@]}"
This script passes -n
and +
args unchanged to emacsclient, otherwise args are treated as files for your local Emacs to open. Each file is prefixed with the TRAMP protocol and host so Emacs knows how to open it. You might be able to change ssh:
to a different TRAMP protocol if you prefer.
You must replace z
with the hostname of your remote machine. This is used by local Emacs to connect via TRAMP. (You might be able to use hostname
here for generality. I prefer to use tiny entries like z
in my local ssh_config
for brevity, and the remote has no idea I'm doing this. Try it!)
Usage:
ec file
in the remote shell opens file in local Emacs and waits
ec -n file
in the remote shell opens file in local Emacs and returns
export EDITOR=~/bin/ec
in remote .bashrc
makes the magic happen
To ensure my server
file is good I have this in my local .emacs
, again using the tiny hostname z
:
(setq server-use-tcp t
server-port 9999)
(defun server-start-and-copy ()
"Start server and copy server file to remote box."
(interactive)
(server-start)
(copy-file "~/.emacs.d/server/server" "/z:.emacs.d/server/server" t)
(chmod "/z:.emacs.d/server/server" (string-to-number "644" 8))
)
(add-hook 'emacs-startup-hook 'server-start-and-copy)
Port 9999 is a RemoteForward. I put this in my local ~/.ssh/ssh_config
to automate the forwarding, plus the ControlMaster stuff for speed.
Host z
HostName dev.example.com
User dev
ControlMaster auto
ControlPath ~/.ssh/z.sock
RemoteForward 9999 localhost:9999
Finally, make sure TRAMP knows about your ssh_config
if you use it:
(require 'tramp)
(tramp-set-completion-function "ssh"
'((tramp-parse-sconfig "~/.ssh/config")))
with-editor
library relates to the question? Sounds useful – Malabarba Oct 08 '14 at 16:18$EDITOR
variable to something on the remote machine for this to work? Does it just hook intoemacsclient
? – Tikhon Jelvis Oct 08 '14 at 17:40M-x async-shell-command crontab -e RET
over TRAMP and it just opens the crontab buffer for editing in your local emacs instance. You then save withC-c C-c
– Silex Oct 09 '14 at 08:26git-modes
. I'm following this lib closely and it's just that its author (@tarsius) is busy with releasing magit, but eventually it'd be a package of its own. About $EDITOR, you don't need to set it to anything yourself, it's done when needed when you run whatever commands uses it. Magit uses this lib with $GIT_EDITOR. – Silex Oct 09 '14 at 08:36$EDITOR
is set when I run an emacs command that uses it? But what if I just runcrontab -e
from a shell buffer—how wouldcrontab
know what to call if not from$EDITOR
? – Tikhon Jelvis Oct 09 '14 at 17:33shell-mode
/term-mode
buffer. This can be addressed with some additional configuration, see http://emacs.stackexchange.com/questions/5589/automatically-update-default-directory-when-pwd-changes-in-shell-mode-and-term-m. If you do get it to work, then please report your findings at https://github.com/magit/magit/issues/1638. – tarsius Dec 20 '14 at 17:47with-editor
for something like "crontab -e
" works for me, but how do I start editing an arbitrary file from anM-x shell
? I tried "$EDITOR file.txt
", but get the error message ""WITH-EDITOR:: 1: "WITH-EDITOR:: Syntax error: Unterminated quoted string
" – asjo Sep 02 '17 at 11:23ansi-term
and thenM-x with-editor-export-editor
and then ssh (from this ansi-term session) on a different computer (hoping to use my local emacs as the EDITOR) and runcrontab -e
, I getemacsclient: can't find socket; have you started the server?
. In short it looks like nothing changed and my local machine is still trying to use its default editor. Is there something else to configure? – Startec Sep 05 '17 at 04:05M-x shell
, therewith-editor
would work. See @tarsius comment above. – Silex Sep 10 '17 at 11:53