Lesson 25 of 26

Abbreviations in Vim

Duration: 2:25 Watch on YouTube (external link, opens in new tab)

Markdown version

In this lesson

Roy describes abbreviations as text completions on the fly: he defines one that expands my email into his email address, which could also be kept in the vimrc, and in insert mode it expands as soon as he types a character that is not alphanumeric. He suggests them for long text such as copyright notices and email signatures, or for common spelling mistakes. Unlike a mapping, an abbreviation is only expanded when it is typed outside of another word.

Transcript

347 words

[00:09] (external link, opens in new tab) In Vim are really yummy little snack feature kind of thing. Kind of like a little popcorn that you can put into your Vim soup and just enjoy it continuously and add to it as much as you like. And basically they're just text completions on the fly. You know, let's say that you always type your email and you don't want to write your email always. You just want to have text auto expanded to that. Well, you just type the command. Sorry, let's out get out of insert mode. Type the command IB

[00:40] (external link, opens in new tab) Riv. I just type tab to go there. and you just say when I type my my email, I want you to expand it to roy.com. So now if I have that or if I had that in my vimrc, all I have to do is to say let's go into insert mode. My email is my email. And if I press anything that's not alpha numeric,

[01:11] (external link, opens in new tab) It gets expanded automatically. And technically, we could always configure Vim to say on what it gets expanded on, etc. But that's the basic idea. You can just have abbreviations for things that are long like copyright or email signatures or or sometimes people use them just for common spelling mistakes uppercase lowerase mistakes. Anything really is just a really simple powerful nugget that you use. Now you might ask why wouldn't I use that instead as a u binding as a mapping and the answer is that mapping does not take does not take into account the text that you wrote before.

[01:54] (external link, opens in new tab) So technically I could type that text my email into a mapping but then if it was inside another word then that other word would get halfway would get expanded into my text. This does not happen if I just use abbreviations because Vim is smart enough to know what came before the abbreviation, what came after, and it looks specifically for the fact that you type the abbreviation outside of a word. So that's it. Hope you enjoy this little nugget.