How to set emacs to open new files in current instance on Ubuntu/Mint? - emacs

I want to keep emacs open most of the time, and then whenever I open a text file, python file, etc (from nautilus/finder), I want it to open as a new buffer in my current emacs instance, rather than starting a new instance.
I tried following this guide:
I wasn't really sure about what to do with the file, but what I tried was copying it to /usr/bin/emacs_openfile, adding #!/bin/sh to the first line, and running chmod +x emacs_openfile
However, it doesn't work for me and just opens a new instance, even though I was able to associate text files with this program.
On a sidenote, I set emacs as the default editor using this.

You want emacs client.
Basically, set emacsclient as your default editor, and add (server-start) somewere in your emacs config.
There needs to be a running Emacs instance for emacsclient to work, but if it's a hassle it's possible to have a headless Emacs launched at login.

Did you start server in emacs? Make sure that you add (server-start) to your .emacs or do M-x server-start from an existing EMACS session.
Conceptually, it's really easy, just do emacsclient file-name on the command line or where you name the program.

Just for your information, If you are using GUI version on Mac, the dir os emacsclient is below:
/Applications/Emacs.app/Contents/MacOS/bin/emacsclient

Related

Opening an already-open file with GNU Emacs

Suppose that the file A.txt is already opened with GNU-emacs, but I don't notice it, so I try opening it (again) with
emacs A.txt
Instead of just showing the already-open window it opens a second one!
(This is of course annoying, since I will end up with 2 conflicting versions.)
How can I avoid this (under ubuntu) ?
Command emacs starts a new instance of the application.
What you are looking for is emacsclient which connects to an existing emacs instance and tells it to edit the file.
You need to start the server first though.

Make emacs never open up a new frame

How would you set emacs up to never create any new frame. If I want frames, I will do that from inside emacs.
Annoyingly, whenever I click a file from a file manager outside emacs, this opens up a completely new instance of emacs, with all the long loading time going along with that.
Opening a bunch of files, each taking like 5 seconds to finally load is not very convenient.
So what I want to do is this: Whenever I click a file on the file manager, I want that file to be opend up in the one instance of emacs that is already running as a new buffer.
How would I do that?
Using emacs 24.3.1 on Fedora 19 with Gnome 3.8.4
You want to start one single instance of emacs (which should start a server using (server-start) in your ~/.emacs) and then use emacsclient. You probably should
export EDITOR=emacsclient
in e.g. your ~/.bashrc
See invoking emacsclient (in Emacs documentation) and EmacsClient (in Emacs Wiki).
Here's what I did. I have Ubuntu, but I'm pretty sure that
Gnome also uses
/usr/share/applications/.
Here's my /usr/local/share/applications/emacsclient.desktop:
[Desktop Entry]
Name=Emacsclient
GenericName=Text Editor
Comment=View and edit files
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=/usr/local/bin/emacsclient %F
Icon=emacs
Type=Application
Terminal=false
Categories=Development;TextEditor;
StartupWMClass=Emacs
Here's my /usr/local/share/applications/emacs.desktop(just for completeness):
[Desktop Entry]
Name=Emacs
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=emacs %F
Icon=emacs
Type=Application
Terminal=false
Categories=Development;TextEditor;
StartupWMClass=Emacs
The command to update these shortcuts without rebooting is:
sudo update-desktop-database
Now you should have an Emacsclient entry in your file managers "open with" dialog.
Make the associations and the files will open in emacs with a click of a mouse.
Just make sure to have in your ~/.emacs:
(require 'server)
(or (server-running-p) (server-start))

Run latex within Emacs

I used to launch latex within Emacs on a .tex file by C-c C-c.
I just changed my machine, and need to reconfigure all. I have already installed texlive-full, and copied .emacs from the previous machine to the new machine.
When I open a .tex file under Emacs, launch C-c C-c, and then type LaTeX, it opens another buffer, named *tex-shell*, and looks like as so:
Could anyone tell me what happened?
I don't really now what happen, but it seem you want to use auctex:
sudo apt-get install auctex
and relaunch Emacs should solve your problem.

emacs on Windows: .emacs is no valid file name

I've found out where to put my .emacs file, but it seems it can't begin with a ".".
I tried naming it "emacs" or "_emacs", but how can I find out if it is used?
You can always create the file using Emacs itself: C-x C-f ~/.emacs. The ~ represents your home directory, which you can set as environment variable HOME.
Have a look at this page and this one for start up instructions.
If you are creating a file in Explorer, it won't allow you to use a .name (gives this error).
A simple work-around, if you have bash (cygwin, git-bash, or any other variant) installed is to use that to rename the file. It may also work in powershell or command prompt, I've not tested those.
Files can start with '.', this doesn't cause any trouble alone, but explorer won't let you name them with it.
Windows Explorer disallows the creation of filenames starting with a dot. A simple workaround with builtin Windows tools is to create the file with a dummy name (eg. _emacs), then use cmd.exe to rename it:
cd path/to/file
ren _emacs .emacs
Recent versions of windows (e.g. Windows 7) seem to allow creation of a .emacs file using windows explorer. When creating/renaming the file simply enter .emacs. instead of .emacs.
To test if the .emacs that you are editing is the .emacs that is being loaded, you could put the following elisp command in it:
(minibuffer-message "it worked")
Now exit and restart emacs, while watching the minibuffer at the bottom of the screen to see if it appears (it will only appear for 2 seconds).
Windows allows the creation and use of that type of file, but Windows Explorer does not allow a file to be named to that using Windows Explorer. Use another tool (like the command line, or emacs) to create the file with that name.

.emacs Edit to Always Start Emacs in Terminal Mode?

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?
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
I'm using a bash alias instead of .emacs to do that.
Add this line to your ~/.bashrc.
alias emacs='emacs -nw'
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
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.