11

Scenario: I'm in zsh and I want to edit a long shell command in an already open instance of Emacs. I've set $VISUALto emacsclient -n -c. When I run C-x C-e from zsh, a new Emacs frame opens, populated with contents of the zsh line. After editing and saving the buffer, I hit C-x k, and buffer closes. The zsh line remains unchanged. Why isn't zsh recognizing the changes made in Emacs?

Emacs 24.5.1, zsh 5.0.8, Mac OS X 10.10.4

cg433n
  • 285
  • 2
  • 6

1 Answers1

14

Set VISUAL to emacsclient -c (or some other variation without -n).

The option -n causes emacsclient to return as soon as it's contacted the running Emacs instance to tell it to edit the file. The program calling the editor (here, zsh, but this applies equally to any other program that invokes $VISUAL) knows that you've finished editing because the program that it invoked has exited, which, with -n, happens essentially immediately.

You need to arrange for the program invoked through VISUAL to exit when you've finished editing, i.e. when you close the buffer in Emacs. That's exactly what emacsclient without -n does: it contacts the running Emacs instance to tell it to open the file, then sits around until Emacs replies to tell emacsclient that the editing session is finished. The normal way to finish an editing session is to press C-x # (server-edit). This notifies emacsclient and closes the buffer, as well as the frame when it was opened by emacsclient -c. Closing the buffer also notifies the emacsclient process that the session is finished, but you get a warning prompt (“Buffer … still has clients, kill it?”), which you can disable by overriding the function server-kill-buffer-query-function (RMS thinks it's dangerous). In a frame opened by emacsclient -c, pressing C-x C-c (save-buffers-kill-terminal) saves the file, closes it and closes the frame.

  • `-n` will also prevent emacsclient from working as an editor for git. – cg433n Jul 08 '15 at 22:07
  • A tangent (and I'll put it in another question if you want), but what uses does the `-n` flag have? – Squidly Jul 09 '15 at 09:45
  • 4
    @MrBones Say I'm in a file manager or on the command line and I want to edit a file. Not “edit a file and then do something with it”, just start editing it. If I ran a separate instance of Emacs per file, I'd run `emacs myfile &` (or the Windows equivalent), putting the Emacs process in the brackground. With emacsclient, I could run `emacsclient myfile &`, but that leaves an `emacsclient` process whose sole job is to wait, then exit. Instead I run `emacsclient -n myfile`, so the emacsclient process goes away by itself. This is so common I have a shell alias for it: `alias e='emacsclient -n'` – Gilles 'SO- stop being evil' Jul 09 '15 at 10:04
  • 1
    @Gilles I realised that I only ever run `emacsclient` in the terminal, which is why I couldn't see the point of `-n`. Thanks – Squidly Jul 09 '15 at 12:06
  • 2
    @MrBones, I would say running emacsclient in a terminal is the precise reason for using -n: after opening sending the file to the open Emacs frame, emacsclient terminates and one may continue to work in the terminal. This is exceptionally handy. – Daniel Jul 09 '15 at 18:00
  • Seven years later and not the original commenter, but I got the impression that "in the terminal" meant "with the purpose of staying in and editing in the terminal." – Amory Apr 16 '21 at 11:10