(This follows the answer to the question: Completion of sentences stored in a file.)

Suppose I have a file, the content of which being organized thanks to org-mode:

* Dreams ≠ reality 
Please, don't take your dreams for reality: (a + b)² ≠ a² + b².
Please, don't take your dreams for reality: cos(a + b) ≠ cos a + cos b.
No, √(a²) ≠ a.

* Derivatives
You should revise the derivatives.

* General remarks
This only works for a positive x.

Edit after @NickD comments:

AFAICS, the function used for reading this file, insert-file-contents, filters (I mean ignores) empty lines. Is it possible to ask it to filter lines starting with a * as well?

Is it possible to ask the function used for reading this file, insert-file-contents, or a consequent one, to filter some lines, e.g. those starting with a *

  • I don't know why you think insert-file-contents ignores empty lines, but I don't think that's true.
    – NickD
    Jun 8, 2020 at 19:17
  • @NickD Ah, OK, sorry: should be the way it is used in the linked answer which ignore them (AFAICS). Jun 8, 2020 at 19:21
  • 1
    That's done by split-string: the buffer contents are read as a string, then split-strig splits it on newlines and (because of the t third argument) it omits null string matches. It has nothing to do with insert-file-contents.
    – NickD
    Jun 8, 2020 at 19:51
  • @NickD OK, thanks. I rephrased my question. Jun 8, 2020 at 20:01
  • Ar you interested in this in the context of the linked answer? If so, split-string takes a TRIM argument that can be used to omit such lines from the result. But that's not going to work in a more general context.
    – NickD
    Jun 8, 2020 at 20:41

1 Answer 1


The answer to any "Is it possible" question is almost always yes (there is a theorem by Turing which tells us the limit of what is computable, but in practice people rarely come up against that limit). It is better to rephrase this type of question to ask how to do something, rather than merely if it is possible, since that's usually what most people mean by it. It's also a good idea to list what you've already tried, and possibly why that didn't work.

You want to use the seq-remove function. You can read the documentation for it by typing C-h f seq-remove <RET>. You'll see that it takes a predicate function and a sequence, and returns a new sequence comprised of those elements for which the predicate function returns nil.

For the predicate, I recommend writing a lambda that calls string-match-p. This returns true when a string matches a regular expression. Again, you can see the documentation for functions with C-h f.

Putting those together, something like this ought to work:

(seq-remove (lambda (line)
              (string-match-p "^\\*+ " line))

The elisp manual includes additional documentation about these topics that you might want to read, along with documentation about similar functions that you may soon need. You can open the info browser with C-h i. You may have many info manuals installed on your computer, but you should specifically take a look at the Emacs manual and the Elisp manual. Chapters 6.1 Sequences and 34.4 Regular Expression Searching would be good places to start.

  • 2
    You might want to make the regex just a bit more complicated to match Org mode's headlines: one or more asterisks followed by a space - something like "^\\*+ ".
    – NickD
    Jun 9, 2020 at 3:12
  • That's a good point.
    – db48x
    Jun 9, 2020 at 3:39
  • Eh, the comment was meant more for the OP, since he specifically asked for lines that start with an asterisk, even though he mentioned Org mode. So your answer answered exactly his question: I just thought that his question should have been slightly different :-)
    – NickD
    Jun 9, 2020 at 4:12
  • Yea, but the whole reason I decided to use a regexp here is that I knew the requirements would expand over time; otherwise I would have suggested (char-equal (aref line 0) ?*).
    – db48x
    Jun 9, 2020 at 8:05
  • 1
    ... or replace the filtering function with one that negates the condition: (lambda (line)(not (string-match-p "^\\*+ " line)))
    – NickD
    Jun 9, 2020 at 13: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.