Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

Vimscript local buffer abbreviation


May 24, 2021 Vim


Table of contents


The last chapter talks about more things, fully understand will be a little difficult, so this chapter is a little easier. Now that we've learned how to define the mapping and setting options for a local buffer, we're learning the abbreviations for the local buffer in the same way.

Open your foo and bar files, switch to foo execute the following command:

:iabbrev <buffer> --- &mdash;

Enter the following text by entering insertion mode under file foo

Hello --- world.

Vim replaces the --- Hello for you. N ow switch to bar try. Replacement bar occur in bar because the abbreviation we define is set to the local buffer for foo only.

Automatic commands and abbreviations

Use the abbreviations and automatic commands of the local buffer to create a simple "snippet" system.

Execute the following command:

:autocmd FileType python     :iabbrev <buffer> iff if:<left>
:autocmd FileType javascript :iabbrev <buffer> iff if ()<left>

Open a Javascript file and enter the iff T hen open a Python file to try. Vim executes the appropriate abbreviation on the current line based on the file type.

Practice

Create more "snippet" abbreviations for different types of files for files you edit frequently. Y ou can create return for most languages, an abbreviation for function for javascript, and an &ldquo; and &rdquo; The abbreviation for .

Add the snippets you created to your ~/.vimrc file.

Remember: The best way to learn to use these snippets is to disable the way you do these things before. E xecute: :iabbrev <buffer> return NOPENOPENOPE will force you to use abbreviations, and this command will not output anything when you enter the return. To save time on learning, create an acronym above for all the snippets you just created to force you to use the snippets you created.