By josh / March 20, 2014

This one simple Sublime Text tweak makes copying and pasting code a breeze

Pasting code from another file or from a sample you find on the Internet is a constant annoyance. The indentation levels hardly ever line up, so a quick copy-and-paste becomes an arduous formatting session.

And even if the indentation levels happen to match, chances are you didn’t select the leading whitespace, so you still have a hassle on your hands.

Think about it… If you copy and paste code 10 times per day on average, and spend 30 seconds messing with the indentation levels each time … Assuming you work 5 days a week, 50 weeks a year, you spend nearly 21 hours a year just fiddling with the tabs or spaces in front of your code!

Sublime’s Paste and Indent command fixes this by automatically adjusting the indentation of your pasted code to match the surrounding lines.

This command is bound to Ctrl+Shift+V on Windows and Linux or Shift+Cmd+V on Mac. Give it a try—it’s magical.

But … if you’re like me, you’ll find it’s impossible to fight muscle memory and find yourself falling back to the standard paste shortcut 90 percent of the time.

Fortunately, Sublime makes it easy to override its default shortcuts, so you can swap the bindings for the Paste command with Paste and Indent.

If you’re on a Mac, add the following to your Default (OSX).sublime-keymap file:

{ "keys": ["super+v"], "command": "paste_and_indent" },
{ "keys": ["super+shift+v"], "command": "paste" }

And if you’re a Windows user, you’ll want to edit your Default (Windows).sublime-keymap file and add:

{ "keys": ["ctrl+v"], "command": "paste_and_indent" },
{ "keys": ["ctrl+shift+v"], "command": "paste" }

For more ways to streamline your Sublime Text workflow, check out:

About the author


Click here to add a comment

Leave a comment: