Eshell - where is the default directory and the path defined? - bash

Two little questions with regards to the configuration of Eshell. I looked at the customization options and even the source code, but to no avail. Maybe I should have taken a better look.
When I start Emacs and open Eshell, the directory is '~/.emacs.d/elpa' instead of my home directory '~/’, what I would expect. I have no idea, where this configuration comes from, and did not find a way to change it.
I changed $PATH in my .bashrc and I customized the Emacs variable 'exec-path' to my needs. However, the first change is not reflected by Eshell, it does not know about it, while bash does. How does Eshell get this information, and how comes it differs from bash in its knowledge about changes in this variable?
Thanks for any hints
Thorsten
PS
I found a similar question [here] (http://stackoverflow.com/questions/3621755/what-setup-file-does-eshell-elisp-shell-read-when-it-starts-how-eshell-sets-i). The answer to point 2. is that eshell does not read .bashrc, therefore $PATH must be set in a file that is read earlier in the start-up process. On my Ubuntu system
/etc/environment
seems to be the file that is read by Emacs on start-up to set $PATH.

eshell seems to start in the current working directory, which for you (right after Emacs started) is "~/.emacs.d/elpa". You can change it manually with "M-x cd", but that shouldn't be necessary. If you edit some file and start eshell from its buffer, eshell will start in the same directory as the file.
But if you really want to start every eshell session in your home directory, you can make a hook call "cd" before eshell starts.

As already mentioned for Q1: eshell starts up in the directory of the current buffer when eshell is started. If I recall correctly (which is NOT a given), on macOS for a short period of time, when emacs was started by the Finder (i.e. double clicking the icon), the current buffer for *scratch* was root /. Now, when it is started by the finder, the current buffer for *scratch* is my home directory.
For Q2: the variable that you want to set is eshell-path-env. This is a colon separated list of paths.
I'm already using .dir-locals.el to set directory specific variables so I plan to do it that way but I'm sure there are start up hooks that you can use.
Remember that eshell is not bash, zsh, csh, etc. It is not ever going to read their rc files nor does it read /etc/environment, etc. Unlike comint-mode derivatives, it is not a subprocess of emacs but is wholly contained within emacs.
I found Mastering Eshell to be extremely helpful.

Related

Vim Vundle broken, can't open file

My vim setup apparently has broken out of nothing. It now spills errors for every single plugin I have configured. This started happening after I have changed some appearance settings, some syntastic features (both of which I don't think are the cause) and changed my shell to fish (this MAYBE is the cause).
Using the directive set shell=/usr/bin/fish or set shell=fish does not change anything, it still fails, for which I tried after reading this question.
The errors happen for both :BundleInstall and :BundleUpdate. My full vimrc file is available here if there is need to read it, it's not long.
Yes, setting your shell to fish is likely to be the root of your problem. The fish shell doesn't support the standard UNIX syntax for file redirections, which breaks Vim's system(). Just set Vim's shell to sh:
set shell=/bin/sh
You can't use the interactive features of fish from Vim anyway.
A while back I wrote up some docs for this on the Vundle wiki that you may find useful. In short, you can either:
Run: env SHELL=(which sh) vim +BundleInstall! +BundleClean +qall
Add set shell=sh to your .vimrc
As an aside, my Tackle project has an Up plugin that includes a handy way to update your vim plugins via Vundle.

Running Windows batch files from Emacs

I'm somewhat new to gnu emacs, and so perhaps this is a noob question, but I have a few batch files I use a lot when coding in emacs to compile/build/execute/debug/etc. I am wondering how I could A) run these batch files from emacs without having to keep opening a cmd prompt window or going to windows explorer and B) bind this to a key shortcut (perhaps I could specify the file?) I have seen several things online about running emacs in batch-mode, but I don't believe this is what I'm looking for. And I know it is possible because I have seen others run batch from emacs (output and everything would appear in a new buffer adjacent to the current as if you did C-x 3)
Thanks in advance!
To run an arbitrary shell command in Emacs, you call shell-command which is bound to M-!
See C-hf shell-command (or C-hkM-!) for details.
I believe in the Windows-native Emacs, the default shell is cmd (or some alias thereof), so I'm reasonably confident that this is what you're thinking of.
I'm not sure whether all of the following work in Windows, but related commands are:
M-& - async-shell-command
M-| - shell-command-on-region
And with a prefix argument (e.g. C-uM-!) any of those commands will insert the shell command's output into the current buffer. (In the case of shell-command-on-region, that replaces the region.)

Emacs adding (prepending) ".:" to PATH in shell

I have noticed that my shell buffers in Emacs (24.3.1) have (an extra) ".:" at the beginning of the $PATH variable on my Mac OS X (but not on my linux laptop that has the same . files), compared to my regular shell, which has it in the middle somewhere. Is this just me, or does Emacs on mac do this for some purpose? And can I prevent it? I don't think it has caused any actual problems, but it does cause warnings from RVM, and theoretically it has risks.
Thanks!
According to the emacs help entry for the shell command, the started shell gets the file ~/.emacs_SHELLNAME or ~/.emacs.d/init_SHELLNAME.sh as first command list (if one of those files exist). So you can modify the value of the PATH environment variable in one of those two files, but you should then check that your shell (configurable with the explicit-shell-file-name variable, the ESHELL environment variable or the shell-file-name variable, in that order) does not ignore commands that are issued during start-up.
Another reason may be that your (system-wide) configuration includes the current directory into the PATH value (you can check that with echo $PATH in a terminal), thus you would have to reconfigure your shell instead of emacs.
Considering security, having the current directory in the PATH does have the risk of unintentionally executing the wrong program (eg.: you have a self-written program named test and want to execute /usr/bin/test). That may cause some serious trouble, if that happens while working with super-user privileges.

Emacs ido-style shell

Is there a command line shell or shell customization that supports emacs-style ido find file? In emacs, I can navigate to a directory extremely quickly using C-x C-f and (ido-mode t).
Ideally, I'm looking for a solution that can be used outside of emacs. Though I'd be open for a way to quickly change directories within an eshell buffer.
Since I also wanted something like this, I tried to implement it as a bash completion
function. Obviously it means. you have to use bash.
It is only lightly tested, so please feel free to try and report bugs /comments.
http://pgas.freeshell.org/shell/bash-ido
Try the Z-shell. It has much better completion than bash. I must admit I haven't used it for a while though and stuck with bash because it's always available.
Bash has an environment variable called CDPATH which can contain a list of directories to search when using the cd command. Also, check out the "Programmable Completion" and "READLINE" sections of the Bash manual. You should be able to cobble together something that works for you.
The best I've been able to come up with so far is autojump. Still looking for a solution closer to ido, but autojump is a great little app.
I know that some terminal emulator support extension, for instance rxvt-unicode can be extended with Perl scripts. I'm not sure since i never wrote an extension myself, but maybe what you want is doable this way.
If you want to have a look at some Perl scripts for urxvt there are some examples in /usr/lib/urxvt/perl with the default urxvt install on Debian.
If you want ido completion in eshell or similar, it might be best to write a function that uses ido to read a directory, then inserts the command to cd to that directory into the shell buffer. I don't use eshell myself, so I couldn't comment on how to actually write this function, but it's an idea.
fzf, the command-line fuzzy finder, adds fuzzy completion for bash and zsh.
According to the developer:
It's an interactive Unix filter for command-line that can be used with
any list; files, command history, processes, hostnames, bookmarks, git
commits, etc.
This is a portable solution (works on Linux, Mac, Windows), which has no dependencies.

Bash completion for executable files in Emacs shell buffer

Bash completion seems to work correctly for the PATH that is set when I start emacs, but if I change the PATH in an emacs shell buffer, I cannot seem to get Emacs to rethink its list of possible completions.
Is there some way to reinitialize Emacs view of the completions?
In a non-emacs shell, everything works as expected with the occasional hash -r thrown in for good measure.
Assuming you are using completion-at-point, there is a variable exec-path that maintains a list of the directories that are searched for executables. shell-command-completion, which is by default part of shell-dynamic-complete-functions, searches those locations.
You can update that variable with new locations to search for executables, eg. (push "<new-directory>" exec-path).

Resources