Home Download Buy Blog Forum Support

Copy to clipboard only if text is selected

Copy to clipboard only if text is selected

Postby whittaker007 on Thu Dec 06, 2012 1:08 am

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: Select all
  // 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 }
    ]
  },


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?
whittaker007
 
Posts: 2
Joined: Thu Dec 06, 2012 12:56 am

Re: Copy to clipboard only if text is selected

Postby C0D312 on Thu Dec 06, 2012 2:10 am

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: Select all
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))


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.
C0D312
 
Posts: 1063
Joined: Sun Jul 10, 2011 3:23 am

Re: Copy to clipboard only if text is selected

Postby adzenith on Thu Dec 06, 2012 4:47 am

// 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,
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Copy to clipboard only if text is selected

Postby adzenith on Thu Dec 06, 2012 4:49 am

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

Code: Select all
  { "keys": ["super+c"], "command": "noop" },
  { "keys": ["super+c"], "command": "copy", "context":
    [
      { "key": "selection_empty", "operator": "equal", "operand": false, "match_all": true }
    ]
  },


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.
adzenith
 
Posts: 1217
Joined: Mon Oct 19, 2009 9:12 pm

Re: Copy to clipboard only if text is selected

Postby coredumperror on Wed Mar 27, 2013 11:29 pm

adzenith wrote:// 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,

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.
coredumperror
 
Posts: 3
Joined: Sat Sep 01, 2012 2:03 am


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 17 guests