16

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.

gsgx
  • 522
  • 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 04 '16 at 17:41

3 Answers3

10

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.

haziz
  • 2,929
  • After setting EDITOR how would you open Emacs in graphical mode? – Manolete Mar 02 '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 04 '16 at 07:52
  • 1
    @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 03 '16 at 11:11
9

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
McNisse
  • 1,863
  • 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 06 '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 09 '18 at 20:13
  • Why is this better?

    In my case because, -nw or any other flags are ignored, when editor is invoked from midnight commander (use internal editor flag in mc is set to false so that F4 invokes $EDITOR)

    – Nikolay Hidalgo Diaz May 20 '22 at 20:03
2

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=\\\"\\\"