Launching eshell when starting up emacs from terminal with cwd - emacs

I added a hook to my init.el to launch Emacs on eshell.
;;start on eshell
(add-hook 'emacs-startup-hook 'eshell)
The thing is, I often launch Emacs from the terminal and would love to have eshell working directory being the working directory of the terminal from which I launched Emacs.
Let's say I'm in a directory X, and launch Emacs
~/X $ emacs
I want this to happen
Welcome to the Emacs shell
~/X $
For now, I have added
(cd "~")
to my init.el, as a temporary solution (changes emacs path to home), but it's not good enough.
Edit
I want to run Emacs in its gui.
Edit 2
Chris's answer worked if not using open -a emacs to launch the application. But just with the executable path instead.

You can detect that Emacs is running in a terminal when the function display-graphic-p returns nil, and you can get the directory from which you invoked Emacs from the default-directory variable.
So something like
(when (not (display-graphic-p))
(cd default-directory)
(eshell))
in your init should change to the "current" directory and then launch eshell when Emacs is invoked from the terminal.
If you always wish to invoke eshell you should be able to remove (eshell) from the code and simply keep your emacs-startup-hook.
Edit: Replaced variable window-system with call to function display-graphic-p as recommended in this answer.
Edit 2: If you simply want to modify your emacs-startup-hook to change to whatever directory you invoke Emacs from and then launch eshell (regardless of the windowing system), you can use something like
(add-hook 'emacs-startup-hook
(lambda ()
(cd default-directory)
(eshell)))

Related

Run emacs command in terminal

