如何在vim中使用ctags进行自动完成

时间:2011-09-28 06:54:13

标签: java android vim

我尝试在vim中开发android项目。但是发现在ctags中查找一个方法有点无聊。有没有办法在vim中使用ctags显示自动完成列表?

感谢

这是我的ctags版本如下:

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

和vim的版本:

Exuberant Ctags 5.8, Copyright (C) 1996-2009 Darren Hiebert
  Compiled: Mar 21 2011, 10:34:51
  Addresses: <dhiebert@users.sourceforge.net>, 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 pkg-vim-maintainers@lists.alioth.debian.org
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

我的.vimrc文件:

   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

3 个答案:

答案 0 :(得分:10)

我想澄清一下:听起来你已经设置了ctags并且想知道如何使用它来自动填充方法名称,是吗?

正如romainl所提到的,<C-n><C-p>正是您所寻找的。它们分别是complete using next matchcomplete using previous match

它们不是vim自动填充的唯一类型,其余部分请参见:he ins-completion(包括拼写和文件名)。我想指出的一个具体完成是<C-x><C-o>(omni-completion),您可能会发现它很有用。

如果你不仅仅关注自动完成:在这个类似的SO问题中,accepted answer by richq一般都有关于ctags的一些好信息。

答案 1 :(得分:1)

我还没有在VIM中尝试过自动填充功能,但发现了一个快速搜索http://vim.wikia.com/wiki/Any_word_completion:“完整”选项控制搜索关键字的位置(包含文件,标记文件,缓冲区和更多)。

答案 2 :(得分:0)

您可以使用[TAB]完成所有操作,并在vimrc中完成此操作。 我们使用[char] [char] [dot] [tab]触发代码完成, 另请参见https://stackoverflow.com/a/46645434/476175,了解如何整合拼写,同义词库和字典。

   :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