Start Emacs in shell when started form shell - shell

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.

Related

shell: reuse backgrounded emacsclient windows, when invoking emacsclient

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.

Open a small window while in Vim for executing commands

I need to write lot of code and compile very often. I hate switching back and forth various windows just to compile the code. Is it possible to open a small window at bottom and run invoke shell and close that window when needed?
With GVim or MacVim, you can run external commands in the command-line: Gvim/MacVim comes with a (very) limited shell that will happily show you whatever the compiler outputs. The general usage pattern is:
:!command
:!command %
With CLI Vim, the same method will pause Vim and return to the shell to execute your command.
In both cases, you'll get a message asking you to press ENTER to come back to your normal editing.
Using :make | cw would be a slightly more sophisticated alternative, with the added bonus of showing the errors in the quickfix window.
An even more sophisticated approach would be to use Tim Pope's Dispatch plugin in combination with tmux or screen.
Sounds like a problem for Screen
http://www.gnu.org/software/screen/
Quick reference of commands
http://aperiodic.net/screen/quick_reference
I use tmux to achieve something like that. I have the following in my ~/.tmux.conf file:
bind s splitw -v -p 25 -c '#{pane_current_path}' '/bin/bash'
bind q kill-pane
On pressing Ctrl-b + s (prefix + s), a new pane containing a bash shell opens up at the bottom. I can run shell commands from there: find, grep, make, etc. When I'm done, I press Ctrl-b + q to close the shell.
To enable tmux on every bash session, add the following to your ~/.bashrc:
[[ -z "$TMUX" ]] && exec tmux
Maybe map a key to shell out to the compiler and run the program if compilation is successful:
:map F8 :!cc % && ./a.out
Or maybe just
:sh
make run
Ctrl-D
Another option is to suspend vi, using Ctrl-Z and do your stuff in the shell, then type fg to bring vim back to the foreground. Note that this is actually a feature of your shell, rather than vim but it produces the effect you seek.
Note this idea originates from the book "Efficient Linux at the Command Line" by Daniel Barrett. I forget the page number.

Implicitly launch detached program (&) from terminal

I feel sure this will end up as a duplicate, but I don't know how to word it so that I can search for it...
I like to do my programming in emacs, which I launch from the terminal. When I use my mac I use aquamacs and the command
aquamacs Program.py
will launch aquamacs in a separate window ready to edit Program.py. When I work on my linux machine however to get the same result I must type
emacs Program.py &
And I'm always forgetting the "&". So 70% of the time I end up closing the emacs window, and relaunching again with the "&". Now I understand why that "&" is there, but is there a way to set up my system so that the command
emacs Program.py
always launches as a detached process? The only time I might not want that behavior is if I was SSHing in over a slow connection, in which case I usually use "-nw" anyway.
You can click on the terminal and press Ctrl-Z to move an already running foreground process to the background.
Alternatively you could add this function to your ~/.bashrc:
emacs() {
command emacs "$#" &
}
For the specific case of emacs, I use a shell script, which is more complicated than this but for your purposes boils down to
#!/bin/csh -f
/bin/emacs $*:q &
Put it in a directory which is earlier in your $path than where the "real" emacs is, and replace /bin/emacs with the real path to emacs on your system.

How to launch GUI Emacs from command line in OSX?

