Vim snippets, without snippet/templating addon

[亡魂溺海] 提交于 2019-12-07 23:41:44

If you want plugin-free solutions you can use:

  • abbreviations

    :iabbrev obj    var foo = {};<Left><Left>
    :iabbrev func   function foo() {<CR><CR>}<Up><Tab>
    :iabbrev lipsum Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer diam augue, egestas quis, aliquam ut, venenatis ut, quam. Quisque ut augue. Integer non neque a lectus venenatis fermentum. Morbi quis eros nec elit molestie vehicula. Integer nunc lacus, sodales posuere, rutrum quis, blandit at, mi. Vivamus imperdiet wisi vel mauris. Morbi mattis ante non metus. Sed turpis dui, fermentum ut, aliquam eget, vulputate ullamcorper, pede. Nam non dolor. Etiam lobortis, urna id bibendum convallis, ligula augue auctor eros, a dictum sapien mi a tellus. Proin vel justo. Nunc malesuada turpis a sapien.
    

    You can expand those with <Space> if you don't mind the trailing space or with <C-]> if you do.

    See :help abbreviations.

  • insert mode mappings

    You can create any complex mapping you want, either using <leader> or not.

    The key (pun not intended), here, is just to choose a rarely used key and use it as a namespace for your mappings. On my French AZERTY keyboard, for example, I have the § key which is totally useless (not used in Vim, not used in French, not used in any programming language I work with). If I wanted to build a library of mappings I would use it as "leader" for those snippets:

    :inoremap §obj    var foo = {};<Left><Left>
    :inoremap §func   function foo() {<CR><CR>}<Up><Tab>
    :inoremap §lipsum Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integer diam augue, egestas quis, aliquam ut, venenatis ut, quam. Quisque ut augue. Integer non neque a lectus venenatis fermentum. Morbi quis eros nec elit molestie vehicula. Integer nunc lacus, sodales posuere, rutrum quis, blandit at, mi. Vivamus imperdiet wisi vel mauris. Morbi mattis ante non metus. Sed turpis dui, fermentum ut, aliquam eget, vulputate ullamcorper, pede. Nam non dolor. Etiam lobortis, urna id bibendum convallis, ligula augue auctor eros, a dictum sapien mi a tellus. Proin vel justo. Nunc malesuada turpis a sapien.
    

Whether you choose abbreviations or mappings, you can save all of them in a dedicated file:

~/.vim/snippets.vim

and source it in your ~/.vimrc:

runtime snippets.vim

If you decide to put that file somewhere outside your ~/.vim/ directory, you can source it with:

source ~/path/to/snippets.vim

edit

About <leader>

<leader> is really not that special: you can generally think of it as a variable but, just like inserting $foo in a database will insert the value of $foo, registering <leader>something will register {current value of mapleader}something.

Supposing you create a custom <leader> mapping:

let mapleader = ","
map <leader>b :bnext<CR>

Vim registers ,b. If you decide to change you <leader> later in the current session:

:let mapleader = "%"

you'll still have ,b. Only further mappings will use the new <leader>:

map <leader>b :bnext<CR>

You get both ,b and %b.

<leader> means something only when you create a mapping. In usage, it's just ,b or %b.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!