Sublime Forum

Is it possible to trigger the folding behavior of clicking on gutter arrow, but via keyboard?

#1

Basically, I’m looking for a way to simulate how folding works in mainstream outliners (an example that may be familiar is Workflowy)

There are 2 ways in which sublime’s folding behavior diverge from the behavior I have in mind

  1. In sublime, if you execute the fold command while caret is on a given line, that line itself became folded up (hence now invisible). By contrast, in most outliners, it is the content below, and “in the scope of” (i.e. have deeper indentation), the current line, that gets folded. Thus if you want to fold up the body of a function (but leave the function definition line visible), and if the caret is current on the function definition line, you must move caret down a line, then fold.

  2. In sublime, but not in most outliners, triggering the folding command results in the folded region being selected. Thus if all one wants to do is to simply hide some section of code, there’s a further step of deselection that must be carried out, making the workflow a bit less smooth.

(Incidenally, as far as I can see, the default folding behavior of vscode seems identical to mainstream outliners on point 1 and 2, and hence different from sublime’s)

Now, the folding behavior that I want is present in sublime. I know this because it’s exactly the same behavior as triggered by clicking on the folding arrow in the gutton.

However I’ve never been able to figure out what the command for triggering “gutter folding” is, or if it exists.

Could someone help me out? If the command doesn’t exist, do you think there’s a way to simulate the effect of clicking on gutter arrow?

Thanks!

0 Likes

#2

Menu items are in Edit -> Code Folding, on my Mac these keyboard shortcuts look like this:

{ "keys": ["super+alt+["], "command": "fold" },
{ "keys": ["super+alt+]"], "command": "unfold" },
1 Like

#3

Thanks for the reply, but what I’m looking for is slightly different. Executing “fold” and “unfold” commands via the keybindings you mentioned (i) results in the folded region being selected (by contrast, merely clicking on the gutter arrow doesn’t result in anything being selected) and moreover (ii) folds the line where the caret is (whereas I’m looking for a way to avoid including the caret line in the folded region)

1 Like

#4

Here’s a gif illustrating the behavior I have in mind. The software shown is vscode - basically I’m trying to see if there’s a way to simulate vscode’s folding behavior in sublime.

2 Likes

#5

You can using a macro (or plugin if you wanted to go that route) and override the existing fold keybinding: http://docs.sublimetext.info/en/latest/extensibility/macros.html#key-binding-for-macros.

User/Macro Name.sublime-macro:

[
	{
		"command": "fold"
	},
	{
		"args":
		{
			"by": "characters",
			"forward": false
		},
		"command": "move"
	}
]
1 Like

#6

I think you could use the Soft Undo feature instead of the moving the caret:

User/Macro Name.sublime-macro

[
	{
		"command": "fold"
	},
	{
		"command": "soft_undo"
	},
]
0 Likes

#7

The only downfall there is if the cursor is in the middle of the folded region, navigation & text modification will be hidden until the cursor is moved outside of the folded region yet the cursor is visible outside the folded region.

0 Likes

#8

This will do as you demonstrated, @0217001506 :

  1. Create a new file, save as .Sublime-Macro under your Packages/User path, named as you prefer. For mine I used FoldToLine.sublime-macro. Here’s the macro for folding:

     [
     	{"command": "move", "args": {"by": "lines", "forward": true}},
     	{"command": "fold"},{"args":{"by":"characters","forward":false}},
     	{"command": "move_to", "args": {"to": "bol"}}
     ]
    
  2. Do the same again with another new .Sublime-Macro file, but for the unfolding command. I used UnfoldUnselect.sublime-macro for mine. Here’s the code for that one:

     [
     	{"command": "unfold"},
     	{"command": "move", "args": {"by": "characters", "forward": false}},
     	{"command": "move_to", "args": {"to": "bol"}},
     ]
    
  3. Next, to reassign the shortcuts for the default fold/unfold commands our new user macros, we need to add the binds in the Sublime Text > Preferences > Key Bindings file. Here’s the code for that. Just be sure to change the path/filename to the files as you chose where/what to save/name them:

     	{ "keys": ["super+alt+["], "command": "run_macro_file", "args": {"file": "Packages/User/Macros/FoldToLine.sublime-macro"} },
     	{ "keys": ["super+alt+]"], "command": "run_macro_file", "args": {"file": "Packages/User/Macros/UnfoldUnselect.sublime-macro"} },
    

If this is your only keybind entry in that file, remove the comma at the end of the last line.

Also, like the macro files, the .sublime-keymap JSON file needs to start with [ and end with ].

That should do it! :slightly_smiling_face::+1:

I realize this thread has been dead & buried for a few years, but it was the first one I found when Googling about this, so I thought I would add a more detailed, thorough answer for anyone else who stumbles across it.

1 Like