16

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?

Martin Tournoij
  • 62,054
  • 25
  • 192
  • 271
Alexander Myshov
  • 2,118
  • 15
  • 18

4 Answers4

13

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.

kenorb
  • 18,433
  • 18
  • 72
  • 134
VanLaser
  • 9,700
  • 2
  • 24
  • 34
  • 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
11

Plethora of Plugins

Toggling true/false and similar has been implemented by many plugin authors. 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).

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.

Fallback

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.

Hotschke
  • 4,740
  • 26
  • 37
8

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
Sagar Rakshe
  • 196
  • 3
2

change the mapping to what you want to use

the undo is in case you use it on top of a space, since ciw will edit the space instead of the word (and there's no easy way to get to the right word reliably)

function SwapBool ()
  let s:w = expand("<cword>")
  if s:w == "false"
    normal ciwtrue
    if expand("<cword>") != "true"
      normal u
    endif
  elseif s:w == "true"
    normal ciwfalse
    if expand("<cword>") != "false"
      normal u
    endif
  endif
endfunction
noremap <C-F> :call SwapBool()^M

also note the ^M needs to be <C-V><Enter>, not literally ^M

Efi
  • 21
  • 1
  • 1
    Welcome to this site! Note that it would be interesting to add a condition which would make the function like the built-in <C-a> and <C-x> when the current word is not a boolean, this way OP would really be able to use these mappings. Also you can use <CR> instead of ^M in your mapping, and it is usually a best practice to provide a mode to your map command, here nnoremap would be more appropriate than noremap. – statox Nov 30 '19 at 09:18