DarkMatter in Cyberspace
  • Home
  • Categories
  • Tags
  • Archives

Text Expansion in Vim


Vim

You have 2 options: imap and abbr. Use "pt" for abbreviation of "Puppet" for example, first open your vim or gVim:

  • imap: ":imap pt Puppet";

  • abbr: ":ab pt Puppet";

The difference is, imap will expand text right after you press "t", while for abbr, you have to type a space or tab or enter after the "t" to perform text expansion. So when writing English, use "abbr" for it's the "normal" way. If you write Chinese with English, imap is the better way, because there is probably no space after the expanded words.

Cursor control

Sometimes we need add some placeholders in the expanded texts, but don't want install a template plugin, like vim-snipmate. We can add some interesting stuff to imap command.

For example, we want to put cursor at <title> in the following texts:

[source,shell,title='<title>',caption="代码清单{chNo}-{counter:} "]
----
----

You can use the following map to bring cursor back to the right position after the text expansion:

imap &c [source,shell,title='',caption="代码清单{chNo}-{counter:console_enhance_src_no} "]<CR>----<CR>----<ESC>2k020la

After the text expansion, we first go back to normal mode with <ESC>, or temporarily with <C-O>, then go to the head of line with 0, and finally move the right place with 20l.

Ctrl-o let you run only one ex command directly inside insert mode and back to insert mode right after the command is over.

Autokey

You can define 'Phrase' in Autokey. For example, when I want to input $TODO$, I can define a keyboard shortcut fml. So every time when I input fml and a space (as an expansion trigger), it will be expand automatically.

Verified on Ubuntu 14.04 Unity Desktop, 2016.12.2.



Published

Mar 19, 2014

Last Updated

Mar 6, 2020

Category

Tech

Tags

  • text expansion 1
  • vim 92

Contact

  • Powered by Pelican. Theme: Elegant by Talha Mansoor