How do I create a directory from within Emacs? - 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.

Related

ansi-term won't find file under current directory? [duplicate]

This question already has answers here:
How can I have term.el (ansi-term) track directories if using anyhting other than bash
(2 answers)
Closed 8 years ago.
It's a very strange problem. I think it must caused my incorrect configuration of ansi-term, but i still can't find out where it is.
The issue is: when i in ansi-term and press M-x find-file, the prompt isn't current directory but the path i entered in my previous find file action. So when i change directory, it still display the same directory. So i have to enter the current directory every time. But it works very well in M-x shell and M-x eshell
Does the same thing happen when you start Emacs without your init file, i.e., emacs -Q? If so, that's the designed behavior or (especially if you use a development snapshot) perhaps an Emacs bug.
If not, then bisect your init file recursively to find out which part of it causes this behavior. To do that, use, e.g., command comment-region (see prefix arg in doc) to comment and uncomment a block of text. Comment out 1/2 of your init file, then 3/4, then 7/8,...,
each time testing whether the uncommented portion causes or removes the problematic behavior. You will very quickly identify what causes the behavior.
Because the path of emacs is different from that of term, it can only be changed by use the emacs command "cd".
So to solve this problem, I add the following code to my emacs configure file. The method is
find the pid of current term
find current working directory(cwd) of this pid.
I use multi-term, I think the method will be similar on ansi-term.
(defadvice term-send-input (after update-cwd)
(let* ((pid (process-id (get-buffer-process (current-buffer))))
(cwd (shell-command-to-string
(format "lsof -p %d -Fn | awk 'NR==2{print}' | sed \"s/n\\//\\//\" | tr -d '\n'" pid))))
(cd cwd)
(message (concat "change emacs path to: " cwd))))
(ad-activate 'term-send-input)
Then you can bound the key of term-send-input to <enter>. When you press <enter> in term, the emacs will change to the same path with the current path of term.
BTW, I use Mac Os. If you are on Linux, you can use the following code to find cwd.
(cwd (file-truename (format "/proc/%d/cwd" pid)))

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).

How to open file in Emacs via eshell?

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.

Find-file with a hint?

Is there a way to give the 'find-file' function a hint?
I'm working with files in the same directory on a remote server, and I'm getting tired of typing in the machine name, and directory structure all the time. It would sure be great if I could write a function that would bring up the find-file prompt with the machine name and directory already filled in.
(Note: I use Emacs 23.1)
Thanks for your help in advance.
If you're starting the 'find-file command from buffers associated with files on the remote server, the staring point should already include the directory/remote server filled in.
One way to skin this cat is to do
M-x cd /ssh:user#machine:/starting/path
note: I use tramp, and that's how find-file starts. I'd not noticed the /ssh: before today, but I don't use tramp very much any longer.
To answer your question directly, this command calls find-file with the "hint":
(defun my-remote-find-file ()
"call 'find-file with a starting directory"
(interactive)
(let ((default-directory "/ssh:user#machine:/starting/path/"))
(call-interactively 'find-file)))
Obviously customize the starting point.
One last way I can think of solving this is to do M-x dired on the remote server, and do your file finding from that point.
Set up recentf, and when you want to open a file on the server, go through the recentf menu; if the exact file you want is not there, just open something else in the same directory, then type C-x C-f and the directory should be filled in for you.
Use a bookmark to get to the remote directory. Then, as noted by Trey above, once you're in a file or directory that is remote, the default-directory will be set to what you want.
You can bookmark a remote file or directory. With Bookmark+, when you bookmark a Dired buffer you can save also all its file markings, omissions etc. -- IOW, the Dired state. When you later jump to the bookmark that saved state is restored. Use bookmarks to organize code projects etc.
bookmarksbookmark