Fandom

Vim Tips Wiki

Use abbreviations for frequently-used words

1,624pages on
this wiki
Add New Page
Talk0 Share

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.

Tip 610 Printable Monobook Previous Next

created 2003 · complexity intermediate · version 6.0


Vim can expand abbreviations entered while in insert mode. An abbreviation is defined by adding a line like the following to your vimrc:

iabbrev <keys> <expansion>

Replace <keys> with the letter or letters which should be expanded to <expansion>, for example:

iabbrev #i #include (typing "#i" and space will be expanded to "#include")
iabbrev #d #define  (typing "#d" and space will be expanded to "#define")
iabbrev s struct    (typing "s" and space will be expanded to "struct")
iabbrev t typedef   (typing "t" and space will be expanded to "typedef")

In some cases Vim expands a letter automatically that you don't want. You have to watch out for that.

When entering a word which is already in the buffer, Ctrl-P or Ctrl-N can be used to autocomplete the word currently being entered. Autocompletion does not require abbreviations to have been defined, but it only works for a word consisting of characters matching the 'iskeyword' option.

If wanted, dictionary completion can used to define words that can be autocompleted. For example, with the following settings, Ctrl-P or Ctrl-N will work with words defined in the specified dictionary file:

:set complete+=k
:set dictionary+=/your/dict/file

CommentsEdit

You can put your common typos as abbreviations, for auto correction:

iabbrev teh the
iabbrev seperate separate

> How cut the space from the resulted substitution?

From :help abbreviations: An exception to this is the character <C-]> (Ctrl-]), which is used to expand an abbreviation without inserting any extra characters.

Example:

:iabbrev hh hello
    "hh<Space>" is expanded to "hello<Space>"
    "hh<C-]>" is expanded to "hello"

Use getchar() to eat up that space, for example:

iabbrev <t <target name="%"></target><Esc>F%s<c-o>:call getchar()<CR>

Regarding how to eat the last typed character (when it is a space): Use :Iabbr and :Inoreabbr from script#50.


See the SuperTab plugin. It does almost all of this without the need for programming.


To eat the last space, for example, with:

iabbrev did <div id="

In insert mode, type did then press Ctrl-]

Result: The abbreviation is expanded with no extra characters, and you are still in insert mode and can continue typing.


Also on Fandom

Random Wiki