30
votes
Accepted
Only show lines containing phrase/regex
...see all the lines from the current buffer...
With built-in commands and no external packages or dependencies in
a new buffer, use:
M-x occur
the same buffer, use:
M-x keep-lines
30
votes
Accepted
how do I quickly remove lines from emacs buffer
You can go to beginning of buffer with M-<, then M-x flush-lines, type your word and hit RET.
(flush-lines REGEXP &optional RSTART REND INTERACTIVE)
Delete lines containing matches for ...
22
votes
Accepted
Incrementally replace a given string
General technique
Your replacement string can contain arbitrary lisp code. From the documentation for replace-regexp:
In interactive calls, the replacement text may contain ‘\,’ followed by a Lisp ...
18
votes
support for regex look behind and ahead?
No, Emacs regular expressions do not support arbitrary zero-width look-ahead/behind assertions.
n.b. Evil and Spacemacs (like all elisp libraries) are irrelevant when it comes to questions about the ...
16
votes
Accepted
Search through the values of all variables in Emacs
Does apropos-value do what you're looking for?
(apropos-value PATTERN &optional DO-ALL)
Show all symbols whose value’s printed representation matches PATTERN.
PATTERN can be a word, a list of ...
16
votes
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
The built-in library subr-x.el has included the inline functions string-trim-left, string-trim-right, and string-trim ...
13
votes
Accepted
Is there any principal difference between "A-Z" and upper?
The macro rx returns regexp strings that can be passed to other Emacs functions.
ELISP> (rx (one-or-more (any upper lower)))
"[[:lower:][:upper:]]+"
ELISP> (rx (one-or-more (any "A-Z" "a-z")))
"...
11
votes
Is there an equivalent of string-match for regexp?
For the sake of an answer:
string-match is meant for to be used with regexps.
(string-match REGEXP STRING &optional START)
Return index of start of first match for REGEXP in STRING, or ...
11
votes
Accepted
What does a backslash followed by a single quote mean in a regular expression?
It's a special construct in emacs regexp that matches the end of a string (not just the end of a line). Quoting the the manual
\'
matches the empty string, but only at the end of the string or ...
11
votes
Accepted
How to apply arithmetic operators to query-replace-regex
\2 in your replacement is a string, and it needs to be a number in order to perform the division.
You could convert it to a number using string-to-number, but there's in-built shorthand for treating ...
9
votes
Accepted
Most performant matching of "any char"
In Emacs's regexps, . does not match all characters. It is a synonym of [^\n]. So the reason for using [\0-\377[:nonascii:]] is when you want to match "any char, even a newline".
W.r.t overflowing ...
9
votes
Accepted
Regexp replace to match a string, but not match a superstring
Try \_<Vector\_>. The \_< construct matches the empty string, but only at the beginning of a symbol. \_> is the same, but at the end of a symbol. What is a "symbol" depends on the buffer's ...
9
votes
What's the idiomatic (or best) way to trim surrounding whitespace from a string?
There is the string manipulation library s.el where trimming whitespace and newlines at the beginning and the end of a string is implemented as function s-trim. I cite that function here with its ...
8
votes
Accepted
Escaping brackets in regexp
Try this:
(if (looking-at "\\[") (insert "f"))
Usually you will need to escape your escaping backslash. This tutorial has a short explanation of the "double backslash".
When in doubt, I always use ...
8
votes
Accepted
How to use the regexp for digits with an interactive search?
As far as I can tell, there is no \d in Emacs regular
expressions.
So, @Tobias's comment is (mostly) correct, except that you need to
escape the curly brackets. Either of the following will work:
`C-...

Dan♦
- 32.7k
8
votes
Accepted
Evil mode and regular expressions
evil uses the Emacs regexp facilities under the hood. Unfortunately, Emacs does not appear to have a separate syntax
class for digits, and does not recognize the \d regexp class.
So, to match your ...

Dan♦
- 32.7k
8
votes
Accepted
Multiple URL formats for bug-reference-mode
Update
Starting with Emacs 28, bug-reference is able to automatically detect and configure itself for Git forges including GitHub and GitLab. Quoth (info "(emacs) Bug Reference"):
[...]
...
8
votes
Accepted
Enable mode if file content contains a matching string
I would like to enable [some mode] whenever a file I open contains a specific string, or a regex
This is exactly what magic-mode-alist is for (there is also magic-fallback-mode-alist if you want the ...
8
votes
Accepted
Find and remove consecutive duplicated words while ignoring case
You can query-replace-regexp as follows:
\(\b\w+\b\)\W+\1\b → \1
This means, match a whole word (\b\w+\b), followed by non word characters (\W+), followed by the first word (\1) and a word ending (\...
8
votes
Whitespace and newlines in regexps?
You could always use one of the character classes. I would say use [:blank:] when the usage context is textual, and use [:space:] when it is programmatic, i.e. plain text vs source code.
When ...
8
votes
Accepted
Regex for the last line of a string
$ matches at the end of a line, not the end of a string. If you want to match at the end of a string you need to use the \' operator:
(string-match "\n.*\\'" "\n \n \n ")
=> 4
...
7
votes
replace-regexp: invalid use of \ in replacement text
To make clear the accepted answer in the comments:
M-x replace-regexp ^([0-9]\{1,2\}) RET \exx\{ RET
^^
should be
M-x replace-regexp ^([0-9]\{1,2\}) RET ...
7
votes
Accepted
Regexp replace without interaction
Use replace-regexp (note the absence of the query- prefix).
But also, note the ! keybinding when running query-replace-regexp. It will replace all occurences beyond point without further questions.
7
votes
Accepted
Why does adding grouping parentheses cause a regexp to no longer match?
You need to escape the \ on your \(...\) to read \\(...\\):
(string-match "^\\([^XY]+X\\)" "fooXbarXbazXextraYstuff") ; => 0

Dan♦
- 32.7k
7
votes
Accepted
7
votes
Accepted
How do I match a closing bracket in Emacs lisp
Put the ] as the first character after the [ which starts the character class, e.g.
[])}]
This is the manual page
7
votes
Accepted
How can I use more than 9 regex capture groups in Emacs Lisp?
Going by the Emacs source code, it is absolutely possible to use more than 9 regex capture groups:
/* Since we have one byte reserved for the register number argument to
{start,stop}_memory, the ...
7
votes
emacs regex with multiple match for text, in multi line buffer
You can use the following regexp
This is test\(.*\n\)+?shoes>\nshoes/\n
.*\n matches a line
+? matches multiple lines in non-greedy way
Using the regexp in C-M-s (isearch-forward-regexp):
Note: ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
regular-expressions × 482replace × 62
search × 48
font-lock × 29
org-mode × 23
query-replace × 21
string × 20
query-replace-regexp × 20
rx × 14
isearch × 12
dired × 10
elisp × 9
syntax-highlighting × 9
align × 9
highlighting × 8
newlines × 8
auctex × 7
whitespace × 7
latex × 6
evil × 6
faces × 6
major-mode × 6
syntax-table × 6
buffers × 5
text-editing × 5