I'm using Emacs Muse for work reasons and I don't really enjoy editing my .muse files in emacs.
I haven't found alternatives to publishing .muse files in another editor.
Is it possible to run Emacs commands from outside Emacs almost as if using it as a sort of interpreter?
I want to be able to go to the terminal and run something like:
> emacs -ne file_with_command file_to_publish.muse
The command in question is M-x muse-project-publish-this-file
edit: In Emacs, this command also has inputs that it prompts me to give one at a time. It's the style of publishing (html in my case) and the directory where the publication will go to.
muse-project-publish-this-file is not design to use in batch mode.Use muse-publish-file instead.
First,
git clone https://github.com/jwiegley/muse /path/to/muse
Then create script.el with content
(add-to-list 'load-path "/path/to/muse/lisp")
(require 'muse-publish)
(muse-publish-file "file_to_publish.muse" (muse-define-style "newstyle" nil) "/path/to/publish/directory")
notice is /muse/lisp not muse.
Last, emacs --batch -l script.el

cannot open load dired-details

I just installed the emacs package dired-details from inside emacs via
M-x package-list-packages
clicked on the package name and then install in the newly opened buffer.
Then I put those lines into my .emacs:
(require 'dired-details)
(setq-default dired-details-hidden-string ">---< ")
(dired-details-install)
When I restart emacs, I get the following error:
File error: Cannot open load file, dired-details
The interesting thing is, that when I mark the code region above and apply
M-x eval-region
everything works as expected.
emacs --version
>> GNU Emacs 24.3.1
package version:
dired-details-20130328.1119
Packages you installed with package.el need to be initialized if you want to access them during emacs initialization.
Add the line
(package-initialize)
to the very beginning of your .emacs .
Also follow phil's recommendation and see the variable
package-enable-at-startup

Emacs not recognising .emacs.d folder on startup

I'm new to Emacs. I recently got a dot-emacs configuration from GitHub. Cloned the repo in my home directory as .emacs.d
This is for setting up a clojure environment. When i open Emacs and enter
M-x nrepl-jack-in, the minibuf displays 'No Match'
What am I doing incorrectly? Thanks!
It's possible that nREPL isn't being loaded. Add the following to your init.el:
(autoload 'nrepl-jack-in "nrepl" nil t)
You should be able to run it now with M-x nrepl-jack-in.
If the command doesn't work you'll need to do some troubleshooting.
make sure that nREPL is actually installed. You'll need both the Emacs library and the nREPL server.
check for any errors during startup. Launch Emacs from the terminal using the --d flag:
emacs --d
You will get a backtrace if anything goes wrong during the startup.
verify that your init.el is actually being loaded. Add the following code to the end of your init.el and restart Emacs.
(message "---> LOADED")
This will show in your *Messages* buffer if everything is being loaded.
Edit:
Looks like your init.el isn't being loaded. Check your home folder for files called .emacs or .emacs.el, which might be getting loaded instead.
Otherwise it could still be a path issue. Open a terminal and make sure this is the file you expect:
less ~/.emacs.d/init.el

Mac OSX, Emacs 24.2 and nrepl.el not working

I'm using nrepl.el, Emacs 24.2. My S.O version is OS X Lion 10.7.5.
Running the command [M-x] nrepl after start a REPL session through lein (:~ $ lein repl) i am able to connect to it but if i try to use [M-x] nrepl-jack-in i get the message bellow:
error in process sentinel: Could not start nREPL server: /bin/bash:
lein: command not found
I installed leiningen using the instructions in the main site and reinstalled it using homebrew with the command brew install leiningen --devel but both methods give me the same error.
Adding the path /usr/loca/bin to emacs exec-path list or trying to configure nrepl.el variable nrepl-lein-command to point to the full path of my lein installation does not help.
My emacs configuration can be found here: https://github.com/khaoz/emacs-files
What i'm doing wrong ?
Thanks :)
While #Arthur is correct a much simpler solution to your problem would be to install the exec-path-from-shell Emacs extension. It will copy your shell PATH (and MANPATH) to Emacs automatically and fairly reliably. This means that the PATH would be correct no matter where you started Emacs from (spotlight included).
for those of you landing on this question who are using a Mac:
lein needs to be on the path as seen by Emacs. This can be done by starting Emacs from bash
/Applications/Emacs.app/Contents/MacOS/Emacs project.clj
then:
M-x nrepl-jack-in
If this solves the problem you can configure emacs to use the correct path by following these instructions
If you are seeing this error in Linux, you may need to set your PATH in ~/.bash_profile instead of ~/.bashrc when running emacs from the menu instead of the shell.
I had to do this:
;; set the path as terminal path [http://lists.gnu.org/archive/html/help-gnu-emacs/2011-10/msg00237.html]
(setq explicit-bash-args (list "--login" "-i"))
;; fix the PATH variable for GUI [http://clojure-doc.org/articles/tutorials/emacs.html#osx]
(defun set-exec-path-from-shell-PATH ()
(let ((path-from-shell
(shell-command-to-string "$SHELL -i -l -c 'echo $PATH'")))
(setenv "PATH" path-from-shell)
(setq exec-path (split-string path-from-shell path-separator))))
(when window-system (set-exec-path-from-shell-PATH))

How can i paste the selected region outside of emacs?

I am using Mac OS and emacs -nw (the terminal mode).
I don't know how can I paste things (having been implemented by M-w in emacs -nw) outside the emacs.
I know that the emacs -ns can do it.
Searching the internet and the command C-h b, i find out that method, but it didn't work out.
(setq x-select-enable-clipboard t)
(setq interprogram-cut-function 'x-select-text)
I don't know much about the argument of interprogram-cut-function.
Where does the x-select-text come from and what does it mean?
If you are using Ubuntu 12.04 or Fedora 21, there are a couple of options to make this work.
First you need to install xclip
sudo apt-get install xclip
First Option: For Emacs 24
If you are using emacs24 you can install from the list of packages
M-x package-list-packages
Select
xclip //mine was version 1.3
In your .emacs add:
(xclip-mode 1)
Second Option. For emacs before version 24
Install xclip.el:
Integrating Emacs with the X11 Clipboard in Linux
Third Option. Using #Nicholas Riley code shown in the answer
To use the code in the answer you need
pbcopy / pbpaste in Ubuntu (command line clipboard)
x-select-text is only used if you're running Emacs in a GUI. (Emacs maps the Mac/Windows pasteboard/clipboard APIs to the X11 model, hence the name). You can always use C-h f to find out more about a function like this one and view its definition if it's written in elisp.
On the Mac, there is no concept of CLIPBOARD versus PRIMARY selections, so there is no point in setting x-select-enable-clipboard.
The whole point of running emacs -nw is that it doesn't interact with the windowing system. Why use Emacs in a terminal when there are plenty of graphical Emacsen that work very nicely on the Mac?
That said, if you really wanted to hook up terminal Emacs to the Mac pasteboard, you could do something like this:
(setq interprogram-cut-function
(lambda (text &optional push)
(let* ((process-connection-type nil)
(pbproxy (start-process "pbcopy" "pbcopy" "/usr/bin/pbcopy")))
(process-send-string pbproxy text)
(process-send-eof pbproxy))))
If you want a way to place the contents of the emacs region onto the clipboard only sometimes, as opposed to every time you do an emacs yank (which causes the clipboard contents the be overwitten all the time), you should check this answer to a related question:
https://stackoverflow.com/a/19625063/3363328
I found that it solved my problem much better than setting xclip mode.