I want files navigation via dired to be made in a separate frame but all files to be open in another frame. So how can I stick dired-mode buffers to a standalone-frame? Thanks.
I am interested in solution w/o using extra-packages.
Put something like this in your init file (.emacs):
(defadvice dired (after dedicate activate)
"Make this Dired window dedicated."
(set-window-dedicated-p (selected-window) t))
That makes Dired's window be dedicated, meaning that it cannot be reused for another buffer. You can do the same thing for other Dired commands -- e.g. dired-other-window.
Related
I managed to make dired work in a single window, when I am navigating through the file system.
Improving ergonomics, I decided to create bookmarks for my most frequent dirs with short names like: 'lwt', 'eve', etc. But every time I open the bookmark, the new dired buffer is created, even if the old one exists.
How to make it open the bookmark in the existing dired buffer?
Edit:
The original answer was actually a non-answer. I apologise for not having tested it properly. I will leave it here so other potential answerers aren't misled like I was.
I have meanwhile taken a good look at the source code of bookmark.el and the dired+ modifications don't have any effect on it. By default the bookmark-jump function uses the switch-to-buffer function as its display function. bookmark-jump however has an optional display-func argument, so a possible solution (involving a bit of elisp hacking) would be to create a function that reuses the current dired buffer (based on the dired+ source code) and invoking bookmark-jump with it, and if the concept works, then bind that to a keyboard short-cut.
Original answer:
The behaviour you are observing is just a side effect of the general "create a new buffer when navigating" behaviour of dired. This fact makes this question an almost duplicate of How do I stop emacs dired mode from opening so many buffers?.
Of the solutions proposed there and at the Dired Reuse Directory Buffer Emacs Wiki page, probably the simplest one is installing the Dired+ package and toggling directory buffer reuse with:
(toggle-diredp-find-file-reuse-dir 1)
in your .emacs file.
If this can help - that's what I use to open my bookmarks in the same buffer.
(defun my-bookmarks-list-same-buffer ()
"Open *Bookmarks* in current buffer."
(interactive)
(bookmark-bmenu-list)
(switch-to-buffer "*Bookmark List*"))
(global-set-key (kbd "s-b") 'my-bookmarks-list-same-buffer)
With Bookmark+, at least, bookmark-jump (C-x j j) to a Dired bookmark does reuse the Dired buffer if it already exists.
Working with >1 Emacs (on >1 machine), and want to check all open buffers for changes (they are open remotely via tramp/ssh) when I resume working on a particular Emacs.
Different Emacs might not have the exact same files open, but there is probably crossover.
Not using Desktop mode or anything flash like that (yet).
Thanks!
If you are wanting buffers to revert in Emacs when the associated files are changed by another program, then you should look at
C-hf global-auto-revert-mode RET
If I understand correctly, you want to revert any buffers to their file's contents if the file has been modified outside emacs.
Here's a little snippet of lisp that will loop through the unmodified buffers and reloads the contents from disk:
(require 'cl)
(loop for buffer being the buffers
do (when
(and (not (buffer-modified-p buffer)) (buffer-file-name buffer))
(switch-to-buffer buffer)
(revert-buffer nil t)))
I have emacs behaving more or less how I want it to by using this common bit of elisp:
(defun toggle-current-window-dedication ()
(interactive)
(let* ((window (selected-window))
(dedicated (window-dedicated-p window)))
(set-window-dedicated-p window (not dedicated))
(message "Window %sdedicated to %s"
(if dedicated "no longer " "")
(buffer-name))))
(global-set-key [pause] 'toggle-current-window-dedication)
Unfortunately, dired uses the directory for the buffer name, so dedicating a dired window only dedicates it to that directory. Once you navigate up or down, it opens a new buffer in a separate window. What I would like to do is dedicate a window to a major mode (dired in this case), and have all new buffers that default to that mode prefer that window. Is this possible?
Try using your code in combination with dired-single, which will cause all dired navigation to happen within a single buffer named *dired*. In the interests of full disclosure, I wrote dired-single.
set-window-dedicated-p forces Emacs to only show that window for that buffer, the other dired buffers cannot use the same window. See the *info* page for set-window-dedicated-p:
`display-buffer' (*note Choosing
Window::) never uses a dedicated
window for displaying another buffer
in it.
Perhaps one of the packages on the wiki page for DiredReuseDirectoryBuffer provides the functionality you're looking for...
When I start working on a project in emacs, I use M-x cd to get into the project root directory. But every time I use C-x C-f to open a file in one of the subdirectories (like app/model/Store.rb) emacs changes current directory to that of the file. Is there a way to make emacs stay at the root?
How about this? It replaces the regular find-file command with your own which always starts in some "root" directory (customize the find-file-root-dir variable):
(defvar find-file-root-dir "~/"
"Directory from which to start all find-file's")
(defun find-file-in-root ()
"Make find-file always start at some root directory."
(interactive)
(let ((default-directory find-file-root-dir))
(call-interactively 'find-file)))
(global-set-key (kbd "C-x C-f") 'find-file-in-root)
Assuming that you want the working directory of a file to be set to whatever the working directory was before you executed find-file, you could try the following:
(defmacro disallow-cd-in-function (fun)
"Prevent FUN (or any function that FUN calls) from changing directory."
`(defadvice ,fun (around dissallow-cd activate)
(let ((old-dir default-directory) ; Save old directory
(new-buf ad-do-it)) ; Capture new buffer
;; If FUN returns a buffer, operate in that buffer in addition
;; to current one.
(when (bufferp new-buf)
(set-buffer new-buf)
(setq default-directory old-dir))
;; Set default-directory in the current buffer
(setq default-directory old-dir))))
Armed with this macro, go search for operations that set the variable default-directory: M-x find-library files; M-x occur (setq default-directory. After some investigation, you discover that the desired function is called find-file-noselect-1. Also, it looks like set-visited-file-name is also a candidate. So:
(disallow-cd-in-function find-file-noselect-1)
(disallow-cd-in-function set-visited-file-name)
Note
Note that (disallow-cd-in-function find-file) would work just fine, but then if you switched to ido-mode, you'd be opening files with ido-find-file instead of find-file. Both of these functions ultimately use find-file-noselect-1, so hitting that with the macro is a more univeral solution.
Is there a way to make emacs stay at the root?
No, there isn't. C-x C-f always visits starting from the default directory of the buffer you are already vising. The default directory, by default, is the same directory as the file. You can change these (separately for every buffer) using M-x cd.
But that is not what you want. What you should do is C-x b to *scratch* (whose default directory is the same as where you launched Emacs from -- in your words "root"), and then visit a new file. And if you need to do this frequently, just open up a dired in there and work your way thru.
I appreciate I'm not answering your question directly, but I noticed you were more specific in your requirements in one of your comments: "I don't use compile or recompile, I just tend to close files I am not working on, since it takes fewer keystrokes to open a file again".
Have you got ido turned on for buffer switching? If you exclude the directory thing for a moment, switching files or buffers with ido is an identical number of keystrokes (C-x C-f vs C-x b, followed by a few characters in the file name). If you include the directory thing, switching files is more tricky for the precisely the reasons you mention. Sticking with buffers is much easier.
Going a step further, with the help of 'anything.el' it's quite easy to abstract away whether a given file is in a buffer or in a file using the file cache. For example, if you do the following:
(file-cache-add-directory-recursively "/my/ruby/project") ".*\\.rb$")
and run 'anything-for-files' (I have it bound to C-x f) all your open buffers are listed, along with all of the files you've just added to the file cache; isolating a given file usually only takes one or two more characters.
Any file in your project is thus 4 or 5 key presses away, and the directory they are in or whether or not they are in a buffer becomes irrelevant.
Hope that's helpful...
Sorry I haven't worked out the details, but you might be able to add a function to find-file-hook that resets the default directory to whatever you want.
For example, when you open a file via C-x-C-f, you can TAB complete file names, and if there are more than one possible completions, it will pop open a completion buffer with a list of possible completions. The problem is, after you've opened the file, the window the buffer was in switches back to normal, but it doesn't close. Is there any way I can make those buffers close automatically after the file has been opened?
Sorry to enter really late on this but this is how I do:
;; Remove completion buffer when done
(add-hook 'minibuffer-exit-hook
'(lambda ()
(let ((buffer "*Completions*"))
(and (get-buffer buffer)
(kill-buffer buffer)))))
Tested on GNU Emacs 22.x and 23.x
Although it does not directly solve your problem have you considered ido-mode as a mechanism for opening files?
ido-mode will bind C-x C-f to ido-find-file this allows you to interactively opening files (selecting between name collisions from within the minibuffer C-s and various other nifty features) I find it a much easier method of finding files and it will get rid of the *Completions* buffer altogether.