I want to have .bashrc check to see if an emacs server is currently running, and if not, start one. I know how to start the server but I am not sure how to check whether it is already running - should I just grep the processes for Emacs? Curious if anyone has something like this set up.
The functionality is built into Emacs. Run
emacsclient and pass it the
-a (long form
--alternate-editor) option with an empty argument, and it'll start Emacs (in daemon mode, i.e. initially without any window) if it isn't already running.
emacsclient -a '' # in sh syntax emacsclient --alternate-editor= # anywhere whitespace-separated command and arguments work
If you always use this command to open files, Set the
VISUAL environment variables to it in your
export VISUAL='emacsclient --alternate-editor=' export EDITOR='emacsclient --alternate-editor='
and make an alias for it in your
alias e='emacsclient --alternate-editor='
Add the option
-c if you want to open a new Emacs window to edit the file. With
emacsclient -c, if you don't pass a file name argument, you get a new Emacs window showing whatever buffer is at the front of the buffer list.
emacs --daemon as part of your session startup and use plain
emacsclient to open files.
~/.bashrc runs every time you open a terminal, not when you log in. On normal Unix systems, the file that runs when you log in is
~/.profile, etc. depending on your login shell), but OSX does things differently (and actually runs
~/.profile and not
~/.bashrc when you open a terminal due to a combination of bad design in OSX and bad design in bash: OSX opens a login shell in each terminal and bash doesn't load
.bashrc in login shells — see https://unix.stackexchange.com/questions/110998/missing-source-bashrc-mac-terminal-profile).
I recommend that you create a startup script, with the command
emacs --daemon at startup. Then there is no need to start Emacs server anymore.
See also this link.
Which Linux distro are you running?