How do I launch GUI Emacs from the command line in OSX?
I have downloaded and installed Emacs from http://emacsformacosx.com/.
I'll accept an answer fulfilling all of the following criteria:
The emacs window opens in front of my terminal window.
Typing "emacs" launches a GUI Emacs window. Finding files in that window will default to looking in the directory from where I started Emacs.
Typing "emacs foo.txt" when foo.txt exists launches a GUI Emacs window with foo.txt loaded.
Typing "emacs foo.txt" when foo.txt does not exist launches a GUI Emacs window with an empty text buffer named "foo.txt". Doing ^X^S in that buffer will save foo.txt in the directory from where I started Emacs.
Call the following script "emacs" and put it in your PATH somewhere:
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#"
That covers #2, #3, and #4.
For #1, put this somewhere in your .emacs file:
(x-focus-frame nil)
The emacsformacosx.com site now has a How-To page, which is where the top snippet came from. There's more info there about running emacsclient and hooking Emacs up to git mergetool.
In your shell, alias the command 'emacs' to point to the OSX emacs application
In my shell (running the default bash), I have the following (in my .bashrc)
alias emacs='open -a /Applications/Emacs.app $1'
Then, typing emacs on the command line starts the emacs application.
I would, however, recommend that you open a copy of emacs and just keep it up and running. If that's the case, and you want to load a file into an existing copy of emacs, you can use the emacsclient by placing the following in your .bashrc:
alias ec='/Applications/Emacs.app/Contents/MacOS/bin/emacsclient'
Then add the following to your .emacs file to start the emacs server (which receives the emacsclient calls)
;;========================================
;; start the emacsserver that listens to emacsclient
(server-start)
Then you can type
ec .bashrc
to load a copy of .bashrc into an existing emacs session!
This improves on David Caldwell's answer by starting Emacs in the background:
#!/bin/sh
$(/Applications/Emacs.app/Contents/MacOS/Emacs "$#") &
As stated in the other answer, this covers #2, #3, and #4. For #1, put this somewhere in your .emacs file: (x-focus-frame nil).
Note that the following does not work for me -- it does not start Emacs in a directory specified on the command line (e.g. emacs .)
# NOT RECOMMENDED
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#" &
I assume you either:
Start the emacs daemon on login
Have (server-start) in your .emacs
Don't mind having lots of separate copies of emacs running
If so, then I think this satisfies the original four criteria, plus one more:
The emacs window opens in front of my terminal window.
it will always open to the foreground (with x-focus-frame).
Typing "emacs" launches a GUI Emacs window. Finding files in that window will default to looking in the directory from where I started Emacs.
It will open an existing emacs window in dired mode.
Typing "emacs foo.txt" when foo.txt exists launches a GUI Emacs window with foo.txt loaded.
If emacs is already running and has a server, then it will open in the existing window and come to the foreground.
Typing "emacs foo.txt" when foo.txt does not exist launches a GUI Emacs window with an empty text buffer named "foo.txt". Doing ^X^S in that buffer will save foo.txt in the directory from where I started Emacs.
Correct.
One extra:
Control returns to the terminal session immediately after typing the command.
~/bin/emacs
#!/bin/bash
EMACSPATH=/Applications/Emacs.app/Contents/MacOS
# Check if an emacs server is available
# (by checking to see if it will evaluate a lisp statement)
if ! (${EMACSPATH}/bin/emacsclient --eval "t" 2> /dev/null > /dev/null )
then
# There is no server available so,
# Start Emacs.app detached from the terminal
# and change Emac's directory to PWD
nohup ${EMACSPATH}/Emacs --chdir "${PWD}" "${#}" 2>&1 > /dev/null &
else
# The emacs server is available so use emacsclient
if [ -z "${#}" ]
then
# There are no arguments, so
# tell emacs to open a new window
${EMACSPATH}/bin/emacsclient --eval "(list-directory \"${PWD}\")"
else
# There are arguments, so
# tell emacs to open them
${EMACSPATH}/bin/emacsclient --no-wait "${#}"
fi
# Bring emacs to the foreground
${EMACSPATH}/bin/emacsclient --eval "(x-focus-frame nil)"
fi
On Mountain Lion, I am using Yamamoto Mitsuharu's port https://github.com/railwaycat/emacs-mac-port with the following alias:
alias emacs=/Applications/Emacs.app/Contents/MacOS/Emacs
and it satisfies all of your criteria.
Just built emacs with homebrew package manager according to this guide:
http://www.emacswiki.org/emacs/EmacsForMacOS
with brew install --cocoa emacs
After that one should launch the .app version to get gui, which in my case was /usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs
Further improving on David James' response the following works for me:
Per instructions to open a file from a terminal found at http://www.emacswiki.org/emacs/EmacsForMacOS#toc20
open -a /Applications/Emacs.app <file-name>
combining this with David Jame's response I've created the following emax bash script and placed it in my path at ~/bin
#!/bin/bash
(open -a /Applications/Emacs.app "$#") &
Caveat: in order to get emacs to open the current directory in Dired by name mode, you need to use
emax .
Environment:
OS X Yosemite Version 10.10.2
GNU Emacs 24.4.2 (x86_64-apple-darwin14.0.0, NS apple-appkit-1343.14)
of 2014-11-13
Simple solution...
A lot of very complex solutions to this problem are posted here. That's fair because it seems non-trivial.
However, this solution works really well for me.
ec() {
emacsclient -n $# 2> /dev/null
if [[ $? == 1 ]]; then
open -a Emacs.app -- $#
fi
}
Usage
ec file [...]
Let's unpack what's happening:
pass all the ec arguments to emacsclient and don't (-n) wait for emacs before continuing.
If Emacs is already running, we're all done and you're editing.
swallow up the error message posted by emacsclient when there's no emacs running. (2> /dev/null)
Manually handle the exit code 1 ([[ $? == 1 ]])
open Emacs.app and pass file arguments to it (paths will be correctly opened.)
You're all done, and Emacs has opened your files.
The other answers here didn't quite work for me. In particular, on my machine, the bash script
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$#"
always opens emacs in the home directory. To get it to open in the current working directory, I had to do
#!/bin/sh
/Applications/Emacs.app/Contents/MacOS/Emacs "$PWD/$#"
instead.
Compile Emacs according to the following steps:
./configure --with-x --prefix=/usr
make
sudo make install
And your done! It may help to download and install XQuartz, but that's just my opinion.
This is my script for open emacs/emacsclient on osx.
#!/bin/bash
# Ensure (server-start) is added in your emacs init script.
EMACS=/Applications/MacPorts/Emacs.app/Contents/MacOS/Emacs
EMACSCLIENT=/Applications/Macports/Emacs.app/\
Contents/MacOS/bin/emacsclient
# test if client already exsit.
$EMACSCLIENT -e "(frames-on-display-list)" &>/dev/null
# use emacsclient to connect existing server.
if [ $? -eq 0 ]; then
$EMACSCLIENT -n "$#"
# open emacs.app instead.
else
`$EMACS "$#"` &
fi
In all of the above when using "open" - make sure you use the "--args" option
Do not do this:
alias emacs='open -a /Applications/Emacs.app $1'
Instead this:
alias emacs='open -a /Applications/Emacs.app --args $1'
the --args option prevents "open" from consuming various options intended for Emacs.
The top answer is good, but I wanted the emacs process to run in the background so I could still use my shell. This answer appeared to do what I wanted, but didn't start emacs in the right directory, meaning absolute paths were required (or hacks to append pwd to the paths which wouldn't work in all cases). Furthermore, simply using & meant that if I killed the terminal, emacs would also be killed.
I decided to use screen and a bash function, and the following solution works for me on macOS 10.15.6 and emacs 26.2 installed with brew:
function emacs() {
screen -d -m /Applications/Emacs.app/Contents/MacOS/Emacs "$#"
}
For the meaning of the -d -m command line flags, they have a special meaning when used together and so can essentially be thought of as one command line flag. The explanation is in the manpage:
Start screen in "detached" mode. This creates a new session but doesn't attach to it. This is useful for system startup scripts.
open_emacs() {
num=$(ps aux | grep -E "[E]macs-x86_64-10_14 --|[e]macs --" | wc -l)
if [ $num -eq 0 ]; then
echo "## starting emacs"
# Run in a subshell to remove notifications and close STDOUT and STDERR:
(&>/dev/null emacsclient -t -q &)
fi
}
alias e="open_emacs"
Following line (&>/dev/null emacsclient -t -q &) will start the emacs daemon if it is not running on the background.
macOS may have defined the app name starting with E (ex: Emacs-x86_64-10_14.app), based on that you can check whether the emacs daemon running on the background or not.
Just want to update a response to this question. Since it is still a relevant question, but now there is an easier solution:
brew install --cask emacs
When this installs Emacs, it does the behavior you requested, without further intervention. It even runs the Emacs Server on startup.
Files installed/linked by default:
ebrowse -> /Applications/Emacs.app/Contents/MacOS/bin/ebrowse
emacs -> /Applications/Emacs.app/Contents/MacOS/Emacs
emacsclient -> /Applications/Emacs.app/Contents/MacOS/bin/emacsclient
etags -> /Applications/Emacs.app/Contents/MacOS/bin/etags
BTW, this is now a recommended way of installing Emacs on MacOS:
https://www.gnu.org/software/emacs/download.html#nonfree

