using emacs for visudo editor not opening /etc/sudoers file on Mac

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 the emacs intro screen
  • Defaults 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 (or exporting it to my ~/.bash_profile file), then running visudo with Defaults env_keep+="EDITOR" shows the emacs intro screen
  • Defaults editor = "emacs" says the editor variable must start with /
  • Defaults editor = /usr/bin/emacs shows the intro screen
  • Defaults 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).

8

1 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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like