How to get value of user-emacs-directory from shell? - emacs

I'm writing a script to automatically install some elisp. I'd like to find the value of user-emacs-directory so that I can copy the file to the right directory. Is there any way to do this from the shell?
I was hoping it would work to run:
$ emacs --batch --eval="(print user-emacs-directory)"
Symbol's value as variable is void: user-emacs-directory
but as you can see that was not fruitful. Then I tried loading my .emacs file first:
$ emacs --batch -l ~/.emacs --eval="(print user-emacs-directory)"
Loading /Users/noah/dotfiles/emacs/init.el (source)...
Symbol's function definition is void: global-visual-line-mode
but that also chokes. Googling hasn't led me to an answer yet. Any ideas?
edit: The specific problem was that I was using an old version of Emacs (22.1.1). Running Emacs 24.4.1 works:
$ emacs -l ~/.emacs --batch --eval="(print user-emacs-directory)" 2>/dev/null
"~/dotfiles/emacs"
(which is the custom value I've set in my own ~/.emacs file)
Note that I'm redirecting stderr to /dev/null to suppress some loading messages.

Works for me on vanilla GNU Emacs 24.5.1 shipped with Ubuntu:
$ emacs -Q --batch --eval="(princ user-emacs-directory)"
~/.emacs.d/
note princ instead of print to avoid extra whitespace and quote marks.

--batch implies -q which means there is no user-emacs-directory
edit: Ugh. No, -q doesn't mean that at all. As sds points out, the proposed code should probably be working. That'll teach me to jump to conclusions. I'd delete this answer, but as the original approach evidentially doesn't work for Noah for some reason, I'll leave this here just in case it still helps...
Typically you just want $HOME/.emacs.d/
See C-hig (emacs) Find Init RET for more info.

Related

emacs init file on MS Windows

There's a huge literature on the topic, but, nevertheless, I cannot get this done.
My ultimate goal is to work with fstar on Microsoft Windows.
C-x C-f resolves ~ as C:/Users/myname which is in line with my HOME environment variable in the Environmental variables Windows section.
(expand-file-name user-emacs-directory), as described here yields, C:/Users/myname/.emacs.d/
In C:/Users/myname/.emacs.d/ I have placed .emacs.el and init.el with the suggested script:
(require 'package) (add-to-list 'package-archives '("melpa" .
"http://melpa.org/packages/") t) (package-initialize)
However M-x returns undefined, no matter if I start Emacs with or without the -q flag (see here). My "Messages" buffer is empty".
A couple of things you could try:
Check the value of the variable user-init-file (use C-h v). That should tell you if Emacs loads the file you want it to load. If you started Emacs with the -q option, the value of this variable should be nil.
The error M-x is undefined can be caused by rebinding the Escape key. (That's because pressing a key while holding down the "Meta" key is equivalent to first pressing Escape and then the key in question.) Is there something in the init file that might cause this to happen?
Try starting Emacs with -Q instead of -q. This makes Emacs skip "site-wide" init files. I can't really think of a reason why your system would have any of those, but it might be worth ruling this out.
You could edit your question and include your entire init file (surround it with ``` on a line by itself), so we could have a look.

Emacs compilation mode won't see bash alias

Emacs M-x compile does not see any aliases set in .bashrc. If I use M-x shell then type the alias, it is fine. I tried sourcing .bashrc from /etc/profile, from ~/.profile, ~/bash_env, anything I can think of to no avail.
I am on Emacs 23 and Ubuntu 11. I start emacs using /usr/bin/emacs %F, from a desktop button.
Emacs inherits its environment from the parent process. How are you invoking Emacs - from the command line, or some other way?
What happens if you:
M-x compile RET C-a C-k bash -i -c your_alias RET
Invoking bash as an interactive shell (-i option) should read your .bashrc aliases.
Edit: I think both M-x shell-command and M-x compile execute commands in an inferior shell via call-process. Try the following in your .emacs (or just evaluate):
(setq shell-file-name "bash")
(setq shell-command-switch "-ic")
I notice that after evaluation of the above, .bashrc aliases are picked up for use by both M-x shell-command and M-x compile, i.e
M-x compile RET your_alias RET
should then work.
My environment: Emacs 24.1 (pretest rc1), OSX 10.7.3
Keith Flower's answer works but can result in some slowdowns due to .bashrc being unnecessarily loaded in other places (presumably many many times, my computer is not exactly under-powered but emacs was almost unusable when trying to use autocomplete.el).
An alternative way is to locally modify shell-command-switch only for the functions where it is needed. This can be done using emacs' "advice" feature to create a wrapper around those functions. Here's an example that modifies compile:
;; Define + active modification to compile that locally sets
;; shell-command-switch to "-ic".
(defadvice compile (around use-bashrc activate)
"Load .bashrc in any calls to bash (e.g. so we can use aliases)"
(let ((shell-command-switch "-ic"))
ad-do-it))
You need to write similar "advice" for each function that you want to use .bashrc (e.g. I also needed to define the same advice for recompile), just copy the above and replace compile in the above with another function name.
You may like emac's bash-completion :
https://github.com/szermatt/emacs-bash-completion
You'll be able to use tab completion of your aliases in the compilation minibuffer and in shell-mode.
Enjoy !
(they speak about it here Bash autocompletion in Emacs shell-mode )
I think compilation commands are not interpreted through a shell: they are juste exec'ed by emacs (which means aliases, shell functions and other shell-specific things are not taken into account).
Try to wrap you compilation command into a shell-script which would source the correct environment.
You can do this either with a full-fledged shell-script in the form
#!/bin/bash
source "~/.bashrc"
my_command
or directly in emacs with a compilation command of the form
bash -c "source ~/.bashrc; my_command"
See Is there a way to get my emacs to recognize my bash aliases and custom functions when I run a shell command? for a fix which doesn't run all your .bashrc and doesn't create these error messages:
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell

I want to run the cygwin bash shell from native windows emacs app

I have followed instructions from How can I run Cygwin Bash Shell from within Emacs? this question and I have gone further and added the (setq explicit-bash-args '("--login" "-i")) command, however emacs continues to only display the dos prompt when I type M-x shell. In summery my .emacs file looks like this:
(defun cygwin-shell ()
"Run cygwin bash in shell mode."
(interactive)
(let ((explicit-shell-file-name "C:/cygwin/bin/bash"))
(call-interactively 'shell)))
(setq explicit-bash-args '("--login" "-i"))`
Please be gentle with the answers as I am right at the bottom of the famous vertical emacs learning curve!
If you implemented the answer from that question, note that you have to do M-x cygwin-shell to start bash. If you want to use it for every M-x shell you need to call
(setq explicit-shell-file-name "C:/cygwin/bin/bash")
Since you stated that you are learning, here's a few tips when trying this out.
type C-x C-f ~/.emacs to open your .emacs file in your user path.
Enter your function above at the end
M-x load-file [RET] .emacs: loads the buffer (no need to restart emacs)
C-h a: If you are interested in some specific action, you can look it up
C-h v [RET] variable: can inspect the variable, check the value of explicit-bash-args for instance
And, btw, I'm not sure what the "--login -i" does, but someone stated in a comment that you should have that so "ls" would work. If you have your cygwin bin path in your PATH environment variable, bash will find ls anyway. No need to escape the path variable either, this is handled by bash (do an echo $PATH in bash when you get it working and you'll see).

cygwin shell in Emacs - output messed up?

I saw an answer here how to start the cygwin shell. However, the cygwin shell output is messed up.
(defun cygwin-shell ()
"Run cygwin bash in shell mode."
(interactive)
(let ((explicit-shell-file-name "C:/cygwin/bin/bash"))
(call-interactively 'shell)))
(setq explicit-bash-args '("--login" "-i"))
and here is a sample output of the shell
]0;~
seth#seth ~
$ cd ~
]0;~
seth#seth ~
$ dir
]0;~
seth#seth ~
as one can see, output is screwed up. How do i fix this?
edit: i just noticed that ^[]0 always appears at the end of each command \ otherwise output text works fine. Anyway to get rid of this ending?
alright, i figured this out. in ~/.bashrc, i added
export PS1="\e[0;31m[\u#\h \W]\$ \e[m "
this makes prompt red in single line (which is easy on eyes vs the yellow in original cygwin prompt!)
see
http://www.cyberciti.biz/faq/bash-shell-change-the-color-of-my-shell-prompt-under-linux-or-unix/
In addition, you have to make sure you do not use dos endings. To convert dos endings to unix, type C-x RET f unix and save or place in .emacs file
(set-buffer-file-coding-system 'unix)
Look into EmacsW32. With it, your .emacs configuration becomes
(setq w32shell-cygwin-bin "c:\\cygwin\\bin")
(require 'w32shell)
(w32shell-add-emacs)
(w32shell-set-shell "cygwin")
and everything works.
In Emacs 24.2, I had to put the export PS1=... line from Seth's answer (2) in ~/.emacs_bash instead of in ~/.bashrc.
Use
export PS1="\e[0;32m\u#\h\e[m \e[0;33m\w\e[m\n\$ "
If you like to keep the original color and format.

Something wrong with Emacs shell

I use Ubuntu8.10 and emacs-snapshot. Running shell-mode on emacs and input "ls" shows escape codes:
screenshot http://lh3.ggpht.com/_os_zrveP8Ns/SdMmohKNjmI/AAAAAAAADB4/VlKpr5H_7ZA/s512/screen.png
How can I get the output I expect?
You can use AnsiTerm which does support colors or you can enable AnsiColor for the normal shell:
(autoload 'ansi-color-for-comint-mode-on "ansi-color" nil t)
(add-hook 'shell-mode-hook 'ansi-color-for-comint-mode-on)
Furthermore, you may choose another shell: M-x term or M-x eshell. The former provides an interface that is much closer to a real terminal emulator than shell-mode (once you start it, you can get out of the mode with C-c C-j and get in again with C-c C-k). The latter is a shell implementation written in Elisp (you can use the common shell commands as well as evaluating Lisp code).
Expanding on vatine's answer, you can add that inside your .cshrc (.tcshrc/.bashrc) wrapped with a check for the environment variable INSIDE_EMACS.
For example (from my .tcshrc):
if ( $?INSIDE_EMACS ) then
alias l 'ls --color=never'
endif
M-x ansi-color-for-comint-mode-on
The problem is that "l" is trying to colorise the output and emacs isn't having any of it. Try the following:
$ unalias l
$ alias l ls --color=never
I wrapped my alias ls ='ls --color=auto' in ~/.bashrc:
case "$TERM" in
xterm*|rxvt*)
if [ -x /usr/bin/dircolors ]; then
alias ls='ls --color=auto'
...
fi
;;
*)
;;
esac
This disables using color=auto in emacs.