Sublime Forum

Word-selection behavior

#1

When double-clicking to select a word, ST2 selects that word and nothing else. Word processors will often, like Word, also select the space following the selected word, so if you, say, delete the selected word, the extra space goes along with it. Is there a way to make that the default word-selection behavior for ST2? i.e., so that on double-click, both the word and the space after it are selected?

thanks,
D

0 Likes

#2

Not possible?

0 Likes

#3

one more bump on this one, please. Any ideas?

0 Likes

#4

Someone might correct me, but I shall assume that there is not a simple setting that would change the double-click behaviour.

It would be possible to create a (Python) TextCommand to do this, and assign it to a keyboard combination. (I’m not sure if it would be possible to achieve with mouse buttons (mousemap)?)

The TextCommand could run the command assigned to ctrl-D - “find_under_expand”. But you would probably need to examine the character following this, to ensure that it is a space. Then you could run the equivalent of Shift-Right:

{ "keys": "shift+right"], "command": "move", "args": {"by": "characters", "forward": true, "extend": true} },

But someone more knowledgeable might propose an easier solution :smile:

0 Likes

#5

OK, thanks for the idea, abigsonsw. It’s a direction for me to start exploring. I’m new to most of this, so may be too early for me to figure this out, but seems that it will eventually be possible.

thanks,
D

0 Likes

#6

[quote=“dutch”]OK, thanks for the idea, abigsonsw. It’s a direction for me to start exploring. I’m new to most of this, so may be too early for me to figure this out, but seems that it will eventually be possible.

thanks,
D[/quote]

In the meantime, double-click the word and press Shift+Right arrow to include the space :smile:

0 Likes

#7

I’m new to this as well :smile: but the following seems to be working for me. Edit your Preferences> Key Bindings - User to add the line:

{ "keys": "ctrl+alt+x"], "command": "word_space" },

but first check that ‘ctrl-alt-x’ isn’t already in use, or choose a different key combination.

Then save the following code as ‘wordspace.py’ in your Packages\User folder. (It doesn’t have to be named ‘wordspace’.)

[code]import sublime, sublime_plugin

class WordSpaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
sels = self.view.sel()
if len(sels) != 1:
return # don’t use if multi-select
curr_sel = sels[0]
the_word = self.view.word(curr_sel.end())
if the_word.empty():
return
the_char = sublime.Region(the_word.end(), the_word.end()+1)
if self.view.substr(the_char) == ’ ':
the_word = the_word.cover(the_char)
self.view.sel().add(the_word)[/code]
It should highlight the current word when pressing Ctrl-Alt-X, but it will extend it to the include the following space, if there is one.

It’s not bullet-proof - i.e. limited error handing - but it seems to be working :laughing:

0 Likes

#8

This seams to work

…/Packages/User/Default.sublime-mousemap

{ "button": "button1", "count": 2, "press_command": "drag_select", "press_args": {"by": "words"}, "command": "select_word_include_white_space" }, { "button": "button1", "count": 2, "modifiers": "ctrl"], "press_command": "drag_select", "press_args": {"by": "words", "additive": true}, "command": "select_word_include_white_space" }

…/Pacakges/User/select_word_include_white_space.py

[code]import sublime_plugin, sublime

class SelectWordIncludeWhiteSpaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
for region in self.view.sel():
if region.empty():
continue
if self.view.substr(sublime.Region(region.end(), region.end()+1) ) == ’ ':
self.view.sel().add(sublime.Region(region.begin(), region.end()+1))[/code]

0 Likes

#9

Thanks, all. I will experiment with these fixes. Much appreciated.

0 Likes