This is a noob question, so basically in vim I want to run ctags -R when I am writing to my source or header files (*.h or *.cpp) so I tried the following:

autocmd BufWritePost *.cpp :!ctags -R
autocmd BufWritePost *.h :!ctags -R

It seems to work, but every time I save, it wants a confirmation, so I have to press enter, like this:

"coap/coap_pdu_ll.h" 30L, 793C written
:!ctags R

Press ENTER or type command to continue

Is there a way to get rid of the confirmation?


When you execute an external command with :!, Vim doesn't know what the external command might have written to the terminal, so it waits for you to confirm that you are ready to continue.

To avoid that, you can use the system() function like this:

autocmd BufWritePost *.cpp call system("ctags -R")
autocmd BufWritePost *.h   call system("ctags -R")

You could also simplify that to one command:

autocmd BufWritePost *.cpp,*.h call system("ctags -R")

A problem with calling system() is that you lose any error messages it might produce. That can be solved by using echo instead of call, like this:

autocmd BufWritePost *.cpp,*.h echo system("ctags -R")

However, that echoes an empty line to the display when the command succeeds, causing another "Press ENTER or type command to continue" prompt. The simplest solution for that is to increase the height of the command line:

:set cmdheight=2

Another, more complicated solution would be to echo a message only if there is one. Then you avoid the prompt without losing any screen space.

autocmd BufWritePost *.cpp,*.h let msg=system('ctags -R') | if msg!="" | echo msg | endif

Since it looks like you are using C++ why not just rebuild your tags when you compile? Just add a rule to your makefile.

Other options:

  • Really good suggestions, In fact I am trying gutentags right now. But, I am mostly a minimalist, to me a line in .vimrc file is better then a plugin, unless the plugin offers me something that a line cannot do. I think that plugin is required for larger code bases. Sep 15 '15 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.