Sublime Forum

Remove / Jump over sequential whitespaces like Notepad++

#1

Q1:
How can I delete (forward / backward) for sequential whitespaces (eg: space, newlines, tab , etc) in the same way as Notepad++ :

In Notepad++, when I press Ctrl+Backspace (or Ctrl+Delete), it can remove sequential whitespaces in a time

Note that:

  1. the sequential whitespaces I want to remove may not lay on the ending of file, so trim_trailing_white_space_on_save setting does not work.

  2. I may want to remove a specific set of sequential whitespaces, but not all occurrences in my file.
    Using regex can search until the cursor highlighting the target and remove it, but it is much more efficient to place the caret at the desired location and press Ctrl+Backspace (or Ctrl+Delete) for 1 time and done

Q2:
Also I want to move caret across sequential whitespaces in the same way as Notepad++

In Notepad++, when I press Ctrl+Left (or Ctrl+Right), it can jump over sequential whitespaces in a time

0 Likes

#2

Sublime does exactly that by default, doesn’t it? ctrl modifier moves and removes words and any amount of whitespace.

The primary difference between ST and any other Windows application is the way it positions carets when moving caret word-wise.

ST prefers to position caret at the end of words when moving rightwards and to word begins, when moving leftwards, to enable quick selection and removal of whole words (without leading or trailing whitespace).

All other Windows Apps (such as Notepad/Notepad++) always keep caret at word begins, thus always remove whitespace and following/preceding words (or vice versa). You can’t therefore easily remove words without surrounding spaces.

If you prefer the “Windows” style of caret movement you might want to try following user specific key bindings.

Note: They are enabled by "windows_caret_movement": true in Preferences.sublime-settings.

[
	// Windows Style Word Navigation
	//  (caret is always moved to beginning of words)

	{ // move caret to beginning of previous word on the left
		"keys": ["ctrl+left"],
		"command": "move", "args": {"by": "words", "forward": false},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // move caret to beginning of next word on the right
		"keys": ["ctrl+right"],
		"command": "move", "args": {"by": "words", "forward": true},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // select everything including previous word on the left
		"keys": ["ctrl+shift+left"],
		"command": "move", "args": {"by": "words", "forward": false, "extend": true},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // select everything to next word on the right
		"keys": ["ctrl+shift+right"],
		"command": "move", "args": {"by": "words", "forward": true, "extend": true},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // delete everything including previous word to the left
		"keys": ["ctrl+backspace"],
		"command": "chain",
		"args": {
			"commands": [
				{ "command": "move", "args": {"by": "words", "forward": false, "extend": true } },
				{ "command": "right_delete" }
			]
		},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // delete delete everything to next word on the right
		"keys": ["ctrl+delete"],
		"command": "chain",
		"args": {
			"commands": [
				{ "command": "move", "args": {"by": "words", "forward": true, "extend": true} },
				{ "command": "left_delete" }
			]
		},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},

	// Windows Style Subword Navigation
	//  (caret is always moved to beginning of sub-words)

	{ // move caret to previous sub-word on the left
		"keys": ["alt+left"],
		"command": "move", "args": {"by": "subwords", "forward": false},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // move caret to next sub-word on the right
		"keys": ["alt+right"],
		"command": "move", "args": {"by": "subwords", "forward": true},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // select sub-word to the left
		"keys": ["alt+shift+left"],
		"command": "move", "args": {"by": "subwords", "forward": false, "extend": true},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // select sub-word to the right
		"keys": ["alt+shift+right"],
		"command": "move", "args": {"by": "subwords", "forward": true, "extend": true},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // remove sub-word to the left
		"keys": ["alt+backspace"],
		"command": "chain",
		"args": {
			"commands": [
				{ "command": "move", "args": {"by": "subwords", "forward": false, "extend": true } },
				{ "command": "right_delete" }
			]
		},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
	{ // remove single space/tab at bol instead of all whitespace
		"keys": ["alt+backspace"],
		"command": "chain",
		"args": {
			"commands": [
				{"command": "move", "args": {"by": "characters", "forward": false, "extend": true} },
				{"command": "right_delete" }
			]
		},
		"context": [
			{ "key": "setting.windows_caret_movement" },
			{ "key": "preceding_text", "operator": "regex_match", "operand": "^[ \t]+$", "match_all": true }
		],
	},
	{ // remove sub-word to the right
		"keys": ["alt+delete"],
		"command": "chain",
		"args": {
			"commands": [
				{ "command": "move", "args": {"by": "subwords", "forward": true, "extend": true} },
				{ "command": "left_delete" }
			]
		},
		"context": [
			{ "key": "setting.windows_caret_movement" }
		]
	},
]
0 Likes