I’m sorry if this was already asked, but i don’t even know what words to use in the title
What i’d need is to alter the behaviour of the “CTRL+arrow” (or SHIFT+CTRL+arrow), which is movement or selection by “whole words”.
My use case is with C and C++ files:
I’d like that the selections stops at each parenthesis, instead of going further to the end of the word next to it.
In general, i would like to add characters (like “(” , “)” …) , or strings (like “//”) so that ST stops at them.
I don’t even know if it is a global setting or a specific syntax one, and as i said, i can’t find even the right term to look for it.
Globally used word boundaries are configured via Preferences.sublime-settings, but can be overridden by syntax specific settings (e.g. C++.sublime-settings).
The following code snippet contains global default word and subword boundaries.
// Characters that are considered to separate words
"word_separators": "./\\()\"'-:,.;<>~!@#$%^&*|+=[]{}`~?",
// Characters that are considered to separate sub-words
"sub_word_separators": "_",
Note: For subword navigation to properly work (at least on Windows), various customized key bindings are required.
Yes, ctrl+right and friends stop on punctuation characters such as ( or ) by default.
Would you like to expand?
Default key bindings such as
ctrl+right or <kbd>ctrl+shift+right</kbd> use characters specified in word_separators to look for word boundaries
alt+right or <kbd>alt+shift+right</kbd> use characters specified in sub_word_separators to look for word boundaries.
However various other bindings like ctrl+backspace, which also use word_separators don’t have sub-word counter-parts, which may cause incomplete UX.
I’ve added the following bindings to my user config, which are the ones I missed at most. There may however be more worth for complete sub-word experience.
You’re telling me that it should stop before "strctBuffer (as i’d like)?
If yes, why isn’t working on my ST? My word_separators like looks just like the one you showed me.
Well, in the scenaio I had in mind, it actually does.
Even though moving forward and backward over ( seems inconsistent aka. different.
I can however confirm caret moving here the same way as in your screenshots.
It moves by words,
forward:
from word begin to word end
and then from word end to next word’s end
backward:
from word end to word begin
and then from word begin to previous word’s begin
It appears forward direction could be tweaked by moving forward by 1 sub-word after BufferReset, but it would still move caret behind ( but in front of structBuffer).
In this case, I don’t think your desired caret movement phillosophy can be achieved with simple settings. I guess custom plugin-based solution could achieve it, but otherwise it would be up to sublimehq to change default behavior or make it configurable.
Adding the following key bindings might result in behavior closer to your expectations. I used \W in context pattern, but it may be tweaked (maybe only whitespace or something along those lines), in order to tweak conditions when to move between words.
key bindings in User/Default.sublime-keymap override default bindings or those from any other package.
The following says:
When hitting ctrl+left, override default key binding and move caret to the end of the previous word, if the text in front of the caret, ends with ($) non-alphanumeric character (\W).
When hitting ctrl+right, override default key binding and move to beginning of the next word, if the text after the caret, starts with (^) non-alphanumeric character (\W).
word_separators setting to specify meaning of words, word_ends,
sub_word_separators setting to specify meaning of subwords and subword_ends.
Unfortunatelly it might not always be the same as \W pattern, but it should work reliable enough. If not, you might want to replace it by a pattern which matches your preferred word-separators. But this may fairly quickly end up in various syntax specific key bindings, as various syntaxes have different demands on word boundaries.
Ideally it would provide a context which tells us, whether caret is at the beginning or end of a word or subword, to tweak those bindings.