- How do I find out what my default terminal text editor is? [duplicate]
- 3 Answers 3
- Set your default terminal editor in Linux
- Set nano as your editor in bash
- Set vim as your editor in bash
- Set emacs as your editor in bash
- How to open the default text editor in Linux?
- 2 Answers 2
- Command for the default in-terminal text editor
- 4 Answers 4
How do I find out what my default terminal text editor is? [duplicate]
While reading the git introductory material, I read a piece that brought this question to mind. How do I use the Terminal to find out what my Default Text Editor is? Are there a General Commands that I can use to find this out?
3 Answers 3
In Ubuntu, there is a generic editor command which is set by the Debian alternatives system.
the file will be opened using the original editor e.g. vim , nano which is prioritized as editor currently.
You can check the details with:
update-alternatives --display editor
To set a new editor as editor :
sudo update-alternatives --config editor
Also note that bash checks some environment variables for tasks related to it, to be exact bash checks VISUAL , EDITOR one after another. If unset, bash defaults to nano (unless an editor you’ve installed has overridden this).
Some processes spawned from bash check these environment variables too.
This is a peculiarity specific to Ubuntu, because it has the Debian alternatives system. On systems, without it git has to check EDITOR variable
Actually no I’m sure it can’t be emacs , since emacs is not installed by default. Installing emacs probably overrode your previous configuration.
The default editor is as defined by the EDITOR , or VISUAL , environment variable(s).
The default editor is vi if neither were defined. Add
to your ~/.bashrc file to set, for example, nano as your default editor.
To see if the environment variable is set, you can use
One can dereference the value of the named environment variable by prefixing it with a «$»
@heemayl: Thanks for your edit, as I forgot to format properly. However, I very specifically wanted to say $EDITOR and not EDITOR . Why? Because one refers to the environment variable by preceding the name with a $ sign. For example $ $EDITOR bla.txt or echo $EDITOR or . So, I am going to reverse the deletion of the $ sign.
@heemayl: I’ll do an edit,see if you like it, and then delete these comments. It will be a few hours until I can do it though.
@DougSmythies , identifiers (aka names that define i.e. a variable) are language and context dependent. if you’re going to use the $ sign, it should be to dereference the variable, meaning that you’re accessing the value of the variable. you don’t use the $ sign when assigning a value to that variable; so when you refer to the name of one, you do not use it.
There is actually git var -l which allows you to list the variables, including GIT_EDITOR variable. Here’s mine (private info is unset of course):
$ git var -l user.name=***** user.email=**** GIT_COMMITTER_IDENT=**** GIT_AUTHOR_IDENT=**** GIT_EDITOR=editor GIT_PAGER=pager
As heemayl already pointed out, editor command is the one set by /etc/alternatives/editor . In my case, that’s nano (which I assume is default for Ubuntu, because I don’t remember consciously making an effort to change my default editor).
But on other systems other than Ubuntu (or I should say which have no Debian’s alternatives system) , there is no editor . Let’s, however read up the man git :
GIT_EDITOR
This environment variable overrides $EDITOR and $VISUAL. It is used by several Git commands when, on interactive mode, an editor is to be launched. See also git-var(1) and the core.editor option in git- config(1).
And if we look through git-var it tells us
The order of preference is the $GIT_EDITOR environment variable, then core.editor configuration, then $VISUAL, then $EDITOR, and then the default chosen at compile time, which is usually vi.
Thus it is a mere perculiarity of Ubuntu that it has Debian’s alternatives system. On other systems which don’t have Debian’s alternatives systems it would default to vi
Set your default terminal editor in Linux
While others may have this the other way around, I feel clumsy in everything that doesn’t have vim-like keybindings. Mainly speaking about you here nano .
Luckily we can configure our preferred terminal editor for our shell environment. Whether it is nano , vim , emacs or something exotic. This setting is taken from the EDITOR and VISUAL environment variables.
The difference between the EDITOR and VISUAL variables is that the editor you set to EDITOR should be able to work with more advanced interactive terminal functionality.
These variables are set for your shell environment, so depending on whether you use bash, zsh or fish you should edit the correct shell configuration file. For bash that is ~/.bashrc , and for zsh ~/.zshrc .
Set nano as your editor in bash
Assumed you use bash as your shell, add the following to your ~/.bashrc file to set nano as your terminal editor.
# ~/.bashrc export EDITOR='nano' export VISUAL='nano'
Set vim as your editor in bash
Assumed you use bash as your shell, add the following to your ~/.bashrc file to set vim as your terminal editor.
# ~/.bashrc export EDITOR='vi' export VISUAL='vim'
Set emacs as your editor in bash
Assumed you use bash as your shell, add the following to your ~/.bashrc file to set emacs as your terminal editor.
# ~/.bashrc export EDITOR='emacs -nw' export VISUAL='emacs'
How to open the default text editor in Linux?
I need to open the default text editor in Linux without having a file. I know that I could use the command xdg-open to open a file in the default editor, but I need to open the editor without having a file and let the user create the file. I solved it with this script:
#!/bin/sh cd /usr/share/applications/ atalho=`grep $1 defaults.list | tail -1 | sed "s:^$1=::" ` `grep '^Exec' $atalho | tail -1 | sed 's/^Exec=//' | sed 's/%.//'` &
2 Answers 2
There is no completely reliable concept of «default editor» on Linux, let alone more broadly Unix-like systems.
Traditionally, users would set the environment variable EDITOR to the path of their editor of choice. If this variable is set, I’m thinking you can be reasonably confident that they will know how to use it, even if they end up in something horrible like nano .
A slightly newer convention is to set VISUAL to the preferred «visual editor» — I guess the terminology comes from vi to contrast against line editors like ed .
On Debianish systems, the system default editor is configurable via alternatives and available simply with the command editor .
On XDG systems, of course, you could simply
touch path/to/new/file.txt xdg-open path/to/new/file.txt
Needless to say, this only works if you have XDG, i.e. In practice a Linux (or maybe modern *BSD) platform with an active graphical session (excludes Mac and pre-XDG graphical systems as well as of course any server environment where there is no GUI).
As an aside, if I can guess even roughly what your script does, it could probably be pared down to a fairly simple sed script. Remember, sed can do (almost) everything grep and tail can. Maybe see also Combining two sed commands — here is a quick and dirty refactoring.
cd /usr/share/applications $(sed -n "s:^Exec=\([^%]*\)\(%.\(.*\)\)*:\1\3:p" "$(sed -n "s:^$1=::p" defaults.list | tail -1)" | tail -1) &
However, from quick googling, it looks like /usr/share/applications/defaults.list is specific to OpenDesktop environments; but it’s the system-wide default default — the admin could have installed an override in a different location, and individual users probably have individual preferences on top of that. Finding and traversing this hierarchy is precisely what xdg-open does, so I’m not going to try to reimplement it in an ad-hoc script of my own, and suggest you shouldn’t, either.
There is nothing about graphical environments in your question, so it’s unclear whether you are actually looking for a simple editor for beginners who barely know how to click and drool in a graphical environment (in which case I’d say go with touch followed by xdg-open ) or a competent programmers’ editor which way or may not run in a window (maybe try VISUAL with fallback to EDITOR , and document that you use this mechanism).
Command for the default in-terminal text editor
I often see instructions that include vim or nano , meaning to open the file in that step in your text editor of choice. Is there an agnostic command I can use in place of the specific program that would open the input in the user’s default in-terminal text editor, whether it’s vim , nano , or something else? I see editor mentioned in the Similar Questions sidebar—is that still limited to Debian-based distros? And is there any alternative?
in Debian you can also configure the program executed when you call «editor» by executing once: «update-alternatives —config editor». There you can select the one you prefer (default is nano). This will e.g. make so that when you press «v» in less you get the editor you want, so you can switch from nano to vi.
4 Answers 4
You can use $EDITOR , provided that it’s been defined:
But I think most docs use nano because if someone’s blindly following along, it’s a safe bet to use. If the user has decided they actually prefer one editor over another, they’ll know enough to replace it with vim , emacs , etc themselves.
edit may work well on Debian-based systems, but on others it invokes ex , which isn’t recommended.
$EDITOR only makes sense if it’s defined. Invoking ed hardly counts as standard and is abysmal in terms of user-friendliness. edit is perfectly fine on Debian, but may invoke vi on other systems (where does it invoke ex?).
Gilles: You’re right, it only makes sense if it’s been defined. It was the closest thing to a «universal» command per the OP’s question. «Ed is the standard» is an old joke, since it’s the one defined in the POSIX standard (see «man ed» or Google «Ed is the standard» for more info). I don’t know about «edit» on other systems, but mine invokes ex (Arch Linux), so it can’t be considered universal.
POSIX also defines vi. Jokes are fine, but don’t present them as something serious — I‘ve heard that one before but your target audience hasn’t.
If the environment variable VISUAL is set, use that.
Otherwise, if the environment variable EDITOR is set, use that.
Otherwise, Unix tradition defaults to vi . This is not at all user-friendly — people who use vi know how to set up their system to invoke it, your application should be friendly to those users who don’t. Unfortunately there’s no good, portable way to find a decent editor. You can try xdg-mime query default , but even where the utility is available, it doesn’t always work. On Debian and Debian-like systems, invoke sensible-editor , which does all that stuff for you — but I don’t know of anything like it on other Unix variants.
#!/bin/sh if [ -n "$VISUAL" ]; then exec $VISUAL "$@" elif [ -n "$EDITOR" ]; then exec $EDITOR "$@" elif type sensible-editor >/dev/null 2>/dev/null; then exec sensible-editor "$@" elif cmd=$(xdg-mime query default ) 2>/dev/null; [ -n "$cmd" ]; then exec "$cmd" "$@" else editors='nano joe vi' if [ -n "$DISPLAY" ]; then editors="gedit kate $editors" fi for x in $editors; do if type "$x" >/dev/null 2>/dev/null; then exec "$x" "$@" fi done fi
Most programs do whitespace splitting on $VISUAL and $EDITOR , but not all.