3

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).

user2097818
  • 1,821
  • 3
  • 16
  • 34
  • abbreviation might be what you are looking for `h :iabbrev` Although setting up a lot will be annoying (especially if they are multiple lines). Or maybe just insert mode mappings. I'm not sure I recommend either. – FDinoff Aug 02 '14 at 04:46
  • @FDinoff these are multiline blocks, probably important to mention – user2097818 Aug 02 '14 at 05:27
  • 1
    If you want to us Python code for making snippets, consider using [ultisnips](https://github.com/SirVer/ultisnips), even though it's a plugin. – Roland Smith Aug 02 '14 at 09:12

1 Answers1

11

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.

romainl
  • 186,200
  • 21
  • 280
  • 313
  • if I clear out my current mapleader bindings, is that the same as the *unique * namespace you refered to? what is difference with no leader? Will one way **misbehave** more than the other? – user2097818 Aug 02 '14 at 10:11
  • 1
    If you already have a `f` mapping, adding a `foo` mapping will introduce ambiguity and cause Vim to wait for a second or so when you type `f`. My suggestion to use a different "leader" is based on the assumption that you would want your snippets to be separate from your regular `` mappings. – romainl Aug 02 '14 at 11:02
  • Just to make sure I understand better; there is nothing special about it, except that you can change its value in one location, and anywhere the was used will auto-update with it. Otherwise, leader has no special privileges beyond literally typing in values as you illustrated in your answer? (its just more work if I change my mind)? Sound good?, If so, I think you have me sold. – user2097818 Aug 02 '14 at 11:10
  • You covered it well. Ive already made several series of keymaps that all call into 2 python defined commands, passing 1 simple keyword, all handled within 1 extra snippet file (JSON packaging). And its easy enough to call up my defined keymaps as a wannabe autocomplete, quick-reference card. Thanks for help, once I tune the autocomplete, Ill try to remember to post the python utilities that wrap it all up. – user2097818 Aug 02 '14 at 15:23