With ST2, at the current cursor position, I'd like to delete every whitespace or tab or newline until a character is present. CTRL+DEL or CTRL+J kind of works, but sometimes you have to repeat it multiple times (or when there are empty lines in the middle, it doesn't work), etc.
Is there a way to do this with Sublime 2?
Before:
hello here is the cursor |
dfsdlkf
After:
hello here is the cursor |dfsdlkf
Edit
While the RegReplace plugin described below in my original answer is great for all sorts of things, including this task, it is not absolutely required here. You can make a quick specialized plugin that is hard-coded to just do this.
In ST2, select
Tools → New Plugin…. Erase the contents of the file that shows up and paste in this:Hit Save, and it will automatically select your
Packages/Userfolder as the location. Name the fileremove_whitespace_to_next_char.py. As soon as it's saved, theremove_whitespace_to_next_charcommand will be available, no restart required.Follow the directions below for creating a custom keybinding, only use this instead:
Again, you can use whatever key combo is not currently in use. Save the keybindings file, and that's it! Whenever you want to delete any whitespace up to the next non-whitespace character in front of the cursor, just hit CtrlAltSpace.
This plugin will work as-is in ST3 and ST4 as well.
Original Answer
Unfortunately, it is not possible to record find/replace actions with a macro. However, this task is possible using a plugin called
RegReplace. Because you're still using ST2, you'll need to clone the ST2 branch of the repo directly into yourPackagesfolder, which is the one opened when you selectPreferences → Browse Packages…. Once you've cloned the repo and switched to the ST2 branch, restart ST2 for the correct version of the plugin to take effect.Now, select
Preferences → Package Settings → Reg Replace → Settings-User, and a blank file will open up. Set its contents to the following:Save the file - it should automatically save in your
Packages/Userdirectory. The regex is quite simple - it simply finds one or more whitespace characters (\s, which includes,\t,\r, and\n) in front of the current cursor position up to the next non-whitespace character, and replaces them with nothing.Next, we'll need to assign our new command to a key binding. Select
Preferences → Keybindings-Userto open your custom keybindings file. If you don't have any set, the file will consist of an empty JSON array:If you've already assigned custom keybindings, you can put the new one wherever you'd like to. Add the following between the beginning
[and the ending]:This assigns our new command to CtrlAltSpace, which is unused in a fresh installation of ST2. You'll want to verify that it is unused in your environment. You can change it to whatever combination you like.
And that's it. In your sample text with the cursor at the
|position on the first line, running the command yields:NOTE: This solution should work out of the box using ST3 and ST4 as well. Just make sure you use Package Control to install the
RegReplaceplugin instead of cloning manually, so you get updates if there are any in the future.