Tuesday, August 14, 2007

Vim Tips for Java #4: Use 'Tab' for Syntax Completion

Note: This tip has been updated, please see Tab Completion for Vim (Updated) instead.

If you have used my previous tip for automatic syntax completion, you might find that using the <CTRL-X><CTRL-U> keystrokes to perform omni-completion can sometimes get quite frustrating after a while. To help address this annoyance, I wrote a little vim function to use the <tab> button to perform syntax completion instead.

The nice thing about that, is the function does contextual scanning to see if you actually want a <tab> or omni-completion to be performed by scanning the token at immediately before the cursor when the <tab> button is pressed.

Put these lines into your .vimrc:

function! My_TabComplete()
let substr = strpart(getline('.'), col('.'))
let result = match(substr, '\w\+\(\.\w*\)$')
if (result!=-1)
return "\<C-X>\<C-U>"
return "\<tab>"
autocmd FileType java inoremap <tab> <C-R>=My_TabComplete()<CR>

The specific pattern I'm looking for in this case is 'objectOrClass.' or 'objectOrClass.incompleteMethodName', but do feel free to change to fit your own needs. Also, if you are using the keyword replacement keystroke <CTRL-P> rather than vjde, you should modify the script to return '\<C-P>' instead.

If you like reading this, you may also enjoy:


Anonymous said...

Hi Vince,
Thanks for these excellent posts. I was looking at vim plugins for eclipse like eclim but wasn't able to get them working properly, then I came across your tips, they are perfect!!
Just one thing: when using your My_TabComplete() function, it seems to only go to the first part of the 'if' statement and execute the first return statement regardless of the situation. Can you think of any reason why this does not work for me? This is my first time writing/editing functions in the .vimrc file.

I am using instead of this the line:

autocmd FileType java inoremap . .'code for control-x,control-u'

which works fine but I would like to be able to choose whether or not to use the syntax completion as in your implementation.

Unknown said...

Hi Dan,

Glad that the posts had helped you to configure up vim to the way you liked it :)

Sorry about the error, but I've made a mistake with my script in terms of the regular expression matching. I was trying to match a word that has at least 2 letters originally, but somehow it did not work as intended.

Anyway I did modify the regular expression to now be:

let result = match(substr, '\w\+\(\.\w*\)$')

which in this case will only try to complete when you have an 'object.[whatever]', before it attempts to complete. Unfortunately it won't try to complete words automatically, but will complete fields and methods properly.

I'll be on a lookout for other better solutions, and in the meantime, I'll make changes to reflect the changes I've made.

Anonymous said...

thats great, thanks for the help.
I'll try that out later on. Its starting to make a little bit more sense now so I can do more than just copy and paste!

Unknown said...

works better like this:

let substr = strpart(getline('.'), 0, col('.') -1 )

Post a comment