Sublime Forum

Focus/Cursor stuck in Search field

#1

When I search, focus is set to the search field; I then enter some search string in there, or edit the previous search string; I then press {enter} to trigger the search… and I’m stuck within the search field (and even {tab} and shift-{tab} move the cursor just within the search field).

The only - even then debatable! - valid reason for this behavior - instead by triggering the search setting the focus back to the editor - would be when there is NO search result, since then the user would perhaps want to edit their search string, but even that is far from being certain.

I am accustomed to spice up my interaction with software by AHK (AutoHotkey), and in regular circumstances, I would simply have intercepted my {enter} in ST to also switch focus IF {enter} is triggered in the search field.

Unfortunately though, and totally non-standard, ST does NOT allow to identify the search field as a specific control, so this way to escape the problem is not possible; I had to assign an additional key to focus switch then, and every time, for every search, I have to press TWO keys now, {enter} AND the second key.

This is really awful.

Couldn’t you do this as every other application handles the situations, and switch focus back automatically?

0 Likes

#2

I don’t think it is a reasonable assumption for the first found candidate to always be the desired target to go to. Hence automatically closing find panel or switching focus to a view after hitting enter once would be a bug, IMHO.

But everyone has different habbits which can’t be satisfied out of the box for everyone.

Hence ST is heavily customizable. Many aspects can be tweaked either by macros, plugins or just custom key bindings without the need for external tools to modify behaviors.

To automatically hide find panel and return to last active view by hitting enter the following key binding can be added:

	{
		"keys": ["enter"],
		"command": "chain",
		"args": {
			"commands": [
				{"command": "find_next"},
				{"command": "hide_panel" }
			]
		},
		"context":
		[
			{"key": "panel", "operand": "find"},
			{"key": "panel_has_focus"}
		]
	},
2 Likes

#3

