Emacs Stack Exchange is a question and answer site for those using, extending or developing Emacs. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I want to know how do you use Emacs lisp to check the content of the current buffer, specially to know if certain string exists within the current buffer.

share|improve this question
up vote 6 down vote accepted

I think the easiest approach would be

    (goto-char (point-min))
    (search-forward string nil t))

This will return non-nil if the given string is in the current buffer.

share|improve this answer
And in most cases, you'll want to use re-search-forward, for the added flexibility of specifying a regexp rather than a mere substring. – Stefan Mar 19 at 22:52

You can also do this on a way which probably looks familiar from other languages using s.el:

(with-current-buffer "some_buffer"
      (s-contains? "some string"
            (buffer-substring-no-properties (point-min) (point-max))))
share|improve this answer
Emacs is very good at acting directly on buffer contents. Copying portions of the buffer (let alone the entire contents, as in this case) in order to perform string-based operations is generally not the best approach. – phils Mar 19 at 22:14
I second @phils's recommendation, pointing out that it's very inefficient, especially in this case, since it copies the whole buffer's content. – Stefan Mar 19 at 22:50
Yes, you're right. This is not the best approach, I recommend accepted answer over this one. My idea was to point to s.el. @phils – foki Mar 20 at 0:45

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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