Search code examples
bashemacsdot-emacsemacsclient

How can one specify a user's emacs init file to load with emacsclient?


Question

How can one specify a user's emacs init file to load with emacsclient?

emacsclient does not understand '-u user' and '-a ALTERNATE-EDITOR' does not allow me to quote it and provide '-u user'.

For example, running:

/usr/bin/emacsclient -n -a '/usr/bin/emacs -u <username>' ~<username>/notes.txt

returns

/usr/bin/emacsclient: error executing alternate editor "/usr/bin/emacs -u <username>"

Background

I'm using emacs version 23.1.1 and emacsclient version 23.1.

emacs itself supports '-u user' to load a specified user's init file.

I use the following bash function in my aliases file to invoke emacs

# a wrapper is needed to sandwich multiple command line arguments in bash
# 2>/dev/null hides
#   "emacsclient: can't find socket; have you started the server?"
emacs_wrapper () {
  if [ 0 -eq $# ]
  then
    /usr/bin/emacsclient -n -a /usr/bin/emacs ~<username>/notes.txt 2>/dev/null &
  else
    /usr/bin/emacsclient -n -a /usr/bin/emacs $* 2>/dev/null &
  fi
}
alias x='emacs_wrapper'

Typing x followed by a list of files:

  • Connects to an existing emacs server if one is running, otherwise starts a new one
  • Executes as a background process
  • Opens the list of files or my notes file if no files are provided

This works great when I'm logged in as myself. However, many production boxes require me to log in as a production user. I've separated my aliases into a bash script, therefore I can get my aliases and bash functions by simply running.

. ~<username>/alias.sh

Unfortunately, this won't let me use my .emacs file (~<username>/.emacs) :(

This problem has been driving me crazy.


Solution

  • If you can't include command line arguments in your alternate editor specification, then simply write a shell script which does that for you, and supply that as the alternate editor argument instead.

    #!/bin/sh
    emacs -u (username) "$@"