1

I have a buffer with the name very-long-name-xyz-etc. This buffer is the only buffer whose name contains xyz. From terminal I can switch to that buffer using the following

emacsclientw -cna runemacs --eval "(switch-to-buffer \"very-long-name-xyz-etc\")"

Is there a way to do better by just using the partial name xyz?

1
  • 1
    Write a function switch-to-buffer-matching that takes the substring and searches the buffer list for a matching name. Your function can then call switch-to-buffer with the full name.
    – NickD
    Commented May 27 at 16:39

1 Answer 1

2

Here's one implementation of the function @NickD specified:

(require 'dash) ; for `-find', `-compose' and `-partial'

(defun switch-to-buffer-matching (regular-expression)
  "Switch to the first buffer that matches REGULAR-EXPRESSION."
  (switch-to-buffer
    (-find
     (-compose
      (-partial #'string-match-p regular-expression)
      #'buffer-name)
     (buffer-list))))
2
  • I confirm it works. There is however a missing ) at the end of code.
    – Name
    Commented May 29 at 18:30
  • Thanks for pointing that out. For future reference, the norm for such low-level corrections here is to just go in and make the change yourself -- the author will be notified automatically. Commented May 30 at 8:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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