Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
32 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 ...
mkcms's user avatar
  • 1,370
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 ...
zck's user avatar
  • 9,102
19 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 ...
phils's user avatar
  • 51.5k
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 ...
genehack's user avatar
  • 481
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 ...
Basil's user avatar
  • 12.4k
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"))) "...
zck's user avatar
  • 9,102
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 ...
rpluim's user avatar
  • 5,315
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 ...
justbur's user avatar
  • 1,510
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) ......
rpluim's user avatar
  • 5,315
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 ...
Stefan's user avatar
  • 26.5k
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"): [...] ...
Basil's user avatar
  • 12.4k
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 ...
suvayu's user avatar
  • 1,638
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 ...
NickD's user avatar
  • 30.4k
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 ...
Tobias's user avatar
  • 33.2k
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's user avatar
  • 33.1k
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's user avatar
  • 33.1k
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 ...
npostavs's user avatar
  • 9,203
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 (\...
Juancho's user avatar
  • 5,465
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 ...
jue's user avatar
  • 4,576
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 ...
rpluim's user avatar
  • 5,315
7 votes
Accepted

How to match symbol in regexp?

What is wrong with \_<.*?\_> ?
fulvio ciriaco's user avatar
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
icarus's user avatar
  • 1,914
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 ...
wasamasa's user avatar
  • 22.2k
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: ...
xuchunyang's user avatar
  • 14.6k
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 ...
phils's user avatar
  • 51.5k
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, ...
Drew's user avatar
  • 77.8k
6 votes
Accepted

How do I replace only the first space on a line?

You can use the regexp “^\([^ ]*\)”. Please note that it ends with a space. There is also a single space character inside [^ ]. Specify that one, and replace with “\1X” (where the X stands for ...
Harald Hanche-Olsen's user avatar
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 ...
Drew's user avatar
  • 77.8k

Only top scored, non community-wiki answers of a minimum length are eligible