Is it possible to reuse backgrounded emacsclient windows, when invoking emacsclient?
Here's some background information (I mainly use emacs in terminal mode, not gui frames)
When the computer boots, an emacs daemon is started.
In the OS X terminal when I want to open a file, I do emacsclient /filename -nw
Now when I want to do bash stuff I press C-z to background emacs.
Now emacs appears in the jobs command. The fg command would also
make it re-appear.
But while I'm browsing around in bash, I see another file I want to open.
Now, how can I reuse that minimized emacsclient session with a single command?
Yes, put this in your .bashrc:
ec() {
kill %emacsclient 2> /dev/null
emacsclient -nw --eval '(find-file "'"$PWD/$1"'")'
}
Open files with ec file.txt. It's a bit hacky, but I think it will do what you want it to.
I open the file find-file so it'll stay open after you close the Emacs window (C-x C-c). Then when you open a new file, I kill the old Emacs window and open a new one. The effect is that the old file stays open forever, so it seems like you reused the Emacs window.
Related
I'd like to have a single alias which launches gvim ~/.bash_aliases and then re-sources the file after gvim exits (. ~/.bash_aliases)
For example,...
alias newaliases="gvim ~/.bash_aliases && . ~/.bash_aliases"
or perhaps...
alias newaliases="gvim ~/.bash_aliases ; . ~/.bash_aliases"
However in both examples, gvim runs, of course, asynchronously. This in turn causes the second command to be run as soon as gvim is launched rather than when gvim finishes.
I could discover the gvim's pid and run wait pid prior to re-sourcing. But that seems a bit tedious (e.g., I would need to guarantee I've got the pid of the correct gvim instance). Perhaps it is the only solution?
Use the -f option to gvim, which makes it run in the foreground, so the next command won't run until you finish editing.
-f
Foreground. For the GUI version, Vim will not fork and detach from the shell it was started in. ... This option should be used when Vim is executed by a program that will wait for the edit session to finish (e.g. mail).
I would like to use the mouse features of gvim, but have it behave as normal vim otherwise. For that, I have added the line set guioptions= in my .gvimrc .
I could make an alias to gvim (mapping it to exec gvim), so that when I open gvim from a terminal, it appears to open in the same window.
Is there any way to go back to the starting terminal when I close gvim? (like when you close vim)
:set mouse=a in vim did the trick.
I can open a single file with Emacs, no problem. But I'm used to Emacs on Linux, where typing emacs test.cpp & would open a new Emacs frame, leaving the terminal free. On OS X, however, when I try to open a new frame with emacs test.cpp &, the terminal shows a process was created but the window fails to pop up. If I push Ctrl+C, the terminal shows "Stopped", but I see the process is still running because I can see it in the task monitor.
How to solve this problem? Thanks!
The issue here is that the command emacs on Linux is generally just the Emacs binary itself, which opens a new frame (that's Emacs-speak for X11 or other GUI window) unless you specify -nw on the command line; in contrast, the emacs command provided by Homebrew is a shell script that calls /usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs -nw "$#" (substituting the version installed for 24.3). So the Homebrew version launches as a terminal app.
You can make a shell script that just runs /usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs "$#", and it would behave somewhat as you expect, except that you would be launching a brand-new instance of the Mac app, which would cause an additional Emacs icon to appear on the Dock. Such a thing isn't considered "Mac-like," but it's not the end of the world.
An alternate solution, which is used a lot across different operating systems, is to make one Emacs process a server and then use emacsclient to open files from the command line. Emacsclient can open files in the current terminal, a new frame (GUI window), or an existing instance of Emacs. For an instance of Emacs to run as a server just requires that you run M-x server-start within it, or put (server-start) inside your init file (~/.emacs or ~/.emacs.d/init.el).
My Emacs config has this snippet, which starts server mode automatically when I launch the GUI app:
(when (display-graphic-p)
(server-start))
Then, once Emacs is running (you can make it autostart upon login in System Preferences > Users & Groups > Login Items), type emacsclient -nw test.cpp to open the file in the terminal, or emacsclient test.cpp & to open it in an existing frame, or emacsclient -c test.cpp & to open a new frame. (Note that if you open it in an existing frame, you use C-x # to close the buffer without closing the frame, as opposed to C-x C-c.)
Note that the terminal emacsclient command I gave just now didn't use & but the GUI ones did. & at the end of a command line puts the process in the background, meaning it's not monopolizing your terminal. For whatever reason (probably because it wouldn't be sensible to have a full-screen terminal app running in the background), when you invoke the terminal version of Emacs with &, it just suspends itself. The same thing would happen if you pressed C-z within Emacs. To get it back into the foreground, type fg (actually you can have multiple background processes, in which case fg would just pick the most recent one unless you specified a job specifier; see bash's man page (man bash) and search for JOB CONTROL if you're interested in the details).
I recently started to use Emacs as my main editor. There is one thing that bothers me:
When I start Emacs in my shell it starts Emacs in a new window. I want it to start in the shell if it's called by the shell and in a new window when it's called from the workspace.
Can this be achieved by some configuration that I miss or is it any kind of lisp?
I'm using Manjaro with Xfce and fish-shell (http://fishshell.com/)
Regards,
Robin
So there's the alias solution, but a pb I encounter is that I sometimes make emacs sleep with Ctrl-z and then I forget I have an emacs session launched so I use my alias once again and I end up with two emacs in the terminal, which annoys me.
So I use a function which checks if an emacs is already running:
cemacs () {
if (ps|grep emacs); then
echo "Hey, emacs is already running";
fg %emacs
else
emacs -nw $#
fi
}
Shortcut
I defined a handy shortcut to revive a sleeping emacs:
bind -x '"\C-x\C-e":fg %emacs'
Emacs-server
So that's what I used for quite long, and it isn't perfect. I can not launch a normal emacs and then my function, unless if I use emacs server: http://wikemacs.org/index.php/Emacs_server
Just create an alias to emacsclient -t.
and shell-mode
But now, I much prefer to use a terminal inside emacs (it is so handy to move around the shell's buffer, to copy-paste without the mouse, to look for a string, to go to the beginning of the output, to manipulate files with dired,…).
Good Idea !
I've just updated my .bashrc :
edit_file_in_emacs_console() {
emacsclient -t $# || emacs -q -nw $#
}
if [ "$PS1" ]; then
alias emacs=edit_file_in_emacs_console
fi
So I use current emacs server if available, or a simple emacs console (-q) for faster startup.
Suppose I am in terminal in Emacs (M-x term), and I list the following files in current directory:
text_code.R
Now I am in bash-3.2$ (terminal) and hope to open this .R file in another Emacs buffer and then edit. Is there a way to do it? This might be a trivial question, for I am a newbie to Linux and Emacs. Thanks in advance!
Remember that in Term Mode you can type C-c C-f to open a file (just like C-x C-f outside Term Mode). The prompt will already be on your current directory, so you just have to start typing the name of the file and autocomplete it with TAB.
I don't know the official procedure for what you want to do, but here is a procedure that works:
Either tell emacs to run as a daemon (Ref: EmacsAsDaemon) or in emacs start daemon via M-x server-start.
In the term, a command like emacsclient -n filename will start editing the specified file in the current window. Note, emacsclient also has a -c, --create-frame option to edit in a new frame. You probably will want to use a -n option as above, so you can continue using your term, after selecting it from the buffers list in another pane or frame.
If you start the daemon via M-x server-start in emacs, the daemon will terminate when you exit from emacs. If you set it up via reference mentioned above, use kill-emacs or save-buffers-kill-emacs commands or shell command emacsclient -e '(kill-emacs)' to stop it, as mentioned in part 6 of reference.