How to start "emacsformacosx" in terminal

I am using MAC OX 10.6 , and install the emacs from here http://emacsformacosx.com/
I want to know how to start it in terminal, so my ecb can open current directory
It is actually quite easy, just run it from terminal like this:
/Applications/Emacs.app/Contents/MacOS/Emacs -nw
the -nw option means to start emacs without the gui frame.
You can put the following in your shell (on my mac .zshenv) :
alias Emacs="/Applications/Emacs.app/Contents/MacOS/Emacs -nw"
Then I just have two commands:
Emacs : for emacs version 24
emacs : for the apple version of emacs
Of course you can just alias the Emacs.app to emacs, but this allows me to customize the two differently - for instance Emacs 24 allows me to use list-packages and so forth. emacs 22 ignores most of this, so I can always revert to a 'bare metal' emacs if need be. Your usage may vary, but if you don't remember the arguments to emacs you can find them by doing this:
emacs --help
Some interesting ones:
Emacs.app --fullscreen
Emacs.app --line-spacing
Emacs.app --vertical-scroll-bars
More info here : http://www.gnu.org/software/emacs/manual/html_node/emacs/Option-Index.html#Option-Index
The answer from #Toymakerii is a good one, but you might also consider adding:
export PATH=/Applications/Emacs.app/Contents/MacOS/bin:$PATH
This way, you can use emacsclient to open files in an already-running Emacs instance:
emacsclient -t SOMEFILE # Open SOMEFILE in a terminal frame
emacsclient -c SOMEFILE # Open SOMEFILE in a new graphical frame
Depending on your Emacs version, you might need to put the following in your ~/.emacs.d/init.el (or ~/.emacs, if you're old-fashioned):
(require 'server)
(unless (server-running-p)
(server-start))
In my ~/.profile i have the following:
function emacs
{
if [ -e "$#" ]
then
command open -a emacs "${#}"
else
touch "$#"
command open -a emacs "${#}"
fi
}
(The reason for having a function is to make it also work when the specified file does not yet exist when emacs is started)
By default terminal will open /usr/bin/emacs on OS X.
You can change this behavior by changing what the "emacs" command will do. Open up ~/.profile and type the following:
alias emacs=open /Applications/Emacs.app
The next time you open a prompt this change will be active. (or you can run "source ~/.profile")
The easiest is to simply do
open /Applications/Emacs.app --args foo
An alias would then be
alias emacs=open /Applications/Emacs.app --args "${#}"
or in csh/tcsh
alias emacs 'open /Applications/Emacs.app --args $1'
edit: this seems to need a full path to open the correct file... I don't know if this is a problem with Emacs.app or with tcsh

Resources