Blog 1

Random Talk on Random Thoughts

My Improved Custom Autocompletion Mappings in Vim

| Comments |

Background

Inspired by $\rm \LaTeX$-Suite, I added some autocompletion mappings in my VIMRC last year.1 Three months later, I saw the word “FileType” in Vim’s User Manual, and shortened these autocommand a little bit.2 In my opinion, it’s incredibly amazing since I can type text at the speed of thought.

Problem

If I’ve finished editing an HTML file and open another $\rm \LaTeX$ file, then my insert mode mappings will interfere with those defined by $\rm \LaTeX$-Suite. How can I restrict my custom mappings to the current working buffer only?

Since I didn’t know how to describe this problem in words in February, I spent hours searching for webpages on Google, but got nothing. At that time, I had focus on my homework and exams, so I put this problem aside. As a GNU/Linux user, I adjust my wants according to what I know and what I can do, after accepting the fact that M$ Off* has no known plan to support the free operating system.

Last night, when I wrote a recent linklog about Vim’s auto indenting, I did some configurations on Vim. This reminded me of hte problem of setting up autocompletion for brackets. After writing over 150 blog posts in Vim, I realise the inconvenience without these keyboard shortcuts for autocompletion in the insert mode.

Solution

First, I glanced through the codeblocks in the cited posts in the first two footnotes. After that, I googled “autocmd group”, and found two sites very useful.

  1. Vim inoremap for specific filetypes on Stack Overflow.

    <buffer> is the missing part of the autocommands.

  2. Autocommand Groups on Learn Vimscript the Hard Way.

    au! avoids Vim to slow down after running :so $MYVIMRC. Therefore, I also added ! after the function MyDiff in my VIMRC on M$ Win* 7.3


  1. See Links to Good Posts in Blog 1 for the mappings. 

  2. See the bottom part of Working With Vim’s Autocommand Groups in Blog 1 for the shorter mappings. 

  3. View my VIMRC on M$ Win* 7 at commit c11d1a1.

    This morning, I tried to write similar keyboard shortcuts for ‘'’, ‘"’ and ‘`’ at lines 62–63 and 67. However, typing two ‘"’s quickly on M$ Win* 7, GVim duplicated my input. Therefore, I changed the rules so that they would only be invoked when one quickly typed any one of these characters twice.

    Remarks: On Ubuntu 14.04 LTS, I have the same experience. Thus, I just copied the whole autocommand group to my VIMRC there

Comments