7

How do you configure emacs to open a file in the current window -- using the mouse -- when it is selected from a dired buffer?

That is, when I open a directory and then select a file with the mouse I want the file displayed in the same window which displayed the dired buffer.

lawlist
  • 18,826
  • 5
  • 37
  • 118
Jim
  • 73
  • 1
  • 3
  • Do you mean "How to open a file in the same window from where you clicked it in a `dired` buffer"? – Kaushal Modi Oct 21 '15 at 15:34
  • Yes, that is what I am asking. – Jim Oct 21 '15 at 16:00
  • If I use the cursor and enter key it does open in the same window. Using the mouse to select it opens it in another window (splits the window if only one window is open). – Jim Oct 21 '15 at 16:11
  • 1
    Check out this: http://emacswiki.org/emacs/DiredReuseDirectoryBuffer I didn't get a chance to try it though. – Kaushal Modi Oct 21 '15 at 16:52

4 Answers4

4

If you really want that, just define your own command to do it. You can copy the code for what mouse-2 is already bound to, dired-mouse-find-file-other-window, and just change the occurrence of find-file-other-window to find-file. Then bind your command to mouse-2, in place of dired-mouse-find-file-other-window.

The only changes I made here are (1) the name of the command and (2) find-file-other-window instead of find-file:

(defun dired-mouse-find-file (event)
  "In Dired, visit the file or directory name you click on."
  (interactive "e")
  (let (window pos file)
    (save-excursion
      (setq window (posn-window (event-end event))
            pos (posn-point (event-end event)))
      (if (not (windowp window))
          (error "No file chosen"))
      (set-buffer (window-buffer window))
      (goto-char pos)
      (setq file (dired-get-file-for-visit)))
    (if (file-directory-p file)
        (or (and (cdr dired-subdir-alist)
                 (dired-goto-subdir file))
            (progn
              (select-window window)
              (dired-other-window file)))
      (select-window window)
      (find-file (file-name-sans-versions file t)))))

(define-key dired-mode-map [mouse-2] 'dired-mouse-find-file)

And if you want to also replace (kill) the Dired buffer then use find-alternate-file instead of find-file.

Drew
  • 75,699
  • 9
  • 109
  • 225
  • Should I just be able to add this code to my .emacs file? That didn't work. – Jim Oct 21 '15 at 19:32
  • @Jim Just to cover all the grounds, did you restart emacs after that? If yes, and if it still does not work, what do you see when you do `C-h c` followed by middle mouse click in a \*Dired\* buffer? `mouse-2` is middle click. – Kaushal Modi Oct 21 '15 at 21:25
  • Dunno -- *dunno what's in your init file.* ;-) Try it from `emacs -Q` (no init file). If that works (it works for me) then recursively bisect your init file to find out what is interfering. If it doesn't work from `emacs -Q` either then it could be your Emacs version - what does `M-x emacs-version` tell you? – Drew Oct 21 '15 at 21:26
  • @kaushalmodi - I resarted emacs. C-h c = "... at that spot runs the command dired-mouse-find-file-ot.her-window." So I assume that means the key binding isn't being recognized. – Jim Oct 21 '15 at 22:34
  • @Jim I guessed so.. make sure that you put `(require 'dired)` at the very beginning of the code that Drew posted. Save the file and do `M-x eval-buffer` and then see what `C-h c ` does .. – Kaushal Modi Oct 21 '15 at 22:37
  • What @kaushalmodi said. Or just wrap the code in `(eval-after-load "dired" '(progn ...))` and put it in your init file. – Drew Oct 21 '15 at 23:26
2

Drew's answer is not right, it doesn't work for directories, only work for files. I think the other way is better (open dir in the same window and open files in the other window).

If you want to open files in the same window too, change find-file-other-window to find-file.

The working code:

(require 'dired)
(defun dired-mouse-find-file (event)
  "In Dired, visit the file or directory name you click on."
  (interactive "e")
  (let (window pos file)
    (save-excursion
      (setq window (posn-window (event-end event))
            pos (posn-point (event-end event)))
      (if (not (windowp window))
          (error "No file chosen"))
      (set-buffer (window-buffer window))
      (goto-char pos)
      (setq file (dired-get-file-for-visit)))
    (if (file-directory-p file)
        (or (and (cdr dired-subdir-alist)
                 (dired-goto-subdir file))
            (progn
              (select-window window)
              (dired file)))
      (select-window window)
      (find-file-other-window (file-name-sans-versions file t)))))

(define-key dired-mode-map [mouse-2] 'dired-mouse-find-file)
Liang Zou
  • 31
  • 1
  • 1. The question was about opening a *file* (not a directory). 2. I said that if you also want to open a *directory* in the same window then just use *`find-alternate-file`* instead of `find-file`. 3. The OP *does not want* to open the file (or dir) in a different window. You may prefer that behavior, but it does not respond to the OP. – Drew Mar 17 '17 at 14:12
0

This line of code should be enough. More details can be seen here.

(put 'dired-find-alternate-file 'disabled nil)
(add-hook 'dired-mode-hook (lambda () (local-set-key (kbd "<mouse-2>") #'dired-find-alternate-file)))
0

I found that this works (as of Emacs 26.3). It gives the exact behavior specified, opening a file in the same window (and opening a directory in another), affecting mouse actions only:

(define-key dired-mode-map [mouse-2] (lambda (event)
  (interactive "e")
  (dired-mouse-find-file event 'find-file 'dired-other-window)))

However, I find it much more useful to open both files and directories in the same window, which is neatly simpler:

(define-key dired-mode-map [mouse-2] 'dired-mouse-find-file)

Since mouse-2 is calling dired-mouse-find-file just with -other-window variants passed in, and since dired-mouse-find-file defaults to the non -other-window functions, it seems like a clean replacement.