Home » Odeon Blogs » Nai Chng »

Learning Vim: Tab Autocomplete

Learning Vim: Tab Autocomplete

I recently made the switch to Vim mainly due to my frustrations with folder navigation in Geany. When pairing with my colleagues, seeing them move through folders and changing code made me feel like I was programming with the handbrakes on. 


Two weeks on, I've been learning more tricks day by day and I can say it's one of the best decisions I've made so far in my short programming journey. For those who are learning, I found this blog post to be very true in that I learnt vim progressively. I had an idea of the basics before I decided to make the jump permanently.

One of my major gripes with Vim was that the auto-complete shortcut was mapped to control-n. This felt very unnatural to me. So, I hunted around for a snippet I could paste into my .vimrc file that could solve this problem and viola, I found the follow code snippet on http://vim.wikia.com/wiki/Autocomplete_with_TAB_when_typing_words:

  1. "Use TAB to complete when typing words, else inserts TABs as usual.
  2. "Uses dictionary and source files to find matching words to complete.
  3. "See help completion for source,
  4. "Note: usual completion is on <c-n> but more trouble to press all the time.
  5. "Never type the same word twice and maybe learn a new spellings!
  6. "Use the Linux dictionary when spelling is in doubt.
  7. "Window users can copy the file to their machine.
  8. function! Tab_Or_Complete()
  9. if col('.')>1 && strpart( getline('.'), col('.')-2, 3 ) =~ '^\w'
  10. return "\</c-n><c-n>"
  11. else
  12. return "\<tab>"
  13. endif
  14. endfunction
  15. :inoremap </tab><tab> <c-r>=Tab_Or_Complete()<cr>
  16. :set dictionary="/usr/dict/words"</cr></c-r></tab></c-n>


Category: vim


Tagged as: vim



Leave a Comment :

(required)


(required)




(required)




(required)






Leave a Comment


Page generated in: 0.21s