The following script I have will allow me to detect whether if I really wanted a closing bracket/brace or whether if has already been inserted previously by the auto-bracketing script, and hence I should skip it, by checking what the current character on the cursor right is. So here's what's needed to be added to your
.vimrc
in order for it to work:
autocmd Filetype java imap ( ()<left>
function! My_BracketComplete()
let char = strpart(getline('.'), col('.')-1, 1)
if (char == ")")
return "\<Right>"
else
return ")"
endif
endfunction
autocmd FileType java inoremap ) <C-R>=My_BracketComplete()<CR>
autocmd Filetype java imap { {}<left><cr><cr><up><tab>
function! My_BraceComplete()
let char = strpart(getline('.'), col('.')-1, 1)
if (char == ")")
return "\<Right>"
else
return "}"
endif
endfunction
autocmd FileType java inoremap } <C-R>=My_BraceComplete()<CR>
While it is neither the cleanest nor the most elegant way, but it replicates faithfully with what Netbeans does. Unfortunately, this solution will still not work for quotes and double-quotes because of the nature of the
'imap'
command, which will make vim go into a non-terminating loop. So if some of you have thought of a better way to do this, do share it with me by posting your solution on my blog.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 #4: Using 'tab' for Syntax Completion
Vim Tips for Java #5: Folding Code Blocks to Prevent Visual Blindness
4 comments:
thanks very much! I've been looking for it, too!
vim.org already has a script that solves all these issues (including quotes, and all the other brackets). Its located at:
http://www.vim.org/scripts/script.php?script_id=1849
Cheers for that, I'll go look it up myself as well :)
amazing!
Post a Comment