I would like to use emacs to edit my sudoers file using visudo on my Mac, but for the life of me, I can't get it to happen. I've tried all of the following configuration settings in the sudoers file, to no avail:
Defaults editor = "/usr/bin/emacs"shows theemacsintro screenDefaults editor = "/usr/bin/emacs visudo"creates new file named "visudo"Defaults editor = "/usr/bin/emacs sudoers"opens the sudoers file, but as a read-only buffer- running
EDITOR="/usr/bin/emacs"in the shell (orexporting it to my~/.bash_profilefile), then runningvisudowithDefaults env_keep+="EDITOR"shows theemacsintro screen Defaults editor = "emacs"says theeditorvariable must start with/Defaults editor = /usr/bin/emacsshows the intro screenDefaults editor = "/usr/bin/emacs -u matt"shows the intro screen
Basically, nothing I do works. What am I doing wrong?
I'm running macOS High Sierra (10.13).
81 Answer
The issue is that visudo runs emacs like this: /usr/bin/emacs -- /etc/sudoers.tmp. Emacs doesn't interpret '--' as 'end of arguments' and everything after it as filenames like other editors, but shows the intro screen instead. The easiest way around this I know of is a wrapper:
bash-3.2$ sudo cat > /usr/bin/emacs-visudo-wrapper.sh << 'EOF'
#!/bin/sh
set -eu
ARGS=()
for var in "$@"; do [ "$var" != '--' ] && ARGS+=("$var")
done
exec /usr/bin/emacs "${ARGS[@]}"
EOF
bash-3.2$ sudo chown root:wheel /usr/bin/emacs-visudo-wrapper.sh
bash-3.2$ sudo chmod 755 /usr/bin/emacs-visudo-wrapper.sh
bash-3.2$ sudo cat >> /etc/sudoers << 'EOF'
Defaults editor = "/usr/bin/emacs-visudo-wrapper.sh"
EOF