By josh / June 9, 2014

Prevent copy and paste rage with this simple tweak (part 2)

A few weeks back I wrote a post about the copy_with_empty_selection setting, which allows you to tell Sublime not to copy the current line if you haven’t selected any text.

A lot of readers were happy with the tweak, but reader Carson noticed a downside:

Hey Josh, great tip (and I love all your emails), but one thing I noticed is that this breaks the ability to just ctrl+x or ctrl+c and grab a whole line when your cursor is on a line (not selecting everything). I mean, duh – of course it does, but I use that a lot and ran into it within a few minutes after setting this up. You’re probably already aware of that, but in case you weren’t – well, now you are. 🙂 Keep up the great work, sir.

It is useful to be able to grab the entire line without making a selection first, so I did some experimentation to see if I could find a keyboard shortcut mapping that preserves the ability to use cut and copy on lines of code but prevents you from accidentally overwriting the clipboard when the cursor is on a blank line. (For me, this is the most frustrating part of the copy lines feature.)

Keyboard shortcuts support a context element that allows you to add rules about when the shortcuts should or shouldn’t fire. The following keybinding maps the copy and cut commands (on OS X) to the noop command when the selection is empty and the text that proceeds and follows the cursor contains only white space:

[
    { 
        "keys": ["super+c"], "command": "noop", "context":
        [
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "preceding_text", "operator": "regex_match", "operand": "^\\s*$", "match_all": true },
            { "key": "following_text", "operator": "regex_match", "operand": "^\\s*$", "match_all": true }
        ]
    },
    { 
        "keys": ["super+x"], "command": "noop", "context":
        [
            { "key": "selection_empty", "operator": "equal", "operand": true, "match_all": true },
            { "key": "preceding_text", "operator": "regex_match", "operand": "^\\s*$", "match_all": true },
            { "key": "following_text", "operator": "regex_match", "operand": "^\\s*$", "match_all": true }
        ]
    }
]

If you’re on Windows or Linux, you’ll want to replace super with ctrl.

With this keybinding in place, you can delete the copy_with_empty_selection override from your preferences file and the copy and cut shortcuts will allow you to grab the contents of the current line without making a selection. But it will ignore the copy and cut commands if you’re on a blank line.

Sublime’s keyboard shortcut mappings are extremely flexible. To learn more about how you can make use of advanced features in Sublime, go here:

http://sublimetexttips.com/newsletter

Josh Earl

About the author

josh

3 comments
muah - February 3, 2015

hi there ,big thanx for this ,
am currently trying to cut the current line without selection ,the problem is the `super+x` works but it behave like `super+c` which copy the line not cut it, is there a work around for this ?

Reply
    Cpt. Obvious - April 25, 2016

    Have you tried the delete button?

    Reply
Leeroy - February 23, 2016

I have been using an OS-level clipboard manager (ClipX) for a while and I cannot recommend it enough!

I love the feng shui of Sublime and woul’ve been thrilled to stick to its clipboard managing feature instead (and have a clean systray :P). In practice though ClipX has time and time again proven to be an invaluable safety net. As much as it saddens me, even in 2016, an upgrade to the copying and pasting functionality is THE one thing with the greatest positive impact on my productivity. I cannot conceive working without it.

Until computers evolve a decent way to herd data around, everyone should use an OS-level clipboard manager.

Reply
Click here to add a comment

Leave a comment: