Advanced filtering utilities in Vim
Efficient Search and Replace

© Lead Image © adimas, fotila.com
Whether you are writing code or text, vim-abolish can help you customize search and replace functions in Vim.
Traditionally, vim-abolish is introduced by saying that what it does is hard to explain. This tradition dates back to its creator Tim Pope, who introduced it by saying that "I've deferred release, primarily because it's so gosh darn hard to explain" [1]. However, while exactly what is being abolished is uncertain, vim-abolish can best be described as advanced search and replace functions, roughly analogous to but less powerful than the command sed
. It consists of four utilities: abbreviation (word completion), substitution, search, and coercion (case change). With a bit of organization, you can set up these utilities to use multiple forms of a word, such as different tenses, prefixes, suffixes, or spellings. With these tools, vim-abolish is equally useful for writing code or text documents.
Vim-abolish is installed by downloading the plugin from its homepage [1] to ~/.vim
or ~\vimfiles
, depending on your distribution. If you are using the plugin manager Pathogen, you can install Git and run the commands:
cd ~/.vim/bundle git clone git://github.com/tpope/vim-abolish.git
The command installs the executable to ~/.vim/bundle/vim-abolish
. From within Vim, you can run :help Abolish
for examples of how to use each utility (Figure 1), including some complex search patterns [2].
The Command Structure
Vim-abolish offers two command structure choices. The most verbose command structure begins with :Abolish
and varies with each utility. For example, if you are using the abbreviation utility (see below), the structure would be:
:Abolish [options] {abbreviation} {replacement}
However, if you are using the search utility, the structure becomes:
:Abolish! -search OPTIONS PATTERN
The other command structure option, :Subvert
, can also vary, but tends to be shorter and more consistent than Abolish
. It can be used for substitution, search, and coercion, but not abbreviation. For example, Subvert
's basic search command structure is:
:Subvert/PATTERN/FLAGS
Even more importantly, the command can be abbreviated to
:S/PATTERN/FLAGS
which also has the advantage of using fewer, lesser used keys that might tempt you to look down at the keyboard rather than the screen.
With both the Abolish
and Subvert
commands, you can use comma-separated strings in curly braces so that the command covers variations of the basic pattern, such as:
:S/gues{s,sed,stimate}/
In this example, the basic pattern of gues is supplemented by the flags or variations of s, sed, and stimate. With this structure, the command will find guess, guessed, and guesstimate in both lowercase, title case, and uppercase. A command can be any number of patterns, each with its own completion flags. For instance, the help file gives the example
:Abolish {despe,sepa}rat{e,es,ed,ing,ely,ion,ions,or}
which finds about 50 words, including desperate, separate, separates, and separated.
Such patterns make vim-abolish concise but also require careful planning. At the risk of not using the plugin's full power, new users should probably begin with simple patterns, avoiding more complex ones until they are comfortable with the command structure.
Abbreviation
Abbreviation is a more powerful version of Vim's substitution command. It creates autocorrect entries that can be stored for use with other files. Abbreviation entries can save typing or correct typos, serving as a personalized spell checker.
The command structure is:
:Abolish OPTIONS ABBREVIATION REPLACEMENT
For instance, once you enter the command:
:Abolish cmns communications
cmns is changed to communications. If you type Cmns, it becomes Communications, and CMNS becomes COMMUNICATIONS. No extra command structure is needed for this support of different letter cases. However, both the abbreviation and the correction must be a single word, and you cannot use single or double quotation marks to make the command treat a phrase as a word.
Similarly, if you regularly type fro for for, you can correct it automatically with:
:Abolish fro for
By default, abbreviations work only in Vim's Insert mode while the current document is open. However, if you add the option -buffer
after the basic command, the abbreviation will work throughout the current buffer as well.
Similarly, adding the option -cmndline
will make the abbreviation work in the Vim command line, whereas adding an exclamation mark to the basic command (:Abolish!
) saves the abbreviation for general use in Vim. Abbreviations are saved in ~./.vim/after/plugin/abolish.vim
.
To delete an abbreviation as thoroughly as possible from within Vim, enter the command:
:Abolish --delete -buffer -cmndline ABBREVIATION
The -buffer
and -cmndline
options, of course, can be omitted if you are sure they were not used in creating the abbreviation.
Search
The command structure for search is either:
:Abolish -search STRING
or
:S/STRING/
By default, both commands search forward from the present cursor position in the document, looping back to the beginning and continuing to the starting point. Alternatively, both :Abolish!
or :S?TERM?
search backward from the cursor position, looping back to the end of the document to complete the search.
Searches can be further refined with -flags=OPTION
inserted after the Abolish
command
:Abolish -flags=OPTION -search STRING
or placed after the search term in the abbreviated :Subvert
command:
:S/STRING/ -search STRING
With both Abolish
and Subvert
searches, the I
option disables support for case variations so that only the exact string entered is located. Additionally, v
finds only variable names in code and w
only complete words. These options are not always needed, but they can sometimes help to avoid false positives.
Vim-abolish's search
command also supports grep options and regular expressions. For instance,
:S/gues{s,sed,stimate}/*.txt
finds the results guess.txt, guessed.txt, and guesstimate.txt.
Buy Linux Magazine
Subscribe to our Linux Newsletters
Find Linux and Open Source Jobs
Subscribe to our ADMIN Newsletters
Support Our Work
Linux Magazine content is made possible with support from readers like you. Please consider contributing when you've found an article to be beneficial.
News
-
Fedora 39 Beta is Now Available for Testing
For fans and users of Fedora Linux, the first beta of release 39 is now available, which is a minor upgrade but does include GNOME 45.
-
Fedora Linux 40 to Drop X11 for KDE Plasma
When Fedora 40 arrives in 2024, there will be a few big changes coming, especially for the KDE Plasma option.
-
Real-Time Ubuntu Available in AWS Marketplace
Anyone looking for a Linux distribution for real-time processing could do a whole lot worse than Real-Time Ubuntu.
-
KSMBD Finally Reaches a Stable State
For those who've been looking forward to the first release of KSMBD, after two years it's no longer considered experimental.
-
Nitrux 3.0.0 Has Been Released
The latest version of Nitrux brings plenty of innovation and fresh apps to the table.
-
Linux From Scratch 12.0 Now Available
If you're looking to roll your own Linux distribution, the latest version of Linux From Scratch is now available with plenty of updates.
-
Linux Kernel 6.5 Has Been Released
The newest Linux kernel, version 6.5, now includes initial support for two very exciting features.
-
UbuntuDDE 23.04 Now Available
A new version of the UbuntuDDE remix has finally arrived with all the updates from the Deepin desktop and everything that comes with the Ubuntu 23.04 base.
-
Star Labs Reveals a New Surface-Like Linux Tablet
If you've ever wanted a tablet that rivals the MS Surface, you're in luck as Star Labs has created such a device.
-
SUSE Going Private (Again)
The company behind SUSE Linux Enterprise, Rancher, and NeuVector recently announced that Marcel LUX III SARL (Marcel), its majority shareholder, intends to delist it from the Frankfurt Stock Exchange by way of a merger.