Sublime Forum

NeoVintageous: Toggle boolean values

#1

In NeoVintageous, I would like to be able to swap between ‘true’ and ‘false’ with a single command, rather having to do ciw and then type the new value each time.

There are many plugins for vim that will do this, but I don’t think it’s possible to install extra plugins with Vintageous. So is my best route to look for a native sublime text plugin?

0 Likes

#2

My first thought is to create a mapping but I can’t think of a way to get it to toggle. Two separate ones is easy enough:

nnoremap <leader>t ciwtrue<Esc>
nnoremap <leader>f ciwfalse<Esc>

You could create a Sublime plugin then create a mapping to it, for example let’s say you create a plugin:

a quick example

import sublime_plugin


class TrueFalseToggle(sublime_plugin.TextCommand):

    def run(self, edit):
        for sel in self.view.sel():
            word = self.view.word(sel)
            word_str = self.view.substr(word)

            if word_str in ('true', 'false'):
                replacement = 'true' if word_str == 'false' else 'false'
                self.view.replace(edit, word, replacement)

Then map it:

nnoremap <leader>t :TrueFalseToggle<CR>
1 Like

#3

I’m really grateful for your help here.

Reading your code example made me think that surely this must already exist for ST. The ToggleWords plugin allows toggling between words inside user-defined arrays, like this:

 true -> false
  yes -> no
   on -> off
    0 -> 1
 left -> right
  top -> bottom
   up -> down
width -> height

This looks awesome. Their out-of-the-box shortcut is Ctrl+Alt+x, which is fine, but using a vim leader would make more sense inside the vim world.

The leader I use is <space>.

I tried modifying their keymapping as follows:

{
        "keys": [" ", "t"],
        "command": "toggle_word",
}

This works, but it works in all modes, including insert mode, which isn’t acceptable.

I therefore tried mapping it inside .neovintageousrc, as you suggested, but I can’t find a way to make it work. This is what I’ve tried that doesn’t work:

nnoremap <leader>t :toggle_word<CR>

When I tell the console to log all commands, it just reports

command: _nv_feed_key {"key": "<space>"}
command: _nv_feed_key {"key": "t"}

but it doesn’t actually run the toggle_word command. (Using Ctrl+Alt+X does run the command.)

0 Likes

#4

If NeoVintageous uses the command_mode setting, then the following should work:

[
  { "keys": [" ", "t"],
    "command": "toggle_word",
    "context": [
      { "key": "setting.command_mode" },
    ],
  },
]
1 Like

#5

Create a mapping:

noremap <space>t :ToggleWord<CR>

Using a keymap will cause conflicts because after you press <space> Sublime waits for the next key bypassing NeoVintageous.

To map to Sublime Text commands you need format the command name as MixedCase. The
command name is converted to snake_case before being executed by Sublime Text
and basic arguments in the form “key=value” are accepted, for example: >

nnoremap <leader>i :GotoSymbolInProject<CR>
nnoremap <leader>f :ShowOverlay overlay=goto text=@<CR>
nnoremap <leader>t :ToggleWord<CR>

See :help nv for details on mapping support.

1 Like

#6

noremap <space>t :ToggleWord<CR>

Yes, that does the trick perfectly.

To map to Sublime Text commands you need format the command name as MixedCase. The
command name is converted to snake_case before being executed by Sublime Text

I’m glad I stopped trying random variations on “toggle_word” because I would never have guessed it needs to be given as “ToggleWord”!

0 Likes

#7

Thank you. That works perfectly too.

0 Likes