9

I think it would be useful to have possibility to swap true and false values in the code rapidly, with combination ctrl+a or ctrl+x. Is there some plugin for that or something else?

5

I wrote a plugin (toggle-bool) to toggle the boolean values you can try it. It supports the following boolean values:

  • true <-> false
  • yes <-> no
  • on <-> off
  • 0 <-> 1
12

There is switch.vim, and it's customizable.

The main entry point of the plugin is a single command, :Switch. When the command is executed, the plugin looks for one of a few specific patterns under the cursor and performs a substition depending on the pattern.

For example if the cursor is on the true in the following code:

flag = true

then upon executing :Switch, the true will turn into false.

  • You're welcome! I have this crazy idea of a 'vim-dwim' plugin that acts differently based on syntax under cursor (e.g. when hitting Enter), and I think I'll heavily inspire myself from this plugin. If I ever get to start making it, that is. – VanLaser Oct 21 '15 at 18:42
3

Plethora of Plugins

Toggling true/false and similar has been implemented by many plugins . Two of them have already been mentioned:

  • switch.vim by Andrew Radev (#4172, github) and
  • toggle-bool by Sagar Rakshe (github).

Others are

  • toggle by Timo Teifel (#895),
  • monday by Stefan Karlsson (#1046),
  • toggle_words by Vincent Wang (#1676),
  • toggle_word by Alexandru Ungur (#1748),
  • incbool.vim by Dmitry Petrov (#4361, github),
  • nextval by Michael Arlt (#4536, github),
  • voggle by Jasper Smienk (github).

Just in case you want to increase numbers according to a custom series, e.g. the Fibonacci series, check out nexus by Barry Arthur (github).

Plugins Supercharging Ctrl-A/X

However, not all of them follow by default the idea to extend Ctrl-A/X but define a new command and/or a new mapping (incl. switch.vim and toggle-bool). I want to highlight those plugins which 'supercharge' Ctrl-A/X out of the box and, therefore, provide by default what the OP has asked:

A related plugin which also extends Ctrl-A/X is speeddating by Tim Pope (#2120, github) which allows you to increment dates.

Some plugins allow to combine a second Ctrl-A/X incrementor plugin as a fallback, e.g.

  • SwapIt by Michael Brown:

    nmap <Plug>SwapItFallbackIncrement <Plug>SpeedDatingUp
    nmap <Plug>SwapItFallbackDecrement <Plug>SpeedDatingDown
    vmap <Plug>SwapItFallbackIncrement <Plug>SpeedDatingUp
    vmap <Plug>SwapItFallbackDecrement <Plug>SpeedDatingDown
    
  • cycle by bootleq:

    map <silent> <Plug>CycleFallbackNext <Plug>SpeedDatingUp
    map <silent> <Plug>CycleFallbackPrev <Plug>SpeedDatingDown
    
  • switch.vim by Andrew Radev:

    nnoremap <c-a> :if !switch#Switch()<bar>call speeddating#increment()<bar>endif<cr>
    nnoremap <c-x> :if !switch#Switch({'reverse': 1})<bar>call speeddating#decrement()<bar>endif<cr>
    

Note
If you use the seek behavior of the builtin Ctrl-A/X (find next possible value in line to increment), supercharging them can interfere.

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.