How to close a emacsclient gui frame without killing the server - emacs

How do you close an emacsclient frame without killing the server/daemon?
Doing C-x C-c or C-x 5 0 in the gui will exit both frame and server (and other clients), while this just quit the current frame when done from terminal.
(Ps: My emacs version is 24.2,gtk. I've tried without init-file to see if it was coming from my config, but results were the same)

From the minibuffer, when emacsclient starts:
When done with a buffer, type C-x #

I think the key point here is you need to start the server in daemon mode. If the server runs in an existing emacs process - for instance if you have (server-start) in your init file - the server dies with the emacs process.
There are a couple of ways of starting the server in daemon mode:
call emacs with the --daemon command line option
call emacsclient with the --alternate-editor="" command line option, which will in effect run emacs --daemon for you then attempt to connect to it
Once you have the server in daemon mode, you should be able to close any frames and the daemon will continue to sit and wait for further connections.

Starting the emacsclient with -n/--no-wait flag will:
Don't wait for the server to return
You won't need to close the client in that case.
And if you have to a close emacsclient just kill the buffer the server will remain intact.

Because you ran (server-start) from an existing frame, that frame is not an emacsclient frame, and closing the last such frame evidently gets the same treatment as C-x C-c: it will exit Emacs.
If you used emacs --daemon, possibly using the ALTERNATE_EDITOR='' trick, you would not have this problem; you could just use emacsclient -n -c to open a GUI frame, and after closing it the daemon would just sit around waiting for you to open a new frame, whether graphical or terminal-based.

I am using both Debian with i3, and OSX. I usually exit emacsclient GUI frame by the window manager's close window shortcut. For example, in OSX, I use "Command + w" to close the client GUI frame without killing the server, and in i3, I use "Super + Shift + q" (the default close window by i3) to exit the client frame.

In modern version GNU Emacs(27+), it says
When done with this frame, type C-x 5 0

Related

How can I save a typical startup workflow in emacs?

I find myself doing this often:
launch emacs
shh into a server by C-x C-f [RET] /ssh:me#aserver.com:filetoedit.py [RET]
C-x 2 to split windows
C-x o to switch to top window
M-x shell to start a shell on my ssh connection
I there a way to automatically replicate this process on startup? M-x desktop-save doesn't seem to be doing this for me.
Despite my enthusiasm for emacs, I recommend you to take a look at "tmux".
You run emacs in tmux only once. Later on, say when you are back in the office the next day, you simply re-attach to the tmux session and will find your emacs instance is still there.
You don't really specify a lot about your setup, and I agree tmux may help. In my case, I do a version of what you are asking about daily. I'm running GUI emacs 24 on my Mac, I'll be connected to work via VPN with several tramp buffers and shells open. Then I'll just sleep the Mac, commute to work, re-establish the VPN connection and then continue as if nothing happened with the tramp buffers, and after running M-x shell in the shell buffers, continuing as if nothing happened.
To repeat this for several file/shell pairs I would probably save the window config to a register with C-x r w X and C-x r j X for different values of X.

Why won't a new emacs window open in x11

When I type emacs & in X11, a new window doesn't open. Instead I just get the PID of the process. This illustrates what I mean.
bash-3.2$ emacs &
[1] 38624
Why is this happening and how can I get emacs to open in a separate window?
You can't start terminal Emacs as a background job -- it needs to interact with the terminal. Whenever a background job attempts to write to the tty, it is immediately stopped.
I would expect that your next input into the terminal would have resulted in a message similar to this?
[1]+ Stopped emacs
(which would have been useful).
You could then foreground the job with fg.
Running emacs & only makes any sense with GUI Emacs.

Closing emacs in emacs

Occasionally when using emacs in term mode I will mistakenly run emacs file instead of just opening the file. This will create a nested emacs client inside the current client. My problem is how to close the inner client only?
Answer
You should be able to C-z out of it, then kill it with
kill %1
Explanation
C-z will suspend the current process, assigning it a job number and returning you to the shell.
The jobs command will show you the current jobs and their numbers. kill allows you to kill a process by its job number using the %n syntax.
Just use the command M-x kill-emacs inside the inner emacs. Backgrounding and killing it works fine but it is a little bit more hackish.
You should use the top Emacs. Starts emacs with:
emacs --daemon
Starts all frame with:
emacsclient -c
From your term:
emacsclient -n
Or you should use eshell instead.

How to kill a remote process without killing the shell (C-c C-c also kills the shell...)

Assume you work on a remote server from a local emacs shell (started with M-x shell). If you execute a process which takes too long (for example an ls in a directory with a huge amount of files), you would like to stop that process (in the terminal one would use control-c). I tried C-c C-c but that kills the whole shell... How can I only kill the running process on the remote server and not the whole shell?
Update
Step-by-step:
1) M-x shell (execute on local machine)
2) ssh mylogin#myremotemachine.foo.bar (execute on local machine)
3) sleep 60 (execute on remote machine)
4) C-c C-c (execute in the current shell)
=> instead of still being on the remote host and just the sleep command being killed, the whole remote connection is closed and you are on your local machine again.
When I follow the steps you listed, I find myself back at the remote shell prompt. I'm using Emacs 23.1.1.
I find that typing C-q C-c RET also interrupts the remote process, again leaving me at the remote shell prompt. You might try that as an alternate approach.
It is unclear to me if you want to kill the command from within emacs, but you could alternately open another connection or window (if you are using screen or tmux) and perform the standard
ps aux | grep hungprocess
and then
kill hungprocess_pid
For some reason I kept thinking that such a method wouldn't work and that I'd end up taking down emacs as well. I have used such a method multiple times when I haven't set up my gpg pinentry agent when opening a gpg encrypted file in emacs. Emacs/gpg will try to spawn a GUI and therefore hangs emacs, waiting for input in the GUI when I am remotely ssh'ed in.

How can I make emacsclient just open a window for an existing emacs daemon without opening a new file

I use an emacs daemon to preserve my emacs session even if I have to reboot the machine that I run my X server on or if I want to access the same session from a different machine. This works very well but when restoring a session I'd quite like to just run "emacsclient --create-frame --no-wait" to connect to the daemon without opening a new file. It won't let me get away without specifying a filename.
I've tried using --eval to execute a function rather than open a file but the window just goes away when the evaluation is complete.
(Emacs 23.1 via backports on Debian GNU/Linux 5.0.)
From the help provided by emacsclient, you have a few options. First, is the one mentioned already which is emacsclient -c. That will try to create a frame associated with the emacs daemon. The advantage to this is that if DISPLAY is not set, then it will open emacs in the terminal.
Which brings us to the next best option (especially if you are logging in remotely): emacsclient -t which forces emacs to open up in terminal mode even if DISPLAY is set.
Also keep in mind that you can set the display from the command-line as well. I use this often when logging in remotely from VNC. The full command would be emacsclient -d DISPLAY -c
emacsclient -c works for me.
emacsclient -n -e "(make-frame)"
The -n flag means that the emacsclient doesn't wait, and the emacs instance doesn't destroy the frame.
If you are using emacs from the command line, you might also want to consider emacsclient -t