34
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 ...
20
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 ...
17
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 ...
17
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")))
"...
12
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 ...
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 does Emacs compile the regular expressions?
Emacs' regexp implementation caches previously used patterns in
compiled form (20 of them, last time I looked). This is not a detail
you should rely on too much.
(while (re-search-forward rx nil t) ......
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
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"):
[...]
...
9
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 ...
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
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♦
- 33.3k
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
Accepted
Emacs replace-regexp reference the match in the replace argument
replace-regexp is perfect for the job. You could also use query-replace-regexp, which is more visual.
You want to replace XYZ\(...\) with 'XYZ\1'.
Explanation:
. stands for a single character, you ...
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
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: ...
7
votes
Regexp replace to match a string, but not match a superstring
Another simple trick you can use is to match both Vector and VectorBase, and replace them both with VectorBase.
Vector\(Base\)? → VectorBase
More complicated cases can be handled by using elisp in ...
7
votes
Accepted
Why is this regex [[:upper:]] matching with lowercase string?
C-hf string-match-p:
string-match-p is a byte-compiled Lisp function in ‘subr.el’.
(string-match-p REGEXP STRING &optional START)
Same as ‘string-match’ except this function does not change the ...
6
votes
Accepted
regexp to find two consecutive and identical words not necessarily in the same line
\s- matches a whitespace character. A newline character can have whitespace syntax in some modes, but often it does not. Many programming modes change the syntax of newlines to that of a comment end, ...
6
votes
How to grep marked files in the dired mode of emacs?
If you use library Dired+ (dired+.el) then you can use command dired-do-grep (bound by default to M-g in Dired mode) to do what you request.
diredp-do-grep is an interactive compiled Lisp function ...
6
votes
support for regex look behind and ahead?
https://github.com/benma/visual-regexp-steroids.el/
Visual regexp steroids allows you to replace, search, etc. using python regex. Python regex has support for look ahead and look behind.
It even ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
regular-expressions × 498replace × 64
search × 48
font-lock × 30
org-mode × 23
query-replace × 21
string × 20
query-replace-regexp × 20
rx × 15
isearch × 12
dired × 10
elisp × 9
syntax-highlighting × 9
align × 9
highlighting × 8
newlines × 8
auctex × 7
whitespace × 7
latex × 6
evil × 6
major-mode × 6
text-editing × 6
syntax-table × 6
buffers × 5
faces × 5