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
Emacs User's user avatar
  • 5,603
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 ...
mkcms's user avatar
  • 1,350
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,032
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 ...
phils's user avatar
  • 49.8k
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 ...
genehack's user avatar
  • 471
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 ...
Basil's user avatar
  • 12.2k
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,032
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 ...
Jordon Biondo's user avatar
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 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
  • 4,850
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.3k
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
  • 28.2k
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
  • 32.8k
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 ...
elethan's user avatar
  • 4,775
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
  • 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's user avatar
  • 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"): [...] ...
Basil's user avatar
  • 12.2k
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,083
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,435
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 ...
suvayu's user avatar
  • 1,588
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
  • 4,850
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 ...
rocky's user avatar
  • 888
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.
Harald Hanche-Olsen's user avatar
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's user avatar
  • 32.7k
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,904
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
  • 22k
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.4k

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