2

I have the following mapping in my project:

nnoremap <F7> :execute '!gtags'<cr> :cscope kill -1<cr> :cscope add CTAGS<cr>

Under certain conditions I want to be able to execute this mapping from a script, i.e.

if (mycondition)
  execute 'normal <F7>'
endif

The code above didn't work.

Does anybody know if it is possible to do something like that?

  • In my opinion this would be better off in a command. command! RerfreshTags !gtags<bar>cscope kill -1<bar>cscope add GTAGS. Then map <f7> to this command and execute your the command directly in your function. – Peter Rincker Sep 25 '15 at 1:03
  • Could <ESC><F7> work? – sensorario Sep 26 '15 at 11:30
  • @PeterRincker I agree, this would be better off as a command. I was just not familiar with he syntax of how to define one. Well, I will have to learn it. Thanks for the suggestion. – flashburn Sep 26 '15 at 16:19
2

A workaround may be put the code in a function:

function! CallF7()
    execute '!gtags'<cr> :cscope kill -1<cr> :cscope add CTAGS<cr>
endfunction
nnoremap <F7> :call CallF7()

So you can use:

if (mycondition)
  call CallF7()
endif
2

You have to insert the escape code corresponding to F7.

To do so in insert mode on my system, I used Ctrl-vF7. It inserted ^[[18~ and the script worked.

  • 4
    Or use double quotes and escape. e.g. execute "normal \<f7>". Or even better yet just use :normal. e.g. normal <f7> – Peter Rincker Sep 24 '15 at 22:07
  • @PeterRincker The double quoting doesnt seems to work on my system Debian + xterm + tmux + vim 7.4... maybe it comes from my terminal emulator? – statox Sep 24 '15 at 22:09
  • @statox For some reason your suggestion didn't work. When I executed Ctrl-F7 it inserted <F7> – flashburn Sep 24 '15 at 22:12
  • 1
    To understand the double quote escaping rules I suggest reading :h expr-quote. An except: \<xxx> Special key named "xxx". e.g. "\<C-W>" for CTRL-W. – Peter Rincker Sep 25 '15 at 0:51
  • 1
    Single quotes don't do any expansion, double quotes do. See :h expr-string vs the immediately following :h literal-string. Most notably, backslashes don't mean anything special in single-quote strings. But then this means that all of the escapes don't do anything either; use double quotes to get magic things to happen. (Single quoted strings are great for regexes, for example.) – dash-tom-bang Feb 22 '16 at 18:16

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.