Comments (6)
That's a good way to handle one-off patterns. The problem is the more complicated the pattern gets, it gets harder to build something compatible on top of them :D.
One thing I'll note is that I think your search
only works if ignorecase
is on. If you'd like to make sure other people's configs work with this function, maybe search('\ctrue\|false', ...
would be better. The \c
modifier makes the entire pattern after the \c
ignore case. You might also want to check if the result of search
is > 0
, so the function doesn't activate the Switch
command if there's no true or false on the line.
I implemented a function to do this for Python. I dunno if it can be used to upgrade the plugin, but it could be useful for someone else, so maybe include it as an example in the documentation?
I agree it might be useful -- could you add it to the wiki? https://github.com/AndrewRadev/switch.vim/wiki
I've linked to the wiki in the docs under "Customization", although I don't know how visible it is.
from switch.vim.
The problem seems to be if call search
. The code search(...)
is an expression, while call search(...)
is a command. In the command-line, you need to execute commands, which is why you need :call
. But the if-clause expects an expression for its condition.
You can try if search('\c\<true\>\|\<false\>', '', line('.')) > 0
and that should work. Bear in mind though, that the execute
calls won't work, because they'll throw errors if there's no match on the current line. And if they do work, the first substitution replaces 1 with 0 and the second replacement replaces it back to 1 :). Try this:
function! ToggleBool()
let save_pos = getpos(".")
normal! 0
if search('\c\<true\>\|\<false\>', '', line('.')) > 0
Switch
elseif search('\<1\>', '', line('.')) > 0
" decrement 1 -> 0
execute "normal! \<c-x>"
elseif search('\<0\>', '', line('.')) > 0
" increment 0 -> 1
execute "normal! \<c-a>"
endif
call setpos(".", save_pos)
endfunction
The mappings <c-x>
and <c-a>
decrement or increment numbers, so they're an easy way to work locally on 0 or 1. You need execute "normal! .."
with double quotes in particular -- take a look at these two articles for an explanation why:
- https://learnvimscriptthehardway.stevelosh.com/chapters/29.html
- https://learnvimscriptthehardway.stevelosh.com/chapters/30.html
from switch.vim.
Awesome! Merci man - it works perfectly 😎 gerazov/vim-toggle-bool@285ff87
from switch.vim.
Thanks for the tip!
I implemented your recommendation and tried to include also 0 and 1 as they do the bool job in some languages. Switch doesn't switch them so I'm doing a search and replace which works in Vim but not inside my function. Can you take a look?
function! ToggleBool()
let save_pos = getpos(".")
normal 0
if call search('\c\<true\>\|\<false\>', '', line('.')) > 0
execute 'Switch'
else
execute 's/\<1\>/0/'
execute 's/\<0\>/1/'
endif
call setpos(".", save_pos)
endfunction
from switch.vim.
Awesome :). I'll close this issue for now, but as I said, feel free to add a link to your plugin in the wiki 👍
from switch.vim.
I just added a page 😉
from switch.vim.
Related Issues (20)
- Switch.vim pollutes search history HOT 2
- help with a bash switch? HOT 6
- Add case insesitivity HOT 2
- Does not working eruby_tag_type in eruby filetype. HOT 4
- [wiki] Anyone know how to write a substitution for React props? HOT 3
- Invert logical expression, using De Morgan Law HOT 1
- "." repeats always forward, even if the last used command was "SwitchReverse" HOT 2
- Undefined variable: g:switch_builtins and invalid expression error on startup when opening a Ruby file and loading the plugin through NeoBundle HOT 2
- Enable js toggles for typescript, typescript.tsx filetypes HOT 2
- switch#NormalizedCase and word boundaries HOT 3
- increase/decrease number HOT 2
- Switching all the flags at once HOT 2
- Possible to call normal commands in switch definition? HOT 10
- Introduce a way to set priority/precedence? HOT 9
- Idea: Multiple switch alternatives HOT 3
- Comparison to swapit HOT 2
- can you please add a configuration example for lua HOT 2
- NormalizedCaseWords does not work for capitalized words HOT 2
- Patch to allow reverse action (and count) in lambda substitutions HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from switch.vim.