Wanted to share how my sessions are organized.
Requirements:
- Send file from terminal to custom emacs session
- Restore session from terminal
- Save session
Solution:
Install package(plugin) workgroups2 -> https://github.com/pashinin/workgroups2
Add following lisp code to your ~/.emacs.d/init.el or ~/.emacs:
->
(setq server-socket-dir "~/.emacs-local/server")
(defun nk-server-start (custom-server)
; (nk-server-start "abe")
(setq server-name custom-server)
(server-start) ; run emacs server
(setq wg-session-file (concat "~/.emacs-local/sessions/" custom-server))
; (setq wg-session-file "~/.emacs-local/sessions/foo")
(workgroups-mode 1)
(wg-switch-to-workgroup custom-server)
)
; Run file in specific server (foo)
; emacsclient -n callback.sh -s ~/.emacs-local/server/foo
; Show server name in title bar
(setq frame-title-format '("" "%b @ " server-name))
; https://www.emacswiki.org/emacs/FrameTitle
; ;; What to do on Emacs exit / workgroups-mode exit?
(setq wg-emacs-exit-save-behavior 'save) ; Options: 'save 'ask nil
(setq wg-workgroups-mode-exit-save-behavior 'save) ; Options: 'save 'ask nil
Function nk-server-start
is called when emacs is started.
It has one argument - sesssion name.
We can start emacs-session foo
by running following command from terminal:
setsid emacs --eval '(nk-server-start "foo")' &
If we want to open file in session foo
from terminal we need to run:
setsid emacsclient -n -s ~/.emacs-local/server/foo file.txt >> /dev/null &
When we close session, all buffers,settings, etc. are saved in file ~/.emacs-local/sessions/foo
When we run command setsid emacs --eval '(nk-server-start "foo")' &
next time, all buffers will be restored
Because commands are large and I am lazy :) I made some scripts and added them to my $PATH
in order to ease this:
em_start_foo.sh
- Run session, used only once to start session
#!/bin/bash
setsid emacs --eval '(nk-server-start "foo")' &
em_foo.sh
- Add files to session
#!/bin/bash
setsid emacsclient -n -s ~/.emacs-local/server/foo "$@" >> /dev/null &
Now we just run from terminal:
$ em_start_foo.sh # start foo session
$ em_foo.sh file_1.txt # open file_1.txt in foo session
$ em_foo.sh file_2.txt file_3.txt # open file_2.txt and file_3.txt in foo session
Multiple sessions can be run in parallel of course.
Let's say we created also scripts em_start_foo_2.sh
, em_start_foo_2.sh
, em_start_foo_3.sh
, em_start_foo_3.sh
(somewhere in $PATH
of course)
Then we can do something like this:
$ em_start_foo.sh # start foo session
$ em_start_foo_2.sh # start foo_2 session in separate emacs
$ em_foo.sh file_1.txt # open file_1.txt in foo session
$ em_foo_2.sh a.txt b.txt # open a.txt and b.txt in foo_2 session
$ em_start_foo_3.sh # start foo_3 session
$ em_foo_3.sh tmp.txt # open tmp.txt in foo_3 session
##### Close emacs foo_2 from gui - session is automatically saved ###
$ em_start_foo_2.sh # start foo_2 session with all buffers restored
Package workgroups2
is really great!
My emacs init file with session options is available at: https://github.com/nexayq/dot_files/blob/master/emacs/dot_emacs_d/init.el