12

When editing files like sudoers, I want to use emacs instead of nano. So I ran this command

sudo update-alternatives --config editor

And I selected emacs. The only issue is I like emacs in no window mode (the -nw flag) and I've aliased emacs as emacs='emacs -nw' so that I can use no window mode in normal use, but I don't know how to get my default editor to be in no window mode.

In other words, I need to get the command sudo visudo and similar commands that open up editors to open the file with emacs -nw. How can I do this? I'm on Ubuntu 12.04.

  • This is unrelated, but if you're intending to use `emacs -nw' as your default terminal editor, you may want to look into emacsclient and running emacs as a daemon. Otherwise, I would think that it would be far too slow to startup. With an emacs server, it's practically instant startup, and not difficult to set up. – Dodgie Nov 4 '16 at 17:41
8

Create a script that starts emacs with -nw flag, e.g. /usr/local/bin/emacs-nw.

#!/bin/sh

emacs -nw "$@"

Install it with update-alternatives --install.

sudo update-alternatives --install /usr/bin/editor editor /usr/local/bin/emacs-nw 2

Configure editor to be your new script.

sudo update-alternatives --set editor /usr/local/bin/emacs-nw
  • 2
    For those who don't use update-alternatives featured systems, chmod +x /usr/local/bin/emacs-nw and export EDITOR='emacs-nw' in .bashrc do the trick. – smonff Sep 6 '14 at 19:54
  • 1
    Simply updating the EDITOR shell variable to call emacs -nw is more traditional and doesn't involve all this weird file creation for such a simple task. Why is this better? – vaer-k Aug 9 '18 at 20:13
4

Add the following to your ~/.bashrc file (or the config file for your shell if it is not Bash).

export EDITOR="emacs -nw"

This should set (and export) an env variable setting your default editor as Emacs in non-graphical mode.

  • After setting EDITOR how would you open Emacs in graphical mode? – Manolete Mar 2 '16 at 14:59
  • 2
    @Manolete you can use command emacs, this will run Emacs without aliases and such. Of course you can also unalias emacs for a session. – Kyrremann May 4 '16 at 7:52
  • @Manolete Actually launching emacs with emacs at the command line or clicking on it's icon will still launch the graphical form. The command above in my post just sets the EDITOR enviromental variable to "emacs -nw" it actually does not create an alias. It just tells the shell that your preferred editor is emacs -nw. emacs will still launch the graphical form. – haziz Jul 3 '16 at 11:11
1

I have following setting in my ~/.bashrc

export EDITOR="emacsclient -t -a=\"\""

This will first try to connect emacs daemon server if it is already started, otherwise start daemon server first then connect again.

Similarly, I have following setting in my ~/.gitconfig

[core]
    editor = emacsclient -t -a=\\\"\\\"

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.