Reputation: 28392
Switch-to-buffer is the function bound to C-x b. Occasionally I mistype the buffer I am intending to switch to and this causes me to open a fresh buffer with the incorrect name. The preferred behavior (in my case) is to fail to open the buffer... perhaps a failure to complete the buffer name. I recall encountering a few years back a technique that disallows switch-to-buffer to open new buffers. Perhaps someone on StackOverflow can identify that technique?
Thanks!
Setjmp
Upvotes: 1
Views: 250
Reputation: 28392
I was able to track down the solution in Writing GNU Emacs Extensions by Bob Glickstein. (In knew I had seen it somewhere, but it took some time to figure out where). I have the 1997 edition, and the answer is given in Chapter 2, under a section called "Advised Buffer Switching." Glickstein demonstrates the customization of switch-to-buffer as a method of instructing the reader on giving "advice" to functions.
(defadvice switch-to-buffer (before existing-buffer
activate compile)
"When interactive, switch to existing buffers only, unless given a prefix argument."
(interactive
(list (read-buffer "Switch to buffer: "
(other-buffer)
(null current-prefix-arg)))))
The function read-buffer reads the name of the buffer and returns a string. That string is passed to switch-to-buffer. The first argument is the default. The second argument is a boolean determining whether non-existing buffers are allowed.
Setjmp
Upvotes: 1
Reputation: 1537
I think an even better solution than preventing open a wrong buffer is switching to the buffer you meant to.
This can be done by ido.el, which is one of my favorite package. Install that package and configure as the following, then you can type much less (and ignore case) to switch to a buffer.
(ido-mode 'buffer)
(setq ido-enable-flex-matching t)
For instance, you have buffers "abcd.el", "hijk.el" "ABC.c", simply C-x b then type "a.c" and . Now, you are in "ABC.c" buffer. C-x b followed by a single character "h" will lead you to "hijk.el" buffer.
Upvotes: 0
Reputation: 28581
I think you want to customize confirm-nonexistent-file-or-buffer
. E.g. with something like:
(setq confirm-nonexistent-file-or-buffer t)
The default is to only ask for confirmation if you've just hit completion before RET.
Upvotes: 2