LTW: Vim abbreviations
Vim and Neovim have a feature called abbreviations that let you expand a small amount of text into a large amount of text in Insert, Replace, and Command-line modes. The Neovim manual suggests using this feature for long, often-typed words. For example, if I find myself frequently typing my name, I might define an abbreviation:
iabbrev AH Andrew Halle
Now, in insert mode, I can type A then H then space and it will be expanded
to `Andrew Halle █" (block character indicates the cursor) leaving me in Insert
mode. I can also expand the abbreviation by:
- hitting Enter, which will insert a newline and leave me in insert mode.
- hitting
<C-]>which will expand the abbreviation without any additional characters and leave me in Insert mode. - hitting Esc to exit Insert mode which will expand the abbreviation and leave me in normal mode.
The Neovim manual also suggests using this feature for fixing common misspellings. For example, if I define the abbreviation:
iabbrev teh the
and then, in Insert mode, type the characters over teh moon, it will be
automatically fixed to over the moon after I type teh and Space.
This works in Command-line mode too. For example, if I often find myself
mis-spelling the command sort, perhaps I would add an abbreviation like:
cabbrev srot sort
Note: cabbrev means add the abbreviation in Command-line mode. iabbrev
means to add the abbreviation in only Insert mode, and abbrev means to add the
abbreviation in all modes.
Now, when I type :srot and Enter, it gets automatically fixed to :sort.
Here are some other things I learned about abbreviations this week that turn them into a micro-snippet-engine.
-
Abbreviations can include special characters. For example, if I want to type a LaTeX fraction, I can add an abbreviation like:
iabbrev frac \frac{ }{ }<Left><Left>Now, when I type
fracand Space in Insert mode, it expands to\frac{ }{ █ }leaving my cursor ready to type the denominator.This is a little bit of a hack because the two Lefts position the cursor inside the curly braces, and then the space is inserted giving me two spaces between the curly braces, which is what I want (I like to set off my curly braces with spaces in LaTeX. The abbreviation, as written, has two spaces between the numerator curly braces and only one space between the denominator curly braces.
-
Abbreviations can expand expressions. For example, I work at a company that wants all source files to start with a copyright header like
// Copyright YYYY (C) Company Namewhere
YYYYis the year the file was created. I added the following abbreviation to my work config:iabbrev <expr> "// Copyright " . strftime("%Y") . " (C) Company Name<CR><C-U><CR>"Another kind of hack with the carriage returns here. My Neovim config automatically continues comments to the next line when Enter is pressed. Adding
<CR><C-U><CR>to the abbreviation means that I expand this abbreviation by pressing<C-]>and a newline is automatically inserted, the comment characters deleted, and another newline automatically inserted. I'm certain this could be done in a less hacky way, I just haven't bothered to learn what it is.