Vim 片段,没有片段/模板插件 [英] Vim snippets, without snippet/templating addon

查看:28
本文介绍了Vim 片段,没有片段/模板插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我经常使用 vim,但我的工作流程通常会迫使我与其他 IDE 进行交互,所以我不是神一样的 vim 超级用户,我也不想很快成为.Vim 不是我的 IDE,我也不希望它成为.这是一款快速轻便的编辑器,不会妨碍我.

我追求的是使用和配置的简单性,而不是效率上的超级功率或金星.

到目前为止,我一直在用我的代码片段预加载几个寄存器,然后为哪个寄存器具有哪个文本块保留一个备忘单.但是我的寄存器用完了,一个字母数字的字母并没有立即提醒我我预装了什么.

存储任意数量的原始文本块,然后使用单个关键字和 1 个热键(按任意顺序:关键字<>热键)检索它们的最简单方法是什么.

  • 我不需要语言感知片段(聪明的关键字将是我的解决方案)
  • 我不需要以任何方式感知上下文或智能的片段
  • 我不打算使用迷你模板语言来简化我的代码片段,这太过分了.
  • 我的代码段应该直接粘贴到定义它们的原始文本中,没有别的.

我想我可以开始编写仅将文本块打印到当前缓冲区的命名函数.这几乎就是我所需要的,但我希望有一种更简单的方法来维护/更新我的代码片段集合.

<小时>

是否有涵盖我的用例的极简插件,或者我是否需要坚持使用我的 .vimrc/python-commands 方法?

小更新我没有意识到你可以用地图领导者链接这么多字母,这可能会提供我需要的魔法.然后我仍然需要别名回一些更大的函数集(可能使用 python 来定义命令).

解决方案

如果你想要无插件的解决方案,你可以使用:

  • 缩写

    :iabbrev obj var foo = {};:iabbrev func function foo() {}:iabbrevlipsum Lorem ipsum dolor 坐 amet,consectetuer adipiscing 精英.整数直径 augue、egestas quis、aliquam ut、venenatis ut、quam.古怪的 ut augue.整数非 neque a lectus venenatisfermentum.Morbi quis eros nec elit molestie vehicula.整数 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.南非多乐.Etiam lobortis, urna id bibendum convallis, ligula augue actor eros, a dictum sapien mi atellus.Proin vel justo.Nuncmalesuada turpis a sapien.

    如果您不介意尾随空格,您可以使用 展开它们..>

    参见:帮助缩写.

  • 插入模式映射

    您可以创建任何复杂的映射,无论是否使用.

    这里的键(不是双关语)只是选择一个很少使用的键并将其用作映射的命名空间.例如,在我的法语 AZERTY 键盘上,我有一个完全没用的 § 键(不用于 Vim,不用于法语,不用于我使用的任何编程语言).如果我想构建一个映射库,我会将它用作这些片段的领导者":

    :inoremap §obj var foo = {};:inoremap §func function foo() {<CR><CR>}<Up><Tab>:inoremap §lipsum Lorem ipsum dolor 坐 amet,consectetuer adipiscing 精英.整数直径 augue、egestas quis、aliquam ut、venenatis ut、quam.古怪的 ut augue.整数非 neque a lectus venenatisfermentum.Morbi quis eros nec elit molestie vehicula.整数 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.南非多乐.Etiam lobortis, urna id bibendum convallis, ligula augue actor eros, a dictum sapien mi atellus.Proin vel justo.Nuncmalesuada turpis a sapien.

无论您选择缩写还是映射,您都可以将它们全部保存在一个专用文件中:

~/.vim/snippets.vim

并在你的 ~/.vimrc 中获取它:

运行时片段.vim

如果您决定将该文件放在 ~/.vim/ 目录之外的某个位置,您可以使用:

source ~/path/to/snippets.vim

编辑

关于...

真的没有那么特别:你通常可以把它看作一个变量,但是,就像在数据库中插入 $foo 会插入值一样$foo,注册something 会注册{mapleader的当前值}something.

假设你创建了一个自定义的 映射:

let mapleader = ","映射b :bnext

Vim 注册 ,b.如果您决定在当前会话稍后更改 :

:let mapleader = "%"

你仍然会有 ,b.只有进一步的映射才会使用新的 :

map b :bnext

你得到 ,b%b.

表示创建映射时的含义.在使用中,它只是 ,b%b.

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

解决方案

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.

这篇关于Vim 片段,没有片段/模板插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