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?
- 62,054
- 25
- 192
- 271
- 2,118
- 15
- 18
4 Answers
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
Plethora of Plugins
Toggling true/false and similar has been implemented by many plugin authors. Two of them have already been mentioned:
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:
- SwapIt by Michael Brown (#2294, github),
- Increment-activator by Takuya Nishigori (#4817, github),
- CtrlXA by Konfekt (#5600, github),
- Cycle by Zef Houssney (github),
- Cycle by bootleq (github)
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>SpeedDatingDowncycle by bootleq:
map <silent> <Plug>CycleFallbackNext <Plug>SpeedDatingUp map <silent> <Plug>CycleFallbackPrev <Plug>SpeedDatingDownswitch.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.
- 4,740
- 26
- 37
I wrote a plugin (toggle-bool) to toggle the boolean values you can try it. It supports the following boolean values:
true<->falseyes<->noon<->off0<->1
- 196
- 3
-
Oh, It's what I was looking for. But I have a small problem. Can you fix it? https://github.com/sagarrakshe/toggle-bool/issues/1 – Alexander Myshov Oct 22 '15 at 04:57
-
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
- 21
- 1
-
1Welcome 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^Min your mapping, and it is usually a best practice to provide a mode to yourmapcommand, herennoremapwould be more appropriate thannoremap. – statox Nov 30 '19 at 09:18