122

How can I change my default text editor from gedit to Emacs?

1
  • 2
    It is answered here: askubuntu.com/questions/977538/… - basically you want to export EDITOR=emacs - and perhaps add that to your .bashrc so it becomes your default.
    – SDsolar
    Nov 17, 2017 at 21:24

12 Answers 12

124

Here is what worked in my case:

  1. Right click on a text file.
  2. Choose "Properties" (not "Open With...")
  3. Click on the "Open With" tab.
  4. Choose your new text editor.
  5. Mark chosen text editor using a button "Set as default".

This also works on 12.04 and 13.04.

@ Edit: based on comments it does work on all distros until 20.04

18
  • 1
    worked for me on 13.10 (replaced gedit with geany) Jan 29, 2014 at 9:35
  • 1
    Works on 16.04 too!
    – Dženan
    Sep 5, 2016 at 15:02
  • 2
    Also works on 17.04 Jul 25, 2017 at 15:38
  • 3
    This works only on the per filetype basis..
    – vstepaniuk
    Feb 16, 2018 at 17:46
  • 2
    Also works on 18.04
    – AlainD
    Aug 15, 2019 at 9:31
68

To change default text editor across the file types, try updating gnome-text-editor configuration.

sudo update-alternatives --config gnome-text-editor

In some cases:

sudo update-alternatives --config editor
3
  • 4
    How can someone add another option? In my case I get the following: There is only one alternative in link group gnome-text-editor (providing /usr/bin/gnome-text-editor): /usr/bin/gedit - EDIT: found how to do it: sudo update-alternatives --install /usr/bin/gnome-text-editor gnome-text-editor /path/to/executablesublime 100 in my case /snap/bin/subl outoutted by which subl.
    – ttt
    Oct 21, 2021 at 10:58
  • 1
    Thanks for this answer! Same here, I added subl first: sudo update-alternatives --install /usr/bin/gnome-text-editor gnome-text-editor $(which subl) 100. Then it was already selected as editor, as the above command then confirmed.
    – Janos
    Jan 21, 2022 at 11:58
  • 1
    This is the one you need to ensure your editor choice persists even when running programs using wsl.exe <program> in WSL. (Specifically sudo update-alternatives --config editor)
    – daviewales
    Feb 19, 2023 at 23:10
18

I don't use a DE, but for my configurations the next command is the best:

/usr/bin/select-editor 
  • it selects your default sensible-editor from all installed editors
  • must run with current user
  • you must have more than one editor in your system

Man pages select-editor

2
  • 2
    +1 Works in 18.04. Does not affect other users. Does not need sudo.
    – Roux
    Apr 23, 2020 at 5:11
  • After setting EDITOR to vi, VISUAL to vi, linking /etc/alternatives/editor or whatever to vi, I still got the pest named nano as the editor when running commands like virsh edit myvm. Applying a level of self-control I would never have guessed I could reach, I am not venting here. NOT VENTING AT ALL!!!! I renamed nano to nano.deleted, then I got several lines of error messages whenever I edited something. select-editor didn't change that. What did help was rm /usr/bin/sensible-editor followed by ln -s /usr/bin/vi /usr/bin/sensible-editor. Life is good. May 27, 2021 at 6:39
15

A more robust solution would be to replace the bindings in /usr/share/applications/defaults.list.

sed -i 's/gedit/emacs/' /usr/share/applications/defaults.list
2
  • I don't know how, but by copying this command I accidentally cleared the whole file. I would much prefer doing this in a text editor using find-replace (as explained in this answer) for those who are not too familiar with sed.
    – metakermit
    Apr 5, 2013 at 22:37
  • I did this (in a text editor with find-replace, which should have the same result), then logged out and back in but still C++ header files (*.h) are opened in gedit. Dec 14, 2016 at 15:06
12

Right click on a text file, point to "Open With" and it'll show other editors in a sub-menu. Click on "Other Application...". It'll show you a dialog with a list of applications, select Emacs and make sure the "Remember this application for "plain text document" file" option is checked. Click "Open".

4
  • 1
    I did try this, but I'm having a bit of a problem - whenever I double-click on a file in Nautilus, I get a dialog box that says: "Do you want to run "tasks.css", or display its contents? "tasks.css" is an executable text file." And then there are four options - Run in Terminal, Display, Cancel, Run. (This happens with every file, not just CSS files.)
    – begtognen
    Nov 16, 2010 at 12:05
  • 5
    @begtognen: Sorry for the ultra-late reply. But I've been trying to find a solution for this myself. Just found one - go to Nautlius preferences (Edit > Preferences), select the "Behavior" tab and under the "Executable Text Files" section, select the radio button "View executable text files when they are opened". Fixed the issue for me.
    – Mussnoon
    Nov 22, 2010 at 23:28
  • I tried this also, but Emacs doesn't appear in the list of applications. Any thoughts?
    – MTS
    Apr 1, 2014 at 19:44
  • No "Remember this application" there. When outdated you might want to either delete your answer, or clearly specify for what versions of Ubuntu it works. Thanks! :-) Nov 21, 2016 at 2:52
9

If you are working from the terminal then I would add the following to your .bashrc file (or the config file for your favorite shell):

export EDITOR=emacs
export VISUAL=emacs
2
  • 5
    +1 for this answer. Particularly if you're a user and don't have sudo or don't want to modify anyone else's preferences Dec 13, 2019 at 15:01
  • Add these to ~/.bashrc and run source ~/.bashrc to make this permanent.
    – Peaceful
    Feb 28, 2023 at 10:17
4

You can set the default text editor for a specific user in # ~/.local/share/applications/mimeapps.list by:

[Default Applications]
text/plain=gedit.desktop

For global configuration for all users you have to modify the /etc/gnome/defaults.list

3
sed -i 's/gedit/emacs/g' /etc/gnome/defaults.list
3

On Ubuntu 20, you need to change the gnome-text-editor alternatives link from gedit to the one you want. However, there usually is only one such editor detected so update-alternatives --config won't work; you'll have to add the editor you want.

E.g. to add notepadqq:

sudo update-alternatives --install /usr/bin/gnome-text-editor gnome-text-editor /usr/bin/notepadqq 50
sudo update-alternatives --config gnome-text-editor
3

For some reason I had to use xdg-mime:

xdg-mime default org.gnome.gedit.desktop text/plain # For current user

you can use your system-wide .desktop files under /usr/share/applications/ or local files under ~/.local/share/applications/ or create your own.

edit: It's also helpful to know that user configurations are stored in ~/.config/mimeapps.list which overrides the defaults. source: https://stackoverflow.com/a/64850938/9085151

2
  • Probably your application is opening the archive with xdg-open. To see if it is the case open htop in terminal without closing the opened file and find your open file in the tree. Aug 26, 2021 at 8:33
  • @R.W.Prado I've changed my OS to debian since then, so I don't have access to that system anymore. But now I can easily change it with update-alternatives.
    – etzl
    Aug 26, 2021 at 10:53
2

If you would like to replace gedit with any other text editor for all file types, the easiest is to edit the defaults.list file located here:

/usr/share/applications/defaults.list

Just find and replace all gedit.desktop references with your own editor (in this case emacs.desktop). You need root permissions to edit the file. So, just do:

sudo -H gedit /usr/share/applications/defaults.list

Save the file, close it and it's done.

-1

To set Pluma as default text editor for all user (global):

sudo sed -i 's|text/plain=gedit.desktop|text/plain=pluma.desktop|g'  /etc/gnome/defaults.list

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .