13

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: <dhiebert@users.sourceforge.net>, 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: <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

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
Frank Cheng
  • 5,928
  • 9
  • 52
  • 80
  • Not to start an editor flame war, but is there any reason why you're not using an IDE like Eclipse? Don't get me wrong, I'm a die-hard Vim fan, but after I tried using Eclipse for Java, I'm of the firm opinion that a good IDE beats a text editor, any day. This should be especially true given the project generators and other Android-specific tools you get when you use the Google plugin for Eclipse. – exclipy Sep 28 '11 at 07:03
  • 7
    yeah, Eclipse speeds up, but managing an entire project with a simple editor forces you to know almost everything in the project itself. I used to work on large legacy projects in C/C++, nothing is better than a good makefile and a good editor (VI or EMACS or whatever you like). If you can do it in notepad/vi, you can do it with everything. Of course it takes a little more time. – BigMike Sep 28 '11 at 07:31
  • 1
    Thanks for your advice, I will think about it.I use eclipse before, i fed up with its low efficient and when i modify android source code, i always change between eclipse and terminal, it`s not feeling good. – Frank Cheng Sep 28 '11 at 07:34
  • With languages like Java (and C#) I've found an IDE to be well worth it. You may be able to get an extension for Eclipse that lets you use Vim inside it (I've used viEmu for Visual Studio and it was very good, no experience on Eclipse plugins though). – Nick Knowlson Sep 29 '11 at 21:27
  • In my experience, the plugins to allow VIM-like editing in the Eclipse only provides simple VIM functionality. If you're a more seasoned VIM user, they just lead to frustration. Unless of course I'm missing something new...out – eugenevd Mar 04 '14 at 09:13
  • `:h 'complete'` was useful for me - I'd configured this before I'd got tags setup in haskell and needed to change it to include `t` again – JonnyRaa Feb 16 '18 at 15:34

3 Answers3

13

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.

Community
  • 1
  • 1
Nick Knowlson
  • 7,185
  • 6
  • 47
  • 63
  • thank you. But when i press C-n, it won`t show up the method in a class. So i doubt if it has effect in c/c++,not in java. – Frank Cheng Sep 29 '11 at 00:51
  • 1
    That's interesting that `` isn't working for you. Do `` and `` also not work? It sounds like there something missing in how ctags was set up. I'm not sure what your second sentence said, can you rephrase that? – Nick Knowlson Sep 29 '11 at 21:21
  • 1
    The output of ctags --version may be relevant, could you edit the question with that? Also, are you using ctags in a way similar to: http://blog.vinceliu.com/2007/08/vim-tips-for-java-2-using-exuberant.html ? – Nick Knowlson Sep 29 '11 at 21:30
  • , works very well. I doubt if because it`s java so ctags`s autocomplete won`t work out. – Frank Cheng Sep 30 '11 at 01:16
  • Ah sorry, I was trying to be brief but I wasn't clear enough I think. I meant `` and ``, my apologies. – Nick Knowlson Sep 30 '11 at 23:50
  • `I doubt if because it`s java so ctags`s autocomplete won`t work out.` I still don't understand that sentence, I'm afraid. Are you saying you don't think ctags will work with java? Java is on ctags' supported languages page: http://ctags.sourceforge.net/languages.html – Nick Knowlson Sep 30 '11 at 23:52
  • Okay, first troubleshooting step: have you run `ctags -R`? – Nick Knowlson Sep 30 '11 at 23:59
  • yes.and when i press ctrl ], it go into the class under cursor – Frank Cheng Oct 01 '11 at 02:02
  • And could you also answer whether `` and `` work please? – Nick Knowlson Oct 03 '11 at 17:01
1

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
mosh
  • 1,402
  • 15
  • 16
1

I haven't tried autocomplete in VIM but a quick search found http://vim.wikia.com/wiki/Any_word_completion: The 'complete' option controls where the keywords are searched (include files, tag files, buffers, and more).

Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
  • 1
    Yes, `` and `` also scan tag files by default. `` scans tag files only. This might reduce the number of propositions. – romainl Sep 28 '11 at 09:21
  • @romainl You should post `` as a full answer, since that really helped me. – Ainar-G Oct 25 '18 at 16:03