2

Is it possible in vim to do a conditional map, in normal mode? I've seen it for insert mode. I want to remap gq, depending on the outcome of a function. Something like:

nnoremap gq if(g:set_formatprg()) | gq | else | = | endif

Notice that g:set_formatprg() will not always have the same value, so it cannot be replaced by

if(!g:set_formatprg()) | nnoremap gq = | endif
Filburt
  • 17,626
  • 12
  • 64
  • 115
chtenb
  • 14,924
  • 14
  • 78
  • 116

1 Answers1

4

An expression map makes it easy

nnoremap <expr> gq g:set_formatprg() ? 'gq' : '='

For more help see

:h map-expression
Peter Rincker
  • 43,539
  • 9
  • 74
  • 101