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>"
else
return "\<tab>"
endfunction
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:
- Vim Tips for Java #1: Build Java files with Ant Automatically
- Vim Tips for Java #2: Using exuberant-ctags
- Vim Tips for Java #3: Use Omni-Completion (or Intellisense) for Automatic Syntax Completion
- Vim Tips for Java #5: Folding Code Blocks to Prevent Visual Blindness
- Vim Tips for Java #6: Auto-Bracketing Within Vim
4 comments:
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.
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.
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!
works better like this:
let substr = strpart(getline('.'), 0, col('.') -1 )
Post a Comment