I'm getting better and better at using emacs but one thing is annoying when using dired. When I run dired, it opens a new one, which is obviously the intended behaviour. Would it be possible to write a function so when I run M-x dired, it starts dired, however, if there's already a dired buffer, it'll take me there without starting a new instance.

I don't really know anything about elisp.

Edit: I'm adding some pseudo code to clarify it:

function dired2() {
   if (dired.buffer.exists) {
     existing_dired = get.dired.buffer
   else {

Thank you.

  • What if there are more than one dired-mode buffers open, which one do you want to be displayed? What if you have several windows open, then which window do you want to display which buffer? If the buffer is already displayed in another window, what then? What if you have several frames open, then which frame to switch to? You see, it gets very customized. I use a dedicated frame for dired-mode buffers (with the frame-bufs library and tabbar), and then I just switch to that frame -- which has the last viewed window-layout and buffer just as I left it before I went to another frame.
    – lawlist
    Jan 9 '16 at 16:21
  • Well, if what I have in mind was implemented, there would never be more than one instance of dired (if I understand how buffers/frames work). I open Emacs and the first time I start dired, it creates dired which sits then in one of the buffers. Then every time I invoke dired, it takes me to that instance of dired that has already been opened at the beginning.
    – Wasteland
    Jan 9 '16 at 16:29
  • It's not really one instance per se. The default behavior is to open a new buffer for each different directory, and you'll generally have several dired-mode buffers open -- some may be visible in an open window, and/or some may be hidden (i.e., buried). For example, you may have an open buffer ~/, and another buffer ~/Desktop, and another buffer /, and another buffer /usr/bin -- then which of those buffers do you want to switch to? And what if there are four (4) windows visible -- like a classic window pane that looks like a plus sign -- which window do you want to have focus, etc.
    – lawlist
    Jan 9 '16 at 16:46
  • 1
    Maybe you want to reuse existing buffers? -- How do I stop emacs dired mode from opening so many buffers? -- stackoverflow.com/questions/1839313/…
    – lawlist
    Jan 9 '16 at 16:50

Your Answer

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

Browse other questions tagged or ask your own question.