Sublime Forum

Override the default key binding

#1

i try to override the default key binding, on the user key binding, but i find that only changing the default helps.

for example: ctrl+c under Default/Default.sublime-keymap is:

<binding key="ctrl+c" command="copy"/>

and i place under User/Default.sublime-keymap

<binding key="ctrl+c" command="copy"> <context name="allSelectionsEmpty" value="false"/> </binding>

well, it doesn’t work. only if i override the definition on Default/Default.sublime-keymap it works.

0 Likes

#2

good tip nick, it will become handy i am sure, but now after trying this, i can better say what i want (if possible). i got used to the copy/paste when there is no selection in sublime - very useful actually for duplicating lines.

what i want to change is only when i use empty-selection on empty-line - i don’t want it to get into the copy/paste buffer at all. so standing on a text line with no selection - i do want it to be copy/pasted, but standing on an empty line (i’ll be greedy and say only with whitespace) won’t affect the clipboard buffer (i.e. won’t override previous item on clipboard), does it make any sense? any ideas?

0 Likes

#3

Try:

<binding key="ctrl+c" command="noop">
	<context name="allSelectionsEmpty" value="true"/>
	<context name="allPrecedingText" value="$^"/>
	<context name="allFollowingText" value="$^"/>
</binding>

This means the key binding will only run when the text before and after the selection, on the same line, matches the given regexes. This could easily be extended to not copy if the line consists of whitespace only, if desired.

0 Likes

#4

great thanks!

<binding key="ctrl+c" command="noop"> <context name="allSelectionsEmpty" value="true"/> <context name="allPrecedingText" value="^\s]*$"/> <context name="allFollowingText" value="^\s]*$"/> </binding> <binding key="ctrl+x" command="runMacroFileSilently 'Packages/User/DeleteLine.sublime-macro'"> <context name="allSelectionsEmpty" value="true"/> <context name="allPrecedingText" value="^\s]*$"/> <context name="allFollowingText" value="^\s]*$"/> </binding>

and this macro at User/DeleteLine.sublime-macro:

expandSelectionTo line leftDeleteCharacters

0 Likes