Wiki
Clone wikiVIT-Projects / emacsasdefault
Emacs as Default Editor
When we want to look around, navigate, ie browse our files on a computer we use a file browser such as Gnome/Unity Nautilus (now called Files )
By default when you click on a text/C/Python/other-programming file in the file browser, gedit opens the file.
Do you like this default?
If instead, you want
- emacs to open the file
and - It to be the same emacs (ie different buffers in One emacs, not different emacs sessions)
you need to do the following
Below is written for Nautilus. It should apply to any other also with few/no changes.
Shortform emacsclient to ec
Add this line to ~/.bash_aliases
[Create the file if necessary]
alias ec='emacsclient -n -a "" -c'
After this from a new shell you should find that
$ ec somefile
will open somefile. [Shortform for typing "emacsclient"]
However the point of all this is that you dont want to (typically) open emacs from a shell.
You want to do it from nautilus
Setup nautilus to open in emacs
Create emacs.desktop
Put the following into the file ~/.local/share/applications/emacs.desktop
[Desktop Entry]
Name=EmacsClient
GenericName=Text Editor
Comment=Edit text
MimeType=text/english;text/plain;text/x-makefile;text/x-c++hdr;text/x-c++src;text/x-chdr;text/x-csrc;text/x-java;text/x-moc;text/x-pascal;text/x-tcl;text/x-tex;application/x-shellscript;text/x-c;text/x-c++;
Exec=emacsclient -n -a "" -c %F
Icon=emacs
Type=Application
Terminal=false
Categories=Development;TextEditor;
StartupWMClass=Emacs
Start emacs server
Choose ONE of these ways. Both have minor disadvantages (bugs!)
- Put
(server-start)
in emacs'init.el
or -
Start emacs
$ emacs --daemon
Warning
If emacs is running as server C-x C-c
is not guaranteed to kill emacs. It just removes the frame and maybe saves some buffers. You can check this from a shell with
$ ps -ef|grep emacs
It will show something like:
$ ps -ef |grep emacs
<your name> 4153 1460 0 08:35 ? 00:00:25 emacs --daemon
Therefore when you want to reload init.el
now you need to
- Save and close all files
- Explicitly
M-x
kill-emacs
For safety, check from shell that there is no running emacs
THIS ALSO MEANS YOU CAN LOSE FILES IF YOU SHUT THE MACHINE WITHOUT SAVING
[Normally you'd see a window -- now you may not]
Usage
Use from file browser
Now in Nautilus,
- click on a file you want opened in emacs
- Click properties → open-with
Here you should findEmacsClient
[NOT plainemacs
] You may have to select "other applications" - Click on "View all applications" → Select EmacsClient
Note: After making the emacs.desktop
file you may need to reboot
if it does not appear in Nautilus (actually logout of Unity/Gnome is enough)
Make it default
- Right Click on the same file (any file with same extension)
- Click Properties at bottom
- Tab "OpenWith"
- Make EmacsClient as "default application"
- Click "Set as Default"
After this you should find that all files of that extension will by default open in emacs from nautilus.
Check this works before proceeding
Browse from emacs or browser
Earlier you could open a single file with
$ emacs file
And for multiple files the convenient way was to 'visit' them from inside emacs.
Now you can send them to emacs from the file-browser. You can even drag-n-drop them onto emacs!
Using Nautilus as default file opener
- Warning: This section more iffy and experimental
Once you get used to calling out to emacs from nautilus, you may want to go further and have nautilus as your default file-handler in/for emacs.
Note: The default file-opener for emacs is C-x C-f
[Menu → Visit New File is much the same; just looks different ]
It kinda sux to such an extent that a whole cottage industry of improvements has sprung up
Frankly Ive not had too much success with any of these and keep falling back to default C-x C-f
(sux!)
Here is an attempt to just call out to Nautilus for those who prefer it
Add this to emacs' init.el
(defvar openFileProgram "/usr/bin/xdg-open")
(defun open-using-file-browser ()
"Open using OS's file browser"
(interactive)
(let ((process-connection-type nil))
(start-process "" nil openFileProgram default-directory)
))
(define-key menu-bar-file-menu "o"
'("File browser Open" . open-using-file-browser))
After restarting emacs, the file menu should have a new entry "File Browser Open"
Clicking that in emacs will (should!) startup Nautilus
You can go further and do
(global-set-key (kbd "C-x C-f") 'open-using-file-browser)
[Not recommended!]
Making Nautilus more efficient
In Nautilus In Edit → Preferences
- View → Choose View folders by list rather than Icon to get more files seen
- Reduce default zoom level will also show more files in a window
- Behavior → Single Click to open files
Updated