Jarek
Jarek

Reputation: 7729

Emacs - switch to buffer by typing extension and then file name

is it possible in emacs to search filename in a way that i type in the extension first then space and after that file name itself? E.g. org notes org or not will switch to notes.org.

Or is there any way to write such a function to do exactly that?

Upvotes: 5

Views: 506

Answers (6)

Iswitchb, shipped with Emacs, doesn't do exactly this, but close. It shows a list of buffer names in the minibuffer. As you type, the list narrows down to the buffers whose name contain the text you've typed (anywhere, not just at the beginning). So if you type .org, then you'll be down to the buffers whose name contains .org. To open a specific file amongst these, you would then need to either move the cursor to the left and type the last letters of the file name, or press C-r or C-s to rotate amongst the matches.

The Emacs wiki has a page on Iswitchb and a comparison of Icicles, Ido and Iswitchb.

Upvotes: 1

phils
phils

Reputation: 73274

You can (approximately) do this by default by using name globbing.

C-xb *.org TAB

Emacs moves point automatically to sit after the asterisk, so you can immediately continue typing part of the buffer name.

Also *org is slightly easier to type than *.org and would probably get you sufficiently similar results for your purposes.

Upvotes: 1

Drew
Drew

Reputation: 30701

Icicles does this out of the box -- just use S-TAB instead of TAB for completion -- i.e., apropos completion rather than prefix completion.

To complete against multiple patterns (e.g. substrings) without regard to their order, use progressive completion. You use S-SPC to separate the patterns.

    C-x C-f notes S-SPC org

or

    C-x C-f org S-SPC notes

or, to be sure that org is matched as a file extension, use \.org. (Or use C-` to toggle substring/regexp matching, i.e., escaping of regexp special chars.)

Same thing if you use buffer-name completion (or any other kind of completion) rather than file-name completion.

As for Ido's "flex" matching, Icicles has the same thing (it's called "scatter" matching), as well as several other types of fuzzy matching. (But all forms of fuzzy matching are less useful than they sound, IMHO.)

Upvotes: 1

alex vasi
alex vasi

Reputation: 5344

I completely agree with Anders, IDO is quite useful. For example, if you have two buffers "some-video-page.js" and "video.py" you can enter "C-x b vidjs" or "C-x b vidpy" to switch to either of those.

(ido-mode)
(setq ido-enable-flex-matching t)

Also consider using ibuffer to organize you buffers. You can filter buffers by full file name and close multiple buffers based on your current filter.

(global-set-key (kbd "C-x C-b") 'ibuffer)

Returning to your question. There is uniquify library that is now shipped with Emacs and it will include name of the parent directory in buffer names by default. But this is emacs we are speaking about and you can customize it however you want. I have a custom file-hook that prefixes only python buffers with python module name. Will IDO be enough for you or do you still want your custom switch-buffers function?

Upvotes: 1

L42y
L42y

Reputation: 1021

Anything can do this easily, run anything, type any words in the buffer's name, results shows:

Emacs Anything

follow this to install it: http://emacs-fu.blogspot.com/2011/09/finding-just-about-anything.html

if you can want to filter buffer, here is a example config for you:

(require 'anything-config)
(global-set-key (kbd "C-x b")
            (lambda() (interactive)
              (anything
               :prompt "Switch to: "
               :sources
               '(anything-c-source-buffers))))

Upvotes: 3

Anders Waldenborg
Anders Waldenborg

Reputation: 3035

If you use ido you can type org c-space notes.

Ido takes a little while to get used to, but personally I cant live without it.

Upvotes: 12

Related Questions