⌨️ Emacs finally learns how to ctrl+F.
CTRLF (pronounced "control F") is an intuitive and efficient solution for single-buffer text search in Emacs, replacing packages such as Isearch, Swiper, and helm-swoop.
CTRLF is available as a package on MELPA. The easiest way to install this package is using
straight.el:
(straight-use-package 'ctrlf)
However, you may install using any other package manager if you prefer.
To enable CTRLF, simply add to your init-file:
(ctrlf-mode +1)
Note that the autoloading has been configured so that enabling
ctrlf-modewill not actually load CTRLF until you use one of its commands.
Now the usual Isearch bindings will use CTRLF instead:
C-s:
ctrlf-forward-literal(originally
isearch-forward)
C-r:
ctrlf-backward-literal(originally
isearch-backward)
C-M-s:
ctrlf-forward-regexp(originally
isearch-forward-regexp)
C-M-r:
ctrlf-backward-regexp(originally
isearch-backward-regexp)
M-s _:
ctrlf-forward-symbol(originally
isearch-forward-symbol)
M-s .:
ctrlf-forward-symbol-at-point(originally
isearch-forward-symbol-at-point)
The design philosophy of CTRLF is to:
As such, if you are familiar with Isearch then you will probably be at home in CTRLF.
First you must start a search using one of the following keybindings:
C-s: Search forward for a literal string.
C-r: Search backward for a literal string.
C-M-s: Search forward for a regexp.
C-M-r: Search backward for a regexp.
M-s _: Search forward for a symbol.
M-s .: Search forward for the symbol at point.
During a search, the following core keybindings are available:
C-s: Move to next match. With prefix argument, also convert a regexp search back to a literal search. If there is no search query, insert the previous one, as if you had typed
M-p. This allows you to resume a search with
C-s C-s.
C-r: Move to previous match. With prefix argument, also convert a regexp search back to a literal search. If there is no search query, insert the previous one, as if you had typed
M-p. This allows you to resume a search with
C-r C-r.
C-M-s: Move to next match and convert a literal search to a regexp search.
C-M-r: Move to previous match and convert a literal search to a regexp search.
RET: Finish search, leaving point at the currently selected match.
C-g: Abort search, returning point to its original location.
The following additional keybindings are available, emulating standard Emacs bindings:
M-<: Move to first match.
M->: Move to last match.
C-v: Move to first match that appears below the currently visible part of the buffer. In other words, move down by a page.
M-v: Move to first match that appears above the currently visible part of the buffer. In other words, move up by a page.
C-l: Scroll so that the currently selected match is at the center of the window. Typing
C-lmultiple successive times, or providing a prefix argument, has the same effect as usual (see the docstring of the
recenter-top-bottomcommand).
CTRLF behavior toggles use the same bindings as in Isearch:
M-cand
M-s c: Toggle case-sensitive search. By default, search is case-sensitive only if your search contains uppercase letters, like in Isearch (following the logic in
isearch-no-upper-case-p).
M-s s: Change the search style, e.g. between literal, regexp, symbol, fuzzy, or fuzzy-regexp. Search styles are explained thoroughly later in this documentation. There is no equivalent to this in Isearch, but the binding should feel familiar.
M-rand
M-s r: Toggle between regexp and literal search style.
M-s _: Toggle between symbol and literal search style.
M-s .: Change the search input to the symbol at point, and change the search style to symbol.
M-s o: Open an Occur buffer with the existing search input.
Other than this, keybindings are completely standard. For example, to delete the last word in your search query, use
M-DEL, or to retrieve the previous search query in the minibuffer history, use
M-p.
It is standard in Emacs for typing
M-nafter entering the minibuffer to insert a default value into the minibuffer. In CTRLF, this default value is the symbol at point.
You can customize the visual appearance of CTRLF:
ctrlf-highlight-activeis used to highlight the currently selected match.
ctrlf-highlight-passiveis used to highlight the other currently visible matches.
ctrlf-highlight-lineis used to highlight the entire line on which the currently selected match resides, if
ctrlf-highlight-current-lineis non-nil (the default).
ctrlf-auto-recenteris non-nil, then the currently selected match is always kept vertically centered in the window, as if you typed
C-leach time you moved to a new match. This feature is disabled by default.
ctrlf-show-match-count-at-eolis non-nil (the default), then this information is also shown at the end of the current line in the buffer being searched, which alleviates the problem of needing to look back and forth between the minibuffer and the buffer being searched.
^$which would identify all blank lines the buffer) are displayed as thin vertical rectangles with a solid color since there is no text to highlight. The width of these rectangles relative to the width of a normal character is defined by
ctrlf-zero-length-match-width.
You can also customize the keybindings:
ctrlf-mode-bindingslists keybindings that are made globally available in Emacs when
ctrlf-modeis enabled.
ctrlf-minibuffer-bindingslists keybindings that are made available in the minibuffer during a CTRLF search session.
In addition to the functions already bound in
ctrlf-minibuffer-bindings, you can choose to bind
ctrlf-next-matchand
ctrlf-previous-match. These functions are the same as
ctrlf-forwardand
ctrlf-backward, but they do not have the special features of inserting the previous search, changing to a literal search, or starting a new search when not already in a search session.
You can customize the behavior:
ctrlf-go-to-end-of-matchis nil, then the cursor will move to the beginning of the match instead of the end.
CTRLF implements support for literal and regexp using an extensible search style system. This functionality is configured using the
ctrlf-style-alistuser option. End users need not touch this option unless they wish to do advanced customization or are developing a package which integrates with CTRLF.
The keys of
ctrlf-style-alistdefine the available search styles. These styles appear in two places:
ctrlf-change-search-stylecommand bound by default to
C-o s.
STYLEargument to the
ctrlf-forwardand
ctrlf-backwardfunctions.
Basically, a search style defines a way to transform the user's search query into a regexp which can be passed to
search-forward-regexpor
search-backward-regexp. Here are the built-in search styles:
literal: Search for an exact match to the query string, subject to case folding (
C-o c). This is implemented using
regexp-quote.
regexp: Search for a regexp provided by the user. If the regexp is invalid, CTRLF will display an error message in the minibuffer until the problem is corrected.
fuzzy: Split the query string on spaces and search for an occurrence of all the sub-parts separated by arbitrary text. To include a literal space, or more than one, simply add an additional space. For example,
foo $42 baz quuxis turned into
foo.*\$42.*baz quux.
fuzzy-regexp: Same as
fuzzyexcept that the individual sub-parts are interpreted directly as regexps, so that
foo $42 baz quuxis turned into
foo.*$42.*baz quux.
To define a custom search style, you should proceed according to the following steps:
ctrlf-style-alistwith:
:prompt).
:translator).
ctrlf-forward-literaland
ctrlf-backward-literalthat use
ctrlf-forwardand
ctrlf-backward, respectively, as subroutines.
ctrlf-mode-bindings.
ctrlf-modeis a globalized minor mode that enables the buffer-local minor mode
ctrlf-local-mode. This makes it possible to disable it when there is a conflict, for example with
pdf-isearch-minor-modefrom pdf-tools:
(add-hook 'pdf-isearch-minor-mode-hook (lambda () (ctrlf-local-mode -1)))
The minibuffer history for CTRLF is stored in the variable
ctrlf-search-history. You can access it during a search session using
M-pand
M-n. Typing
M-nat the beginning of a session will perform a search for the symbol at point. Furthermore, typing
C-sor
C-rwithout any search query is a synonym for
M-p.
CTRLF integrates with evil-mode's jump-list and search history features. You will be able to jump with
C-oand
C-iand continue a search with
nand
Nbased on your CTRLF searches.
This section documents why I decided to write CTRLF instead of using any of the numerous existing solutions in Emacs.
Isearch has the right idea for buffer search, but it has serious usability problems. The main issue, for me, is that it feels extremely fragile. If you type any command that is not bound in Isearch, then you exit your search and run that command. This means editing your search query is awkward and unintuitive. Another issue is predictability. The behavior of
C-gis hard to predict because it depends on not only what you have typed in what order, but also on the buffer contents and the state of the current search. The handling of wraparound exacerbates problems of predictability: the number of times you must type
C-sto move to the next match is unpredictable and the minibuffer prompt changes in several different and confusing ways during this process.
CTRLF takes the basic idea of Isearch, together with most of its keybindings, but emulates the more reliable user experience of web browser text search. For example: all editing commands can be used during a search as usual;
C-galways has the effect of canceling the search; and
C-salways moves to the next candidate, with wraparound signaled by an overlay which indicates the current match index and the total number of matches (another UI paradigm borrowed from other programs).
The selling point of Swiper is that it shows you an overview of the matches. Ask yourself: when was the last time you actually got anything useful out of that overview? Since all the matches are crammed together, one per line, there is not enough context for meaningful information to be communicated. Furthermore, Swiper constrains itself almost exclusively to line-based search by design, which makes it unsuitable to the task of quick movement within a line or movement to a commonly occurring search string.
For basically the same reason that Swiper is not a good solution, with the added complaint that Helm is extraordinarily complex and in-your-face about this complexity. Single-buffer text search is not a terribly difficult problem, and given that CTRLF is only about 1,100 lines of code, I think that the 1,700 lines of code of
helm-swoopplus the 10,600 that come with Helm is a bit overkill.
M-x occur?
M-x occurimplements noninteractive search, and therefore unsuitable as a tool for quickly jumping to other parts of the buffer.
It does not usefully support text search outside the currently visible window.
Please see the contributor guide for my projects.