Sublime Forum

Why you might want to give your [ctrl+left] and [ctrl+right] these alternative behaviors

#1

instead of the current default behavior, you can make it so they jump from word selections to word selections, as in:
http://img02.imgland.net/pQIW0n.gif

why might you prefer this alternative behavior?

  1. most often you just want to navigate to some word and edit it. in that kind of case, this remapping combines movement and selection in one package, saves you the need to perform an extra act of selection. once you’ve arrived your destination you can proceed to edit right away
  2. this alternative behavior is also more intuitively predictable. watch the video a couple of times and you can form pretty confident “gut” predictions about what will happen when you execute another act of [ctrl+left/right]. The current default behavior of ctrl+left/right is not as predictable. You never feel very sure where the caret will land —— will it stop at the left of that quotation mark or the right?
  3. as you navigate your way forward you also get a lot of extra visual information along the way you don’t get with the default behavior. E.g. the length of the word, and also information from sublime’s automatic highlighting of duplicates.
  4. even if your plan is not to eventually edit/delete/copy some word, you’re only one extra press of [left] or [right] away (which your right hand is already pressing) from cancelling the selection and get off at its left/right end.

if you wan to give it a try, just remap your ctrl+left/right (or any other key combo you prefer) to these two macros

(edit: macro order was wrong as legosublime pointed out; now corrected. please also see below frou’s post below for quickly trying out!)

macro 1 (for ctrl+right):

[
	{
		"args":
		{
			"by": "stops",
			"forward": true,
			"word_begin": true
		},
		"command": "move"
	},
	{
		"args":
		{
			"to": "word"
		},
		"command": "expand_selection"
	}
]

macro 1 (for ctrl+left):

[
	{
		"args":
		{
			"by": "stops",
			"forward": false,
			"word_end": true
		},
		"command": "move"
	},
	{
		"args":
		{
			"to": "word"
		},
		"command": "expand_selection"
	},
]
5 Likes

#2

Awesome. But isn’t it the other way round? Macro 1 = forward: false and Macro 2: forward: true. Great idea, thank you.

1 Like

#3

This is interesting. Though, it’s rather distracting that all the other instances of the words being moved through are constantly flashing with the hollow highlight, even though we’re not interested in “finding” them.

To save others a little time to try it out:

In your User keybindings

{"keys": ["ctrl+left"], "command": "run_macro_file", "args": {"file": "Packages/User/BlobLeft.sublime-macro"}},
{"keys": ["ctrl+right"], "command": "run_macro_file", "args": {"file": "Packages/User/BlobRight.sublime-macro"}},

BlobLeft.sublime-macro

[
    {
        "args":
        {
            "by": "stops",
            "forward": false,
            "word_end": true
        },
        "command": "move"
    },
    {
        "args":
        {
            "to": "word"
        },
        "command": "expand_selection"
    },
]

BlobRight.sublime-macro

[
    {
        "args":
        {
            "by": "stops",
            "forward": true,
            "word_begin": true
        },
        "command": "move"
    },
    {
        "args":
        {
            "to": "word"
        },
        "command": "expand_selection"
    }
]
2 Likes

#4

Nice, I’ll give it a try. How about one for Alt+left/right? :slight_smile:

0 Likes

#5

i mapped those too! works like this

http://img.imgland.net/nUMwJEP.gif

macros are below. however one caveat: i had to invoke a command named “reverse_select” (move the caret from one end of a selection block to the other). (long story short: expand_selection command doesn’t take ‘subword’ as an argument, so the macro for ctrl+left/right can’t be straightforwardly adapted for alt+left/right).
as far as i remember “reverse_select” isn’t a built-in sublime command but comes from a package, (possibly Select Until ?) but googling the command name didn’t turn up anything. It may be possible to rewrite the macro so it doesn’t rely on that command, but i haven’t yet figured out how…

macro for alt+left

[
	{
		"args":
		{
			"by": "subword_ends",
			"forward": false
		},
		"command": "move"
	},
	{
		"args":
		{
			"by": "subwords",
			"extend": true,
			"forward": false
		},
		"command": "move"
	},
	{
		"args": null,
		"command": "reverse_select"
	}
]

macro for alt+right

[
	{
		"args":
		{
			"by": "subword_ends",
			"forward": true
		},
		"command": "move"
	},
	{
		"args":
		{
			"by": "subwords",
			"extend": true,
			"forward": false
		},
		"command": "move"
	},
	{
		"args": null,
		"command": "reverse_select"
	}
]
0 Likes

#6

For those who want it without installing an entire package, here is an implementation of a “reverse_select” command. It flips the cursor to the other side of each individual selection

Packages/User/reverse_select.py

import sublime
import sublime_plugin


class ReverseSelectCommand(sublime_plugin.TextCommand):
  def run(self, edit):
    sel = [sublime.Region(r.b, r.a) for r in self.view.sel()]
    self.view.sel().clear()
    self.view.sel().add_all(sel)
0 Likes