coteditor / CotEditor

Lightweight Plain-Text Editor for macOS
https://coteditor.com
Other
6.49k stars 432 forks source link

Mixed Use of Spaces and Tabs in CotEditor #1754

Closed staylet closed 1 week ago

staylet commented 2 weeks ago

CotEditor is my preferred text editor because of its lightweight and stable features. However, I recently encountered an issue when editing files that include both spaces and tabs. My use case involves spaces for indentation, which is common in popular editors, formatted like this:

<space><space> line1
<space><space> line2

Additionally, the file might include tabs to structure columns, as in:

<space><space> field1 <tab> field2
<space><space> field1 <tab> field2

Initially, I checked under Edit Settings > Prefer using spaces/tabs, but this appears to be a global setting. It doesn’t let me specify precisely when tabs should serve as indent when they should act as <Tab> for alignment within the same file.

Then I discovered that pressing Option+Tab allows me to insert a true <Tab> even when spaces are set as the preferred indentation. This seems like an ideal solution! However, I noticed that Option+Tab does not work universally across other editors like VSCode or TextEdit.

This leads me to wonder if Option+Tab is a unique feature in CotEditor, as I couldn’t find any related settings or documentation. I’d like to know where this keybinding originates and if it can be customized. Is there any information available about Option+Tab specifically in CotEditor?

Thank you!

1024jp commented 1 week ago

However, I noticed that Option+Tab does not work universally across other editors like VSCode or TextEdit.

Your observation is right. Inserting a tab character by Option+Tab is a kind of universal behavior, but it's a bit hard to explain.

Cocoa, which is the native framework for macOS applications, has an API named insertTabIgnoringFieldEditor(_:), which is called when Option+tab is pressed. As the name implies, this API always inserts a tab character by ignoring the normal behavior in the editor when just the tab key is pressed. For example, you will notice that pressing Option+tab in a type of user interface where the tab key moves the input field, such as a search field or input form, will enter the tab character instead of moving the field. This API is also valid for CotEditor.

Text editors like VS Code are not native macOS applications and, therefore, would not support the insertTabIgnoringFieldEditor. TextEdit.app is a native implementation and probably supports it. However, you will not notice the difference because the tab character is also entered when you press the tab key normally.

staylet commented 1 week ago

However, I noticed that Option+Tab does not work universally across other editors like VSCode or TextEdit.

Your observation is right. Inserting a tab character by Option+Tab is a kind of universal behavior, but it's a bit hard to explain.

Cocoa, which is the native framework for macOS applications, has an API named insertTabIgnoringFieldEditor(_:), which is called when Option+tab is pressed. As the name implies, this API always inserts a tab character by ignoring the normal behavior in the editor when just the tab key is pressed. For example, you will notice that pressing Option+tab in a type of user interface where the tab key moves the input field, such as a search field or input form, will enter the tab character instead of moving the field. This API is also valid for CotEditor.

@1024jp Thank you so much for your attention and response to my question! Your input is highly appreciated and has been incredibly helpful.