0

In vim, f<space> and F<space> move the cursor forward to the next space and backward to the previous space, respectively.

Is there a command in Sublime Text 3 that does the same? If so, please demonstrate its use in a key binding.

kkurian
  • 3,844
  • 3
  • 30
  • 49

2 Answers2

2

Ctrl + Left & Ctrl + Right moves the caret by word boundaries, including spaces.

You can alter the characters that dictate word boundaries in your User Settings by editing the word_separators value.


Sublime Text also has Vintage Mode, which emulates many of VIM's functionalities including:
l, h, j, k, W, w, e, E, b, B, alt+w, alt+W, $, ^, %, 0, G, gg, f, F, t, T, ^f, ^b, H, M, L

Community
  • 1
  • 1
Enteleform
  • 3,753
  • 1
  • 15
  • 30
  • As you point out, `ctrl+left/right` doesn't do exactly what `f/F` does. Is it possible to specify `word_separators` for just one key binding? If so, per my question, could you demonstrate how to do this? – kkurian May 31 '16 at 00:06
  • @kkurian: You cannot, `word_separators` can only be set on a [ global | syntax | project | view ] context. Did you try Vintage mode? I'm not a Vim user, but it says it has the `f` & `F` functionalities. As well as: `l, h, j, k, W, w, e, E, b, B, alt+w, alt+W, $, ^, %, 0, G, gg, f, F, t, T, ^f, ^b, H, M, and L` – Enteleform May 31 '16 at 00:11
  • Thank you for pointing out Vintage but I don't want to get into vim-style editing where I've got to switch between modes. I use Sublime for a reason. :-) – kkurian May 31 '16 at 00:16
2

Save the following code to:
/Packages/MoveToSpace/MoveToSpace.py

import sublime, sublime_plugin

class MoveToSpaceCommand( sublime_plugin.TextCommand ):
    def run( self, edit, mode ):

        view = self.view
        selections = self.view.sel()

        if len( selections ) == 0:
            return

        newSelections = []

        for selection in selections:

            spacePoint = None

            if mode.lower() == "forward":

                spacePoint = self.view.find( "[^ ] " , selection.end() ).a

                if spacePoint != -1:
                    newSelections.append( sublime.Region( spacePoint + 1, spacePoint + 1 ) )

            elif mode.lower() == "backward":

                spaceRegions = self.view.find_all( " [^ ]")
                spaceRegion_Count = len( spaceRegions )

                for index in range( 0, spaceRegion_Count ):
                    if spaceRegions[ index ].b < selection.begin():
                        spacePoint = spaceRegions[ index ].a
                    elif spaceRegions[ index ].b >= selection.begin() \
                    and  spacePoint != None:
                        newSelections.append( sublime.Region( spacePoint + 1, spacePoint + 1 ) )
                        break

        if len( newSelections ) > 0:
            view.sel().clear()
            view.sel().add_all( newSelections )

Save the following code to:
/Packages/MoveToSpace/Default.sublime-keymap

[

    {
        "keys":    [ "ctrl+shift+=" ],
        "command": "move_to_space",
        "args":    { "mode": "forward" },
    },

    {
        "keys":    [ "ctrl+shift+-" ],
        "command": "move_to_space",
        "args":    { "mode": "backward" },
    },

]

Included key-bindings are:

  • Ctrl + Shift + Plus to move to the next space
  • Ctrl + Shift + Minus to move to the previous space

You can change the key-bindings to suit your preference.

Enteleform
  • 3,753
  • 1
  • 15
  • 30
  • ST2? It simply doesn't work, e.g., `File "./MoveToSpace.py", line 20, in run AttributeError: 'NoneType' object has no attribute 'a'`. For another example: `File "./MoveToSpace.py", line 38, in run view.sel().add_all(newSelections) Boost.Python.ArgumentError: Python argument types in RegionSet.add_all(RegionSet, list) did not match C++ signature: add_all(SelectionSet {lvalue}, SelectionSet) ` – kkurian Jun 14 '16 at 17:02
  • Also note: ST2 rejects trailing commas before closing braces/brackets. There are three of them in the `Default.sublime-keymap` above. 3 < 6 so I can't make the edit -- SO insists edits be at least 6 chars. – kkurian Jun 14 '16 at 17:06
  • I wrote + tested on ST3. Idk the API differences but you could probably adapt the code pretty easily. Any reason you're still on ST2? It's [**pretty much deprecated**](https://forum.sublimetext.com/t/sublime-text-2-not-developed-anymore/18680/3) at this point... – Enteleform Jun 15 '16 at 01:50
  • Good point. :) Been heads down I guess. Thanks for the nudge. – kkurian Jun 15 '16 at 03:05
  • 1
    Your solution works for me now that I'm on ST3. Updated question to reference ST3 instead of ST2. – kkurian Jun 15 '16 at 03:40
  • 1
    This is terrific! Thanks! A little note for users: it doesn't consider a newline a space – dwn Jul 29 '20 at 02:56