When I C-s I can search. How should I do a function which I could bind for example to C-r which would let me search in the results of some predefined search query?

Like contents of window would be:

function xyz(a, b)
  return a + b

I can C-M-s with function.* as a query and the result will be function xyz(a, b).

I want to have a function where I could enter a query and search in those results(which would be generated behind my eyes automatically).


Thank you all for the solutions. occur, helm-occur also works very well, but I have accepted Jordon Biondo answer because he provided the code I needed to answer my question fully.

  • I'm a bit confused about your goal: it sounds like you want to implement some Lua-specific feature for helm-swoop, but your question (and especially the title) is not very clear on this. Maybe I just didn't get your question because I'm not familiar with helm.
    – paprika
    Oct 24, 2014 at 13:08
  • @paprika see updated question :) Oct 24, 2014 at 13:10
  • isn't this exactly what helm-swoop already does? Oct 24, 2014 at 13:17
  • @JordonBiondo I can see the function in helm-swoop list. But imenu worked great for me! Except I search for a generic solution to my question Oct 24, 2014 at 13:20
  • Just wondering if you've considered "C-x c i" runs the command "helm-semantic-or-imenu", as you're already using helm.
    – rimero
    Oct 24, 2014 at 17:11

5 Answers 5


The simple approach would be to just use rgrep.

use M-xrgrep and specify searching for "function" in the current file in the current directory. You can wrap this up in a command so you don't have to specify the options each time like this:

(defun rgrep-lua-functions ()
  "run `rgrep` searching for 'function' in the current file."
  (rgrep "function" (file-name-nondirectory (buffer-file-name)) "."))

This will yield a searchable buffer with the function signatures and you can jump to the definitions if you want.

  • grep, rgrep, occur, and the like search only line by line. That is, the search contexts are single lines. I thought the OP was asking to search function definitions. But since this answer was accepted, I guess the question as posed is not really what was wanted.
    – Drew
    Oct 24, 2014 at 16:24
  • in the question he specifies that he wants lines like function xyz(a, b) which, if your lua is formatted correctly is exactly what you'll get. Oct 24, 2014 at 16:56
  • It's not clear to me. Yes, he does say he would be satisfied if such lines were found, to be searched. But in his example he shows the whole function definition as the "contents of the window" that he would want to be able to search. Single-line contexts do not present you with such content to be searched (whether in a "window" or otherwise). They present you with only the one line: function xyz(a, b).
    – Drew
    Oct 24, 2014 at 17:08

Would occur work for you?

M-x occur or M-s o then entering your query will provide you with a list of lines matching that regexp. You can then use C-s to search within the Occur buffer to refine your search within the matching lines.

  • Oops. I spent so long writing up my answer that I didn't notice that someone beat me to the punch.
    – nispio
    Oct 24, 2014 at 15:20

Emacs has a command called occur that is very well suited to this type of task. If you have Helm installed, then you can use helm-occur instead.


The occur command is part of vanilla emacs, and is bound to M-s o by default. Occur will prompt you for a regexp to search for, and then it will open a new window showing all of the lines in the current buffer that contain a match. The resulting *Occur* buffer is searchable using isearch (C-s).

  • M-s o function RET to show all lines matching "function"
  • C-x o to move focus to the *Occur* buffer
  • C-s to start a search within the search results

Helm Occur

If you have helm installed you can use helm-occur to do something similar. In this case, the *Helm Occur* buffer will not be directly searchable with C-s. In Helm you can search for multiple search terms just by separating them with a space.

  • M-x helm-occur RET to open a Helm Occur window
  • function to show all lines containing "function"
  • SPC foo to narrow the results to lines also containing foo

In either of these modes, selecting the match in the occur buffer and pressing enter will jump you to that line in the code.


If you use library Icicles then you can easily do this kind of thing. What you are asking for (if I understand correctly), is to search only within certain search contexts.

For example, as in this case, you might want to search only within function definitions - the search contexts are function definitions. In Lisp, this would be things like defuns.

Icicles has several predefined Icicles search commands for searching definitions like this. These are collectively called Icicles Imenu commands.

To search only command definitions, you can use command icicle-imenu-command-full. To search only non-interactive function definitions, use command icicle-imenu-non-interactive-function-full.

Beyond searching definitions, you can easily define any kind of contexts to be searched. The simplest way is by providing a regexp. Command icicle-search prompts you for the search context-defining regexp. You can alternatively use a function to define the search contexts.

Other possibilities include:

  • Searching the text of different kinds of THINGs (e.g., sexps, sentences, lists, strings, comments, XML elements,...), i.e., ignoring other text outside the THINGs.

  • Searching zones of text that have given text or overlay properties, i.e., ignoring other text.


Other answers here that mention occur and similar (helm-occur) provide a limited kind of context searching: the search contexts are just the lines of a buffer.

That is much more limited that, say, searching within whole function definitions, which is what I think you are asking for. With Icicles, command icicle-occur (bound to C-c ') lets you search within lines as search contexts.


With Isearch+:

  1. C-M-s .+function.+, to search for whole lines that contain function.

  2. C-z . followed by a regexp for something you want to look for in the lines that contain function.

(Optional) Repeat #2 with other patterns (regexps) to match in the same line. The matches can be in any order within the line.

Instead of step #1, you can start with C-M-s .+ C-z . function. The point is to search for whole lines that contain function along with any other patterns you like.

How C-z . works: It prompts you for an inline pattern to match (e.g. function). It wraps that with .* on each side, to get a full-line pattern (e.g. .*function.*). It adds a filter predicate that requires that your current search string (.+) also match that pattern (e.g. .*function.*).

See Dynamic Isearch Filtering for more information.

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.