How to open file in Emacs via eshell? - emacs

When in eshell is there a command for opening a file in another buffer?

You can call elisp functions directly. So to open a file, call find-file on the filename. Example:
~ $ ls
myfile
~ $ (find-file "myfile")
Parentheses and quotes are optional, so this works too:
~ $ find-file myfile

In eshell, you don't have to use the entire path when using the find file command. Hitting C-x C-f is the same as typing find-file, and eshell sets the directory to the one you are currently browsing. This is the advantage to me over using ansi-term. Try it out.

find-file basically does it, as ataylor and ryan kung indicated earlier.
$ find-file myfile
but, using eshell itself, you can also can set an even shorter alias:
$ alias ff 'for i in ${eshell-flatten-list $*} {find-file $i}'
(and eshell remembers it permanently). so from now you can just type:
$ ff myfile
thanks to this tutorial

The elisp funct can be directly used in eshell, so you can try:
find-file <filename>

Why use eshell? 'C-x f' and type the location of your file.

Related

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

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.

How to start in the middle of command input

How can I have emacs start and be in the middle of a command input? Particularly, I want emacs to start in the middle of a command input find-file with a message in the small buffer saying:
Find file: ~/
and the cursor at the last character of it so that I can continue typing the remaining path to open the file I want.
You can execute one of the following commands on the command prompt or make a shell script containing it appropriately:
$ emacs -f find-file # if you want to start Emacs in the current direcoty
$ (cd ~; emacs -f find-file) # if you want to start Emacs in your home diretory
From the emacs(1) man page:
-f function, --funcall function
Excute the lisp function function
I have to admit that my lisp is a bit rusty, but this works for me. Drop it in your ~/.emacs file (or whatever init file you are using):
(add-hook 'emacs-startup-hook
(lambda ()
(if (= (length command-line-args) 1)
(call-interactively 'find-file))))
If you call emacs with no arguments, like this:
sawa#localhost:~$ emacs
It will invoke find-file for you. If, on the other hand, you invoke emacs with an argument, such as a filename, like this:
sawa#localhost:~$ emacs somefile.txt
It will default to just visiting somefile.txt

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.

How do I create a directory from within Emacs?

How exactly can I create a new directory using Emacs? What commands do I use? (If possible, please provide an example)
to create the directory dir/to/create, type:
M-x make-directory RET dir/to/create RET
to create directories dir/parent1/node and dir/parent2/node, type:
M-! mkdir -p dir/parent{1,2}/node RET
It assumes that Emacs's inferior shell is bash/zsh or other compatible shell.
or in a Dired mode
+
It doesn't create nonexistent parent directories.
Example:
C-x d *.py RET ; shows python source files in the CWD in `Dired` mode
+ test RET ; create `test` directory in the CWD
CWD stands for Current Working Directory.
or just create a new file with non-existing parent directories using C-x C-f and type:
M-x make-directory RET RET
Emacs asks to create the parent directories automatically while saving a new file in recent Emacs versions. For older version, see How to make Emacs create intermediate dirs - when saving a file?
Ctrl+X D (C-x d) to open a directory in "dired" mode, then + to create a directory.
You can also run single shell commands using M-!
You're basically sending a string to the command line so you don't get any nice auto-completion but it's useful if you know how to perform an action through the command line but don't know an Emacs equivalent way.
M-! mkdir /path/to/new_dir
I guess I did it the hard way earlier today. I did:
M-x shell-command
then
mkdir -p topdir/subdir
You can use M-x make-directory inside of any buffer, not necessarily a dired buffer. It is a lisp function you can use as well.
I came across this question while searching for how to automatically create directories in Emacs. The best answer I found was in another thread from a few years later. The answer from Victor Deryagin was exactly what I was looking for. Adding that code to your .emacs will make Emacs prompt you to create the directory when you go to save the file.