6

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.

  • 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: emacswiki.org/emacs/DiredReuseDirectoryBuffer I didn't get a chance to try it though. – Kaushal Modi Oct 21 '15 at 16:52
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.

  • 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 <mouse-2> = "... 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 <mouse-2> does .. – Kaushal Modi Oct 21 '15 at 22:37
1

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.