19

I use emacs as my editor-of-choice, and since I'm doing a lot of work in a terminal I always run emacs as

emacs -nw

so that it runs in the terminal instead of in a window.

I'd like to just run emacs and have it know that it should run in a terminal. My question is - how do I edit my .emacs file so that this is the default behavior?

GEOCHET
  • 21,119
  • 15
  • 74
  • 98
templatetypedef
  • 362,284
  • 104
  • 897
  • 1,065

4 Answers4

29

You can't do this in the .emacs file. By the time that file is being parsed, the "chosen" emacs binary is already running.

You can install the emacs-nox package as one commenter suggests, or create an alias in your shell so that "emacs" is always treated as "emacs -nw".

Randy

rjray
  • 5,525
  • 4
  • 31
  • 37
20

I'm using a bash alias instead of .emacs to do that.
Add this line to your ~/.bashrc.

alias emacs='emacs -nw'
tototoshi
  • 1,156
  • 1
  • 9
  • 14
3

There is any easy way to solve the problem in general that has nothing to do with emacs at all and will work for any program that can choose between running in the console vs X:

unset DISPLAY

Of course you may not want to put that in your configuration file to be applied globally to all your shell sessions, so if you want it to apply to only emacs, then either call it from the command line like this:

DISPLAY= emacs

note the space!!! if you leave the space out it means you're setting the DISPLAY to emacs instead of setting DISPLAY to nothing... this command is a shorthand for:

DISPLAY=; emacs

So either use the above from the command line(s) or put that in a wrapper script that would look something like this:

#!/bin/bash
unset DISPLAY
exec emacs

I recommend the exec there because it will replace your wrapper script with emacs; to see the difference between the two you can run:

pstree -p
aculich
  • 14,545
  • 9
  • 64
  • 71
  • 1
    btw: I am curious- why do you WANT to always run emacs in a console if you are running in an X environment? – aculich Jan 13 '11 at 13:46
  • That third command with a semicolon is *NOT* the same as the second command. If you add the semicolon it sets DISPLAY to empty in the current shell as well as in the emacs child process. Without the semicolon it only applies to emacs. With semicolon it's destructive. – Thomas Sep 30 '16 at 09:50
  • Why would you want a billion windows open? Also I do all my coding work inside 'screen', which doesn't make sense with X. @aculich – Thomas Sep 30 '16 at 09:51
1

When I was first setting up a "emacs -nw" alias for emacs in windows I got stuck in a situation where I thought tototoshi's explanation hadn't worked. Yet all that was required was a restart of my terminal. Therefore, i think its worth mentioning that in windows (at least) if you are using emacs within the git bash terminal to create the .bashrc file and add "alias emacs='emacs -nw" to it (as tototoshi mentions) you have to close and reopen your terminal for it to work.

Naruto Sempai
  • 6,233
  • 8
  • 35
  • 51