3

I often need to insert the same text into a file.

My approach has been to put the text into my vimrc as a variable:

let map='<!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd">'

And then insert it with Ctrl+r, =map Enter

This works but it somehow feels wrong. It also prevents me from entering long, multiline snippets. Is there a better way to do this?

Edit: I'm open to a solution that uses a plugin, but I'm using an older version of Vim that's missing python and a few other features. I'd prefer a simple solution.

4
  • 5
    What about a snippet plugin like ultisnips or snipmate?
    – statox
    Apr 22, 2016 at 13:29
  • @statox, I haven't found a snippet plugin that I like (e.g. something lightweight and simple). I'm also using an ancient version of Vim that wasn't compiled with python, so utlisnips is out. Apr 22, 2016 at 14:16
  • 1
    Y don't you try something like mapping or abbreviations?
    – SibiCoder
    Apr 22, 2016 at 14:19
  • mu-template may not be simple as it requires other plugins and it has many features, however, it doesn't require Python. Apr 22, 2016 at 15:40

1 Answer 1

4

I came across a solution that suits me well, it may be useful for you also. I add some mappings/abbreviations in insert mode starting with \ (or any character you like, and use not all the time):

" use an abbreviation (css debugging)
iabbrev \r *{color: red !important;}

" use a mapping (html doctype insertion)
inoremap \doctype <!DOCTYPE map PUBLIC "-//OASIS//DTD DITA Map//EN" "map.dtd">

" if you want to keep cursor inside tag, use a mapping also
inoremap \td   <td></td><left><left><left><left><left>
" some more mappings

" if you used a mapping starting with \ make sure you can actually type \
inoremap \\ \

The major down point of this is that the mappings you define can overlap other actual words you may want to use, but on the other side you can create a lot of mappings without use of any external plugin.

I noticed that mapping are not displayed while typed, but you can display them with :set showcmd (thanks to a jair lopez comment)

5
  • 1
    It's basically the same thing to what Nobe4 has but you could also consider using iabbrev. see :help abbrev
    – Tumbler41
    Apr 22, 2016 at 15:22
  • I'm not sure if you can change the cursor position with an abbreviation though...
    – nobe4
    Apr 22, 2016 at 15:23
  • True, but Scribblemacher didn't specifically say he needed to move cursor position did he? It all just depends on what he needs. I was just giving options. :)
    – Tumbler41
    Apr 22, 2016 at 15:24
  • Right, I'll add your idea for the first one
    – nobe4
    Apr 22, 2016 at 15:25
  • 3
    With :set showcmd you can see a mapping as you type it, in the last line of the screen. See :help 'showcmd' for more information. Apr 22, 2016 at 21:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.