Vim snippets, without snippet/templating addon

若如初见. 提交于 2019-12-08 04:12:53

问题


I use vim a lot, but my workflow usually forces me to interact with other IDE's, so I am not a god-like vim poweruser, nor am I trying to be anytime soon. Vim is not an IDE for me, nor do I want it to be. It is a fast and light editor that doesn't get in my way.

I am looking for simplicity in both usage and config, not uber power or gold stars in efficiency.

I have been preloading several registers with my snippets up until now, and then keep a cheatsheet for which register has which text block. But I am running out of registers, and one single alpha-numeric letter does not immediately remind me of what I preloaded it with.

What is the easiest way to store any number of raw text blocks, and then retrieve them using a single keyword and 1 hotkey (in either order: keyword<>hotkey).

  • I don't need language aware snippets (clever keywords will be my solution)
  • I don't need snippets that are context aware or smart in any way
  • I don't plan on using a mini-template-language to streamline my snippets, thats excessive.
  • My snippets should literally paste in the raw text they were defined with, nothing else.

I guess I could just start writing named functions that merely print a block of text into the current buffer. Thats almost all I need, but I was hoping for a little easier way of maintaining/updating my snippet collection.


Are there any minimalist plugins out there that cover my use-case, or do I need to just stick with my .vimrc/python-commands approach?

Minor Update I didn't realize you could chain so many letters with a mapleader, this may provide the magic I need. Then I will still have to alias back to some larger set of functions (probably use python to define commands).


回答1:


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.



来源:https://stackoverflow.com/questions/25091740/vim-snippets-without-snippet-templating-addon

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