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
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:
import sublime_plugin
class RemoveWhitespaceToNextCharCommand(sublime_plugin.TextCommand):
def run(self, edit):
reg = self.view.find(r"\s+", self.view.sel()[0].a)
self.view.replace(edit, reg, "")
Hit Save, and it will automatically select your Packages/User
folder as the location. Name the file remove_whitespace_to_next_char.py
. As soon as it's saved, the remove_whitespace_to_next_char
command will be available, no restart required.
Follow the directions below for creating a custom keybinding, only use this instead:
{
"keys": ["ctrl+alt+space"],
"command": "remove_whitespace_to_next_char"
}
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.
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 your Packages
folder, which is the one opened when you select Preferences → 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:
{
"replacements": {
"remove_spaces_to_next_char": {
"find": "(\\s+)",
"replace": "",
"greedy": false,
"case": false
}
}
}
Save the file - it should automatically save in your Packages/User
directory. 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-User
to 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 ]
:
{
"keys": ["ctrl+alt+space"],
"command": "reg_replace",
"args": {"replacements": ["remove_spaces_to_next_char"]}
}
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:
hello here is the cursor |dfsdlkf
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 RegReplace
plugin instead of cloning manually, so you get updates if there are any in the future.