Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use ctags for autocomplete in vim

Tags:

java

android

vim

I try to develop android project in vim.But find it a bit of boring to look up in ctags for one method.Is there a way to show up a auto-complete list in vim using ctags?

thanks

it's my ctags's version below:

Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
  Compiled: Mar 21 2011, 10:34:51
  Addresses: <[email protected]>, http://ctags.sourceforge.net
  Optional compiled features: +wildcards, +regex

and vim's version:

Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
  Compiled: Mar 21 2011, 10:34:51
  Addresses: <[email protected]>, http://ctags.sourceforge.net
  Optional compiled features: +wildcards, +regex
ccheng@ccheng-desktop:~$ vim --version
VIM - Vi IMproved 7.3 (2010 Aug 15, compiled Mar 24 2011 07:10:07)
Included patches: 1-35
Modified by [email protected]
Compiled by buildd@
Huge version without GUI.  Features included (+) or not (-):
+arabic +autocmd -balloon_eval -browse ++builtin_terms +byte_offset +cindent 
-clientserver -clipboard +cmdline_compl +cmdline_hist +cmdline_info +comments 
+conceal +cryptv +cscope +cursorbind +cursorshape +dialog_con +diff +digraphs 
-dnd -ebcdic +emacs_tags +eval +ex_extra +extra_search +farsi +file_in_path 
+find_in_path +float +folding -footer +fork() +gettext -hangul_input +iconv 
+insert_expand +jumplist +keymap +langmap +libcall +linebreak +lispindent 
+listcmds +localmap -lua +menu +mksession +modify_fname +mouse -mouseshape 
+mouse_dec +mouse_gpm -mouse_jsbterm +mouse_netterm -mouse_sysmouse 
+mouse_xterm +multi_byte +multi_lang -mzscheme +netbeans_intg -osfiletype 
+path_extra -perl +persistent_undo +postscript +printer +profile +python/dyn 
+python3/dyn +quickfix +reltime +rightleft -ruby +scrollbind +signs 
+smartindent -sniff +startuptime +statusline -sun_workshop +syntax +tag_binary 
+tag_old_static -tag_any_white -tcl +terminfo +termresponse +textobjects +title
 -toolbar +user_commands +vertsplit +virtualedit +visual +visualextra +viminfo 
+vreplace +wildignore +wildmenu +windows +writebackup -X11 -xfontset -xim -xsmp
 -xterm_clipboard -xterm_save 
   system vimrc file: "$VIM/vimrc"
     user vimrc file: "$HOME/.vimrc"
      user exrc file: "$HOME/.exrc"
  fall-back for $VIM: "/usr/share/vim"
Compilation: 
gcc -c -I. -Iproto -DHAVE_CONFIG_H     -Wall -g -O2 -D_FORTIFY_SOURCE=1      
Linking: 
gcc   -Wl,--as-needed -o vim       -lm -lncurses -lselinux   -lacl -lgpm

my .vimrc file:

   1 set autochdir
  2 filetype plugin indent on
  3 set nu
  4 set tags=./tags
  5 set path=.,..,../..
  6 set tabstop=4
  7 set hlsearch
  8 set cscopequickfix=s-,c-,d-,i-,t-,e-
  9 set tags=$HOME/gingerbread/frameworks/tags,$HOME/gingerbread/packages/provid    ers/ContactsProvider/tags
 10 
 11 :" Only do this part when compiled with support for autocommands.
 12 :if has("autocmd")
 13 :  autocmd Filetype java setlocal omnifunc=javacomplete#Complete
 14 :endif 
 15 setlocal completefunc=javacomplete#CompleteParamsInfo
like image 780
Frank Cheng Avatar asked Sep 28 '11 06:09

Frank Cheng


People also ask

How do I set tags in Vim?

Select tag from tag list. You can select a particular tag from the tag list after opening the file in the vim editor by using ctags command. Open any source code in vim editor and type ':tselect' to find out the list of tag list of current source code. Here, the same file, abs_num.py is used to check this command.


2 Answers

You can complete everything with [TAB], with this in vimrc. We use [char][char][dot][tab] to trigger tag completion, Also see https://stackoverflow.com/a/46645434/476175 on how to integrate spelling, thesaurus, dictionary.

   :inoremap <Tab>   <C-R>=MoshTabOrComplete()<CR>
    function!               MoshTabOrComplete()
      if col('.') > 3 
        \ && strpart( getline('.'), col('.')-3, 4 ) =~ '^[a-zA-Z]\+[.]' "  prev 2 char are alpha+dot?
        " Tag-completion eg. print.<tab> will complete to printf or println
        return "\<BS>\<C-x>\<C-]>"
      endif
      " if exists("g:MoshThesaurusOmniCompleter") && col('.') > 3 
        " \ && strpart( getline('.'), col('.')-3, 4 ) =~ '^[a-zA-Z]\+[!%?~-]' "  prev 2 char are alpha+punct?
        " Calls MoshThesaurusOmniCompleter 
        " Test: qieut!<tab> will spell correct to quiet, more tests in ./thesaurus-omni.vim
        " return "\<C-x>\<C-o>"
      " endif
      if col('.') > 1 && strpart( getline('.'), col('.')-2, 3 ) =~ '^\w' " prev char is alphabet?
        " Usual omni-completion
        return "\<C-N>"
      endif
      " Insert regular TAB
      return "\<Tab>"
    endfunction
like image 132
mosh Avatar answered Sep 28 '22 00:09

mosh


I want to clarify: it sounds like you have ctags set up already and would like to know how to use it to autocomplete method names, is that right?

As romainl mentions, <C-n> and <C-p> are what you're looking for then. They're complete using next match and complete using previous match, respectively.

They are not the only types of autocompletion vim has though, see :he ins-completion for the rest (includes spelling and file names). One specific completion I'd like to point out is <C-x><C-o> (omni-completion) which you might find useful.

In case you weren't only concerned with autocompletion: in this similar SO question, the accepted answer by richq has some good info on ctags in general.

like image 23
Nick Knowlson Avatar answered Sep 27 '22 23:09

Nick Knowlson