2

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.

8

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 EDITOR and VISUAL environment variables to it in your ~/.profile or ~/.bash_profile or ~/.zprofile:

export VISUAL='emacsclient --alternate-editor='
export EDITOR='emacsclient --alternate-editor='

and make an alias for it in your ~/.bashrc or ~/.zshrc:

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.

Alternatively, run emacs --daemon as part of your session startup and use plain emacsclient to open files.

Note that ~/.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 (or ~/.bash_profile, ~/.profile, etc. depending on your login shell), but OSX does things differently (and actually runs ~/.bash_profile or ~/.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).

  • 1
    I get the error "emacsclient: option '--alternate-editor' requires an argument" when I run emacsclient --a '' - perhaps I am misunderstanding what you mean by empty argument? – Andrew Jun 23 '16 at 22:04
  • @Andrew That's because you aren't actually passing the empty argument. You can't stuff an empty argument in the EDITOR variable, because you can't arrange to have an empty word there. That's why I showed the use of --alternate-editor=, which doesn't require an empty word. – Gilles 'SO- stop being evil' Jun 23 '16 at 22:10
  • Hmm I added the env variables and the alias and it still makes me specify a file before I can connect – Andrew Jun 24 '16 at 15:50
  • @Andrew What commands are you using? What do you mean by “makes me specify a file before I can connect”? What are you trying to do? – Gilles 'SO- stop being evil' Jun 24 '16 at 18:37
  • 1
    I put those lines in my zshrc, and when I type e I get macsclient: file name or argument required Try emacsclient --help' for more information - it is still asking me to specify a file. It works fine if I type e filename. – Andrew Jun 24 '16 at 21:12
0

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?

  • Primarily Ubuntu and osX (unix, not linux tho). – Andrew Jun 21 '16 at 22:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.