Sublime Forum

Copy to clipboard only if text is selected

#1

I want to configure Sublime to only copy to the clipboard if I actually have text selected. If nothing is selected I want super+c to do nothing at all.

I quite often accidentally press super+c (copy) when I really wanted to do super+v (paste). This is not normally a problem in other applications which only copy to the clipboard when something is actually selected. But with Sublime if I have nothing selected, it copies the current line to the clipboard.

This is really annoying since that usually means I end up cloning a line instead of pasting what I had previously copied, but more importantly I loose what I had intended to paste and have to go and find it again so I can copy it a second time.

I’m no stranger to configuring keyboard shortcuts in Sublime, having remapped several shortcuts, but I’m not having any luck with this one. Here’s what I’m currently using in my User keymap:

[code] // remap copy to only work if something is selected
{ “keys”: “super+c”], “command”: “copy”, “context”:

  { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]

},
[/code]

but this seems to have no effect. What’s even more strange is if I completely comment this out and the default keymap so that nothing is assigned to the keyboard shortcut super+c, the default behaviour still applies. Even after restarting Sublime.

Is there any way to get this to work the way I want?

0 Likes

#2

I would say that a better strategy would be to write a plugin that behaves how you want then bind control+c to that.

Something like this: [code]import sublime, sublime_plugin

class NormalCopyCommand(sublime_plugin.TextCommand):
def run(self, edit):
sel = self.view.sel()[0]
if not sel.empty():
sublime.set_clipboard(self.view.word(sel))[/code]

My python is a bit rusty and I forgptten the ST2 api so you might want to double check that. That won’t work for multiple selection either. You’ll have to loop and stuff. Good luck.

0 Likes

#3

// If true, the copy and cut commands will operate on the current line
// when the selection is empty, rather than doing nothing.
“copy_with_empty_selection”: true,

0 Likes

#4

If you really want to do it by rebinding, do it like this:

[code] { “keys”: “super+c”], “command”: “noop” },
{ “keys”: “super+c”], “command”: “copy”, “context”:

  { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
]

},
[/code]

The trick is the noop first, which unbinds it. Otherwise the new binding fires when you’ve got a selection, but if you don’t have a selection the old binding still fires.

0 Likes

#5

[quote=“adzenith”]// If true, the copy and cut commands will operate on the current line
// when the selection is empty, rather than doing nothing.
“copy_with_empty_selection”: true,[/quote]

As of March 2013, this preference is only available in the Dev build of ST2. And for those of us who want to copy to clipboard only if text is selected, they’ll want to set this preference to false.

Fortunately, adzentih’s keybind method works in the Stable build, as well. So there is a working solution if you don’t want to use the Dev build.

0 Likes