I need to make a report about investment everyday. It requires a lot of changes of the sign of figures. (from - to + or from + to -)

Is there a simple way to overload (extend) the ~ command (switch case) with such a toggle function?

1 Answer 1


I would do:

function! ExtendToggle()
  let char = getline(line('.'))[col('.')-1]
  if char == '-'
    return 'r+l'
  elseif char == '+'
    return 'r-l'
    return '~'

nnoremap <expr> ~ ExtendToggle()
  • Great! That's just what I need/want! Thank you very much! I append a SPACE to 'r+' and 'r-' so that it advances afterward like what '~' would do. Commented Mar 1 at 10:22
  • 1
    Thanks for the feedback. I'll adapt the solution according to your remark (replacing <space> by l since for a number of us <space> is the leader key) Commented Mar 1 at 10:42
  • 1
    Note that using <space> should be OK because it is protected by nnoremap. The space will just be a space, not a leader or any other mapping.
    – Kevin
    Commented Mar 1 at 18:58
  • You are right. The <leader> are only used in mapping and the nnoremap is ignoring the mapping so indeed it should be fine (to be sure I have tested) :-) Commented Mar 1 at 19:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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