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
Related
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)))
I want to have a make-shells command in emacs that will open a number of emacs-shell buffers, each with its own working directory. The idea is that for each project I'm working on, I have a shell that starts out in that project's directory, so I can easily switch between them.
Currently I have this code:
(defun shell-dir (name dir)
(interactive "sShell name: \nDDirectory: ")
(shell name)
(switch-to-buffer name)
(comint-send-string (current-buffer) (concat "cd " dir "\r"))
(sleep-for 0 10)
(dirs))
(defun make-shells ()
(interactive)
(shell-dir "project1" "~/proj/project1")
(shell-dir "project2" "~/proj/project2")
(shell-dir "project3" "~/proj/project3")
(delete-window))
This is pretty ugly, though, and half the time (dirs) doesn't pick up the correct path, so tab completion breaks until I re-run it manually. Is there a built-in way to set the current working directory of the emacs shell? Or would something like CEDET (plus less reliance on the shell vs. emacs modes) be a much better solution to this?
I experienced similar problems with the current directory tracking provided by Emacs, so I wrote one which solves the problem once and forever.
Check it out here.
The short version of what it does is that you modify your shell prompt to include a full path to the current directory (only when running inside Emacs), and the Emacs shell buffer will use that.
This means you never have to do M-x dirs again.
There's also the package dirtrack (shipped with Emacs) which does the same thing.
I like my version better because it removes the path from the prompt. I don't want to see the entire path in my prompt as my current directory is often very long.
Once you use one of the above two solutions, you can simplify your shell-dir routine to be:
(defun shell-dir (name dir)
(interactive "sShell name: \nDDirectory: ")
(let ((default-directory dir))
(shell name)))
One more answer... I found there was a way (on Linux) to make Emacs figure out the current directory properly, by using the /proc filesystem.
http://www.emacswiki.org/emacs/ShellDirtrackByProcfs
That way, you just have to start up the shell in whatever directory and Emacs will automatically figure it out and get the tab-completion etc. right.
When I try to close emacs I get the message
"buffer ido.last modified; kill anyway?"
and whatever I answer, emacs stays open. But I can't open this buffer and the ido.last file doesn't exist. How can I get my emacs closed?
If you've used emacs as another user, as root for instance, it's possible that the .ido.last file is owned by root and hence you aren't allowed to remove or save it.
When you get the "buffer .ido.last modified"-question just remove the .ido.last file before you answer yes (if you're using emacs within a shell, just C-z, remove file and then resume with %). Then the .ido.last file will be written to disk with you as the owner.
IDO tries to save a file called ido.last into ~/.emacs.d directory. But, in your case IDO seems to be unable to do so. Maybe your ~/.emacs.d directory is read-only for a particular reason, or your disk is full, etc. So IDO raise an error that prevent your emacs to close.
If you don't use IDO, try to remove this kind of lines from your .emacs :
(require 'ido)
(ido-mode t)
Add this code (eg. to your .emacs.el, init.el, etc.) to
fix the issue with ido preventing emacs from exiting
when the ido.last file is not writable:
(defun ido-kill-emacs-hook ()
(ignore-errors (ido-save-history)))
Like Jérôme Radix said, IDO gives that error when unable to save the ido.last file, and the error prevents emacs from closing.
The default location for IDO to save the file is into ~/.emacs.d directory, but this can be overridden by setting the variable ido-save-directory-list-file. This means you should check the value of this variable (e.g., M-x describe-variable) to see where IDO is trying to save a file, and figure out why it can't save the file.
Possible reasons include:
The directory or file is read-only.
The directory does not exist. IDO will not create the directory for you.
The directory or file is owned by another user.
The disk is full.
For session management with emacs, I appended the following lines to my .emacs file
(load "desktop")
(desktop-load-default)
(desktop-read)
I was able to save the session using M-x desktop-save. But after exiting emacs, I am unable to recover the earlier saved session. I did start emacs in the same directory as that during "desktop-save"
Please let me know if I am missing anything or not correct in my efforts
Thanks
-- Harish
Chapter 51 'Saving Emacs Sessions' of my Emacs manual (using a Emacs 23 snapshot) has this
You can save the desktop manually
with the command M-x desktop-save'.
You can also enable automatic saving
of the desktop when you exit Emacs,
and automatic restoration of the last
saved desktop when Emacs starts: use
the Customization buffer (*note Easy
Customization::) to set
desktop-save-mode' to t' for future
sessions, or add this line in your
~/.emacs' file:
(desktop-save-mode 1)
which is different from what you tried.
The following worked for me (emacs 21.3.1):
(load "desktop")
(setq desktop-save-mode 1)
(desktop-load-default)
(desktop-read)
The desktop-save-mode line is only needed if you want to save desktop automatically on exit (i.e. without bothering to type M-x deskstop-save).
The only difference is that I put those lines at the beginning of the .emacs file but I doubt that's a problem.
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.