Live data from Hacker News

Built-In Vim Autocomplete

github.com

21–30 of 30 posts

Re: Built-In Vim Autocomplete

#21
post #2

This whole time I had no idea vim could autocomplete

This is classic "string-based" autocomplete. It completes based on strings it knows about but doesn't take into account the context. I've used for years but yesterday I realized that since Vim version 7 there's a more intelligent kind of autocomplete triggered by C-x C-o that is context-aware. It can complete object methods, HTML tag attribtes, CSS properties, etc. For info: :help new-omni-completion

I think it must take into account some level of context because I've noticed that it tends to give me exactly what I want the first time I press it.

Re: Built-In Vim Autocomplete

#22
post #19

Long time emacs user here. I had an aha moment yesterday, when after several months of using God mode I said "I should rebind C-t to generic transpose and use a follow-up key to specify char, word, sexp, etc." If I understand correctly, this is sort of how VIM works. I want to try EVIL now. Anyone here that's made the switch in this direction and can talk about it?

I switched to Evil mode and after a year I love it. I wrote the book to help me learn more about it!

Re: Built-In Vim Autocomplete

#23
post #6

Here is a way to bind autocomplete to TAB such that you still get TAB unless you are at the end of a word to complete. It's super convenient. function! InsertTabWrapper() let col = col('.') - 1 if !col || getline('.')[col - 1] !~ '\k' return "\ " else return "\ " endif endfunction inoremap =InsertTabWrapper()

I had something like that in my vimrc for some time (snippet posted here: https://vimrcfu.com/snippet/222), using omni-completion when set in the buffer and keyword completion otherwise. Then, I developed the idea into a plugin that retains the minimalistic spirit, but it's much more flexible: https://github.com/lifepillar/vim-mucomplete.

Re: Built-In Vim Autocomplete

#24
As I like to say about so many things, "The Vim is large, and I'm so very small." Been using Vim, or vi since the eighties, and I didn't know there was any kind of auto/omnicomplete. Or maybe I knew and forgot. I forget.

Re: Built-In Vim Autocomplete

#25
post #16
post #6

Here is a way to bind autocomplete to TAB such that you still get TAB unless you are at the end of a word to complete. It's super convenient. function! InsertTabWrapper() let col = col('.') - 1 if !col || getline('.')[col - 1] !~ '\k' return "\ " else return "\ " endif endfunction inoremap =InsertTabWrapper()

I made a plugin [1] similar to this which offers more options when you press the Tab key. For instance, if you're typing a file path, you get Ctrl-X Ctrl-f completion (vim's file path completion) instead of the regular keyword completion. And if you type a period (configurable) and press Tab in filetypes like C, Python, Ruby, etc, you get omni completion (vim's semi-intelligent completion, which offers methods on cla…

What's wrong with these others? I'm reacting to the word "bloat," which is frequently thrown around on HN. To me, generally bloat is probably a good thing, as it means additional functionality and nice-ites.

Re: Built-In Vim Autocomplete

#26
post #16
post #6

Here is a way to bind autocomplete to TAB such that you still get TAB unless you are at the end of a word to complete. It's super convenient. function! InsertTabWrapper() let col = col('.') - 1 if !col || getline('.')[col - 1] !~ '\k' return "\ " else return "\ " endif endfunction inoremap =InsertTabWrapper()

I made a plugin [1] similar to this which offers more options when you press the Tab key. For instance, if you're typing a file path, you get Ctrl-X Ctrl-f completion (vim's file path completion) instead of the regular keyword completion. And if you type a period (configurable) and press Tab in filetypes like C, Python, Ruby, etc, you get omni completion (vim's semi-intelligent completion, which offers methods on cla…

There's also mu-complete[1] which is much like VimCompletes me - small and lightweight using only vim's built-in functionality. Doesn't work very well with vim (I haven't actually checked yours out, but I assume they're very similar in what they offer)

[1]: https://github.com/lifepillar/vim-mucomplete

Re: Built-In Vim Autocomplete

#27

Vim's built-in autocomplete is the only autocomplete I use. Does anyone know why I should use something else?

I find a plugin (mu-complete https://github.com/lifepillar/vim-mucomplete) which makes use of vim's built in completion makes my life that little bit easier.

Re: Built-In Vim Autocomplete

#28
post #19

Long time emacs user here. I had an aha moment yesterday, when after several months of using God mode I said "I should rebind C-t to generic transpose and use a follow-up key to specify char, word, sexp, etc." If I understand correctly, this is sort of how VIM works. I want to try EVIL now. Anyone here that's made the switch in this direction and can talk about it?

I used emacs for 20 years. Around about 2007 I started doing all of my development on a tiny netbook and emacs was using too much memory. I switched to vim. It took me a good year to get used to it, but now I'm hooked on that way of thinking. About a year ago I switched back to emacs with evil mode. It is close enough to vim that I can barely tell the difference. If you are already familiar with emacs and have a setup that works for you, then switching to evil should be less painful than someone switching from vim.

I think the biggest thing to accept is that you will not be proficient with your editor for quite a long time. I recommend practicing with vim tutorials until it feels reasonably good before taking the plunge entirely. One very important thing is to keep looking for efficient ways to work with it. It's a bit like emacs in that if you ever think, "Oh, this is just painful. There must be a better way", then there is almost guaranteed to be a better way. Stop, find out what it is. Practice it.

My last piece of advice is that vim-style editing is a little bit different that emacs-style editing (at least for me). With emacs, I find that I memorise keybindings and practice using them. With vim, it's much more about the context -- when you are in situation X, you do this kind of thing. So it needs more practice (again IMO). However, once you have your brain oriented (orientated? :-) ), there is this sense of freedom. You are working with larger blocks of abstraction, rather than just the character level. Some people feel this is more efficient (i.e. faster), but I'm not sure. For me it's rather that it is closer to what I'm actually thinking when I'm programming. YMMV and I should warn you again that it takes considerable practice to get to the point where it starts to feel good.

Re: Built-In Vim Autocomplete

#29
post #6

Here is a way to bind autocomplete to TAB such that you still get TAB unless you are at the end of a word to complete. It's super convenient. function! InsertTabWrapper() let col = col('.') - 1 if !col || getline('.')[col - 1] !~ '\k' return "\ " else return "\ " endif endfunction inoremap =InsertTabWrapper()

I got that one some time ago from https://github.com/garybernhardt/dotfiles/blob/master/.vimrc... which offers several other useful functions and settings.

It's nice to point that out. It shows the binding for shift-tab, as well.

  function! InsertTabWrapper()
        let col = col('.') - 1
        if !col || getline('.')[col - 1] !~ '\k'
                return "\"
        else
                return "\"
        endif
  endfunction
  inoremap   InsertTabWrapper()
  inoremap  

Re: Built-In Vim Autocomplete

#30
post #7

What is xctrl?

You mean `Ctrl-xCtrl-f`? That's `Ctrl-x Ctrl-f`. In terms of how it parses, after `Ctrl-`, the next token must be a single character (a key) is accepted. So `Ctrl-x` completes that particular accord and `Ctrl-f` is the accord following it.

Thanks ... that was puzzling me too. And I think you mean "chord" rather than "accord" :-)
Post reply on HN