Sublime Forum

Delete whitespace until first non-whitespace character

#1

For example, I have:

main(arg1,
     arg2)

and I would like to make these into one line, like:

main(arg1,arg2)

In Visual Studio & Notepad++, you can simply put your cursor after the comma, and press CTRL+Delete. This even works if there are multiple blank lines in between arg1 & arg2.

Is there an easy way to do this in Sublime?

0 Likes

#2

there may be other, simpler solutions to this, but I wrote a plugin which you could use with a keybinding

1 Like

#3

I wrote a similar command:

import sublime, sublime_plugin
from sublime import Region

class DeleteWhitespaceCommand(sublime_plugin.TextCommand):
    def run(self, edit, direction=None):
        for caret in self.view.sel():

            if direction in ["right", "both"]:
                pos = caret.end()
                while self.view.substr(pos).isspace(): pos += 1
                self.view.erase(edit, Region(caret.end(), pos))
            
            if direction in ["left", "both"]:
                pos = caret.begin()
                while self.view.substr(pos - 1).isspace(): pos -= 1
                self.view.erase(edit, Region(caret.begin(), pos))
0 Likes

#4

I just press DELETE at the cursor. Build 3175. :smile:

0 Likes

#5

Thank you! This worked great.

0 Likes

#6

Thank you, but I couldn’t get this to work - nothing happens.

0 Likes

#7

You have to pass a direction arg with a value of left, right, or both.

0 Likes

#8

I just use ctrl+j for this :thinking:

2 Likes

#9

Got it, thanks.

0 Likes

#10

All answers given here were great, but none quite matched ctrl+delete behavior on Windows.
There the behavior is as follows:

  • When you’re before whitespace, it should delete until the next non-whitespace character.
  • When you’re in the middle of a word, if deletes the rest of that word.
  • When you’re before a word, it deletes the whole word.
  • When you’re before a symbol, it deletes just the symbol.

If there is a selection, then the selection is first deleted, and then the above behavior is applied.

The following plugin does this all:

import sublime, sublime_plugin
from sublime import Region

class CtrlDeleteCommand(sublime_plugin.TextCommand):
    def run(self, edit):

        for caret in self.view.sel():

            caretEnd=caret.end()
            caretSize=caret.size()
            #first, delete selection
            if caretSize>0:
                self.view.erase(edit, caret)
                caretEnd-=caretSize #caret doesn't change when erasing

            if caretEnd==self.view.size(): continue

            nextChar=self.view.substr(caretEnd)

            pos = caretEnd + 1
            #if before space, delete upcoming whitespace
            if nextChar.isspace():
                while self.view.substr(pos).isspace(): pos += 1
                self.view.erase(edit, Region(caretEnd, pos))
            #else delete next word
            else:
                #if before symbol, just delete symbol, else delete next word
                if nextChar.isdigit() or nextChar.isalpha():
                    while True:
                        nextChar = self.view.substr(pos) #this is fine even if at end of file
                        if not nextChar.isdigit() and not nextChar.isalpha():
                            break;
                        pos+=1
                self.view.erase(edit, Region(caretEnd, pos))
0 Likes