Thank you, deathaxe, for your scriptlet; without the find_next line (which doesn’t make sense since nobody would triggering search automatically exclude the first hit for that search), it brings me back to the editor indeed (and without having to press {escape} for that… whilst closing the search panel though, which seems unavoidable then?

(Standard for “better” editors, e.g. EmEditor, etc., is that the search field remains open but does NOT retain focus IF a hit was found, just “lesser” editors invariably close the search panel in that case; in the absence of a(ny) hit, they (hopefully) all preserve focus within the search panel, since then that’s the only behavior that makes sense.)

I understand that the ST search panel, while retaining focus by its own, really weird standard (i.e. without your scriptlet running), remains “functional” in that way that subsequent {enter} will work as standard F3 works in all other editors: they select the subsequent hit (if there is any), and that seems to make sense for finding / visually checking the hits 1-by-1, since subsequent {enter} are more easily “within reach” than the F3 key is, but obviously NOT for editing one or some of them, since then, the user needs the standard F3 instead, anyway…

But that design decision obviously is related to ST’s - quite dangerous, since a filter functionality (with vicinity view, i.e. display of the surrounding lines, cf. EmEditor) seems to be missing - bulk change functionality: “Find All”, and then any editing within the search panel will edit all hits, wanted and unwanted ones…

Btw, sometimes, “Find All” will sometimes really display all hits very prominently, orange background within the general dark grey background, so that the user can visually check without fault, but then again, the same command just draws a thin rectangle around the hits, and which can easily be overlooked for a hit that should NOT be changed… and sometimes, the “Find All” has to be triggered twice, instead of just once, in order for the display to work as expected, and then again, as soon as the user switches focus from the search pane to the editor, in order for {pgup/dn} to become accessible for scrolling - they are NOT accessible while the search panel retains focus! -, the prominent hit display reverts back to the very “discrete” one, or sometimes, “Find All” makes the search panel vanish, and then again not - all this quite inscrutable indeed…

In order to preserve the prominent hit display, you seem to stay within the search panel by all means, and then exclusively scroll with the mouse wheel - and any mouse click then within the editor will make you lose it again.

So, yes, if you do it all (sic!) exactly as the ST developers want you to do it, the ST design comes with some benefits, but I wouldn’t call that “user-friendly”, general standards being considered bugs, and all that… :wink:

The font rendering on screen is spectacular though…

EDIT:

With your kind information, deathaxe, I was then able to do the right key assigment:

{ "keys": ["enter"], "command": "focus_group", "args": { "group": 0 },
	"context":
	[
		{"key": "panel", "operand": "find"},
		{"key": "panel_has_focus"}
	]
},

(And your scriptlet is also very valuable as a “chain” example!)

Thank you again, very much indeed!

0 Likes

#4

without the find_next line (which doesn’t make sense since nobody would triggering search automatically exclude the first hit for that search),

The find_next command is called when clicking Find button or hitting enter. The suggestion just adds auto-hiding the panel afterwards. Whether it’s needed or not depends on Highlight Matches state. If turned off, find_next must be called once for search to be performed - which was my assumption. It may however be ommitted if Highlight Matches is enabled and the first matching result was already selected while typing.

Note: Haven’t looked into ways to automatically call or omit find_next depending on Highlight Matches.

Standard for “better” editors, e.g. EmEditor, etc., is that the search field remains open but does NOT retain focus …

Re-focusing active view is easy to achive with a tiny plugin, which applicates existing APIs.

Just create a Packages/User/refocus.py with following content:

import sublime_plugin


class FocusActiveSheetCommand(sublime_plugin.WindowCommand):
	def run(self):
		active_sheet = self.window.active_sheet()
		if active_sheet:
			self.window.focus_sheet(active_sheet)

and bind it to your key:

{ "keys": ["enter"], "command": "focus_active_sheet",
	"context": [
		{"key": "panel", "operand": "find"},
		{"key": "panel_has_focus"}
	]
}

or

	{
		"keys": ["enter"],
		"command": "chain",
		"args": {
			"commands": [
				{"command": "find_next"},
				{"command": "focus_active_sheet" }
			]
		},
		"context":
		[
			{"key": "panel", "operand": "find"},
			{"key": "panel_has_focus"}
		]
	

“Find All” makes the search panel vanish, and then again not - all this quite inscrutable indeed…

This behavior can be modified to some degree, using following settings.

	// This determines whether the find panel is closed when the "Find All"
	// or "Replace All" buttons are pressed. Note this does not change the
	// keybinding behavior.
	"close_find_after_find_all": true,
	"close_find_after_replace_all": true,

The idea behind auto-hiding the panel after “Find All” is to get back to the editor once, everything has been replaced.

In order to preserve the prominent hit display, …

ST manages 3 categories of highlightings:

  • primary selection
  • primary find result
  • highlighting of words matching current selection or find result

All of them need to have different styles and/or colors to be distinguishable.

If find panel is open (focused or not), current find result is highlighted with find_highlight and find_highlight_foreground colors.

If find panel is closed, current find result is just selected and thus highlighted with selection color.

Other matching words use outline style using highlight color.

How prominent those colors are, depends on color schemes. If you are dissatisfied with a color scheme’s choice, you probably want to adjust them via UI: Customize Color Scheme.


Beyound that, you could always open enhancement or feature requests with clear description of a problem and an repreoducible outline of what a better behaviour could be.

My personal experience is, that high quality reports, which really make sense, have high chances to get implemented, if a benefit for UX is clearly realizable.

0 Likes

#5

Have a look at Incremental find. Find >Incremental Find in the menu or ctrl+i

0 Likes

#6

Heard about it, but never really got in touch with it.

Any official documentation other than the community docs to read about it?

I just found https://docs.sublimetext.io/guide/usage/search-and-replace.html#single-file

0 Likes

#7

I don’t believe so but it’s supposed to be similar to Visual Studio’s Incremental Search.

0 Likes