Change Indent Keyboard Shortcut?
-
Is there a way to customize the indent Hotkey (Keyboard Shortcut)?
I’m looking to indent a line, having the caret be located anywhere on that line, like the use of “Ctrl + ]” in Google Docs or Google Keep.
I know there’s “Tab” and “Shift + Tab”, but they work only on multi-line selection, or when the caret is located at the beginning of a line.
Might there be an Option, or a Plugin available to customize the indent function?
-
@Stefane-Guevremont
If you have NppExec, you can use the following script:
SCI_SENDMSG SCI_GETCURRENTPOS SET LOCAL POS = $(MSG_RESULT) SCI_SENDMSG SCI_GETTABWIDTH SET LOCAL TAB = $(MSG_RESULT) SET LOCAL FINALPOS ~ $(POS) + $(TAB) SCI_SENDMSG SCI_GOTOLINE $(CURRENT_LINE) NPP_MENUCOMMAND Edit\Indent\Increase Line Indent SCI_SENDMSG SCI_GOTOPOS $(FINALPOS)
And then save it an have NppExec add it to the Macro menu and then assign the 'CTRL + ]` shortcut to it with Settings => Shortcut Mapper…
Cheers.
-
@Stefane-Guevremont
Michael-Vincent said in Change Indent Keyboard Shortcut?:@Stefane-Guevremont
If you have NppExec, you can use the following script:Assume you may want the reverse as well. This works:
::lineindent SCI_SENDMSG SCI_GETCURRENTPOS SET LOCAL POS = $(MSG_RESULT) SCI_SENDMSG SCI_GETTABWIDTH SET LOCAL TAB = $(MSG_RESULT) SCI_SENDMSG SCI_GOTOLINE $(CURRENT_LINE) NPP_MENUCOMMAND Edit\Indent\Increase Line Indent SET LOCAL POS ~ $(POS) + $(TAB) SCI_SENDMSG SCI_GOTOPOS $(POS) ::lineunindent SCI_SENDMSG SCI_GETCURRENTPOS SET LOCAL POS = $(MSG_RESULT) SCI_SENDMSG SCI_GETTABWIDTH SET LOCAL TAB = $(MSG_RESULT) SCI_SENDMSG SCI_GETLENGTH SET LOCAL TOTAL = $(MSG_RESULT) SCI_SENDMSG SCI_GOTOLINE $(CURRENT_LINE) NPP_MENUCOMMAND Edit\Indent\Decrease Line Indent SCI_SENDMSG SCI_GETLENGTH IF "$(MSG_RESULT)"!="$(TOTAL)" THEN SET LOCAL POS ~ $(POS) - $(TAB) ENDIF SCI_SENDMSG SCI_GOTOPOS $(POS)
There’ s just a bit more logic in the unindent case so that we don’t keep changing the position if we’ve already unindented the line all the way it can be to the margin.
Cheers.
-
Hello, @stefane-guevremont, @michael-vincent and All,
Two other solutions :
-
The more simple :
-
Press the
ALT
key and, simultaneously, hit the0
key, then the9
key of the numeric keypad -
Release the
ALT
key
-
- With a macro :
<Macro name="Insertion TABULATION ( \x09 )" Ctrl="yes" Alt="no" Shift="no" Key="221"> <Action type="1" message="2170" wParam="0" lParam="0" sParam="	" /> </Macro>
Best Regards
guy038
-
-
@guy038 said in Change Indent Keyboard Shortcut?:
With a macro
I must be missing something.
With this all I get is a tab character inserted at the current caret position (in the middle of a line).
Wasn’t the OP’s desire to have the caret be in the middle of a line, then have a way to press a key and get the line indented (on the far left)?@Michael-Vincent Right (see below), that’s why I’m questioning @guy038’s macro thing.
-
@Alan-Kilborn said in Change Indent Keyboard Shortcut?:
Wasn’t the OP’s desire to have the caret be in the middle of a line, then have a way to press a key and get the line indented (on the far left)?
I thought so and that’s what my NppExec scripts do (both adding or subtracting a level of indent depending on which you use).
Cheers.
-
@Alan-Kilborn
@PeterJones
@Stefane-Guevremont said in Change Indent Keyboard Shortcut?:I know there’s “Tab” and “Shift + Tab”, but they work only on multi-line selection, or when the caret is located at the beginning of a line.
OP says the above and my testing concurs. Is this not a bug? I mean the “Tab” and “Shift+Tab” are actually mapped to Edit => Indent => Increase Line Indent / Decrease Line Indent. From those option names, I would think the OP’s desired behaviour would be accomplished.
I also see that in the Settings => Shortcut Mapper…, “Tab” and “Shift+Tab” are mapped to Scintilla commands SCI_TAB and SCI_BACKTAB respectively, which don’t implement what the OP wants nor do they do “Increase Line Indent” or “Decrease Line Indent” as “advertised”.
Maybe it’s semantics, the command menu options should be renamed to “insert Tab” / “remove Tab”, or their behavior should be changed to implement what the OP is asking for so they work “as advertised”.
What do you think?
Cheers.
-
I concur with @Alan-Kilborn and @Michael-Vincent 's interpretation.
However, it’s macro-recordable:
<Macro name="FancyIndent" Ctrl="yes" Alt="yes" Shift="no" Key="221"> <Action type="0" message="2453" wParam="0" lParam="0" sParam="" /> <Action type="0" message="2327" wParam="0" lParam="0" sParam="" /> </Macro> <Macro name="FancyUnindent" Ctrl="yes" Alt="yes" Shift="no" Key="219"> <Action type="0" message="2453" wParam="0" lParam="0" sParam="" /> <Action type="0" message="2328" wParam="0" lParam="0" sParam="" />
It can be recorded as
HOME
followed byTAB
orHOME
followed byShift+TAB
(assuming the default Settings > Shortcut Mapper > Scintilla Commands ofSCI_VCHOMEWRAP
=Home
) -
@PeterJones said in Change Indent Keyboard Shortcut?:
It can be recorded as HOME followed by TAB
Yes but that moves the caret; may not be wanted by the OP.
-
@Michael-Vincent said in Change Indent Keyboard Shortcut?:
“Tab” and “Shift+Tab” are actually mapped to Edit => Indent => Increase Line Indent / Decrease Line Indent.
And those functions have the bug: because if you are in the middle of the line, and use even the edit-menu version, it will actually insert the tab (or equivalent spaces, depending on setting), not increase the line indent, as advertised.
Personally, I think the tab key should have the multi-purpose meaning: change indent when typed anywhere before the first non-space/tab character of a line, and insert tab (or equivalent spaces) anywhere after that.
@Alan-Kilborn said,
Yes but that moves the caret; may not be wanted by the OP.
Right, I meant to mention that caveat, but obviously forgot.
-
@PeterJones said in Change Indent Keyboard Shortcut?:
Personally, I think the tab key should have the multi-purpose meaning: change indent when typed anywhere before the first non-space/tab character of a line, and insert tab (or equivalent spaces) anywhere after that.
And I concur with that! I think we certainly agree Edit => Indent => In/Decrease Line Indent don’t work as advertised - regardless of the shortcut key mapped to them.
My point about the Shortcut Mapper was I don’t see the “Tab” / “Shift+Tab” mapped to the Edit => Indent … options leading me to wonder (without looking at the N++ code) if they just insert a SCI_TAB / SCI_BACKTAB character and there is no “logic” behind those items.
In that case it’s not a “bug” - it’s more a feature request to have them “do what they say”.
Cheers.
-
Perhaps of interest; they are links, click on them:
-
Hi All,
My bad ! I read too quickly ! Then, my previous post is quite irrelevant !
So, if I completely understood the problem, the OP want, via a shortcut, to move the caret, from current position on a line to the next
tabstop
? That is to say to the column =n x T + 1
, whereT
is the tab size andn
an integer>= 0
?BR
guy038
-
@guy038 said in Change Indent Keyboard Shortcut?:
to move the caret, from current position on a line to the next tabstop
I don’t think so; I think OP would want to have the line indented one tab stop (be it x space characters or one tab character) and then have the caret remain in the line between the two characters it originally was between. But hey, I could be wrong.
-
@Stefane-Guevremont
@guy038
@Alan-Kilborn
@PeterJones
@Michael-Vincent said in Change Indent Keyboard Shortcut?:Assume you may want the reverse as well.
Not to beat a dead horse, but I updated my NppExec scripts. I liked @PeterJones MACRO, but like my scripts, if I had multiple lines selected, they only operated on the line where the cursor anchor point was. However, with multiple lines selected and the cursor in the middle of one of them, the Edit => Indent => menu options work as expected - they only don’t work when you have a single line and the cursor in the middle.
I’ve tested lots of cases and I think the following scripts work as I expect.
-
With only a single line either highlighted or cursor anywhere in the line - INDENT or UNINDENT - keeping the cursor where it was IF it was in the middle of the text somewhere. Note if the cursor is in the preceding whitespace, indents are inserted at the far left margin, so the cursor shifts INDENT# of spaces with each insert.
-
If multiple lines are selected and the cursor is anywhere either fully selecting the lines or in the middle of them somewhere - INDENT or UNINDENT ALL LINES - cursor may not stay in the current position, rather the highlighting is fully completed to all lines. See example:
The scripts:
::lineindent NPP_CONSOLE keep SCI_SENDMSG SCI_GETSELECTIONSTART SCI_SENDMSG SCI_LINEFROMPOSITION $(MSG_RESULT) SET LOCAL START = $(MSG_RESULT) SCI_SENDMSG SCI_GETSELECTIONEND SCI_SENDMSG SCI_LINEFROMPOSITION $(MSG_RESULT) SET LOCAL END = $(MSG_RESULT) IF "$(START)"!="$(END)" THEN NPP_MENUCOMMAND Edit\Indent\Increase Line Indent ELSE SCI_SENDMSG SCI_GETCURRENTPOS SET LOCAL POS = $(MSG_RESULT) SCI_SENDMSG SCI_GETTABWIDTH SET LOCAL TAB = $(MSG_RESULT) SCI_SENDMSG SCI_GOTOLINE $(CURRENT_LINE) NPP_MENUCOMMAND Edit\Indent\Increase Line Indent SET LOCAL POS ~ $(POS) + $(TAB) SCI_SENDMSG SCI_GOTOPOS $(POS) ENDIF ::lineunindent NPP_CONSOLE keep SCI_SENDMSG SCI_GETSELECTIONSTART SCI_SENDMSG SCI_LINEFROMPOSITION $(MSG_RESULT) SET LOCAL START = $(MSG_RESULT) SCI_SENDMSG SCI_GETSELECTIONEND SCI_SENDMSG SCI_LINEFROMPOSITION $(MSG_RESULT) SET LOCAL END = $(MSG_RESULT) IF "$(START)"!="$(END)" THEN NPP_MENUCOMMAND Edit\Indent\Decrease Line Indent ELSE SCI_SENDMSG SCI_GETCURRENTPOS SET LOCAL POS = $(MSG_RESULT) SCI_SENDMSG SCI_GETTABWIDTH SET LOCAL TAB = $(MSG_RESULT) SCI_SENDMSG SCI_GOTOLINE $(CURRENT_LINE) NPP_MENUCOMMAND Edit\Indent\Decrease Line Indent SCI_SENDMSG SCI_GETLINEINDENTATION $(CURRENT_LINE) IF "$(MSG_RESULT)"!="0" THEN SET LOCAL POS ~ $(POS) - $(TAB) ENDIF SCI_SENDMSG SCI_GOTOPOS $(POS) ENDIF
I then use NppExec to add them to the Macro menu and then Shortcut Mapper to add the “Ctrl + Alt + [ and ]” shortcuts as @PeterJones did (note the OP’s request of “Ctrl + [ and ]” are already taken by SCI_PARAUP and SCI_PARADOWN respectively)
Cheers.
-
-
More horse beating:
So it sure seems odd.
These commands seem to have Tab and Shift+Tab tied to them:These commands aren’t in the Shortcut Mapper (that I could find, in the Edit category) so perhaps what is boxed in blue above is a hard-coding?
At run-time the corresponding code is not hit with the key presses but only invoking via the menu items:
case IDM_EDIT_INS_TAB: _pEditView->execute(SCI_TAB); break; case IDM_EDIT_RMV_TAB: _pEditView->execute(SCI_BACKTAB); break;
And, when invoking via menu, all they do is the same thing a Tab or Shift+Tab does via the normal Scintilla page mappings in Shortcut Mapper.
This certainly is odd.
But then onto the actual behavior.
This is all Scintilla behavior.
Someone in that project decided that this is how SCI_TAB and SCI_BACKTAB should work.
It seems very reasonable for the multiline selection case.
As discussed above, for the non multiline case, it is less so.Probably I am not adding new information, but the oddness of this situation made me do my own little investigation.
-
Interestingly, if you remove the Tab and Shift+Tab mappings on the Scintilla commands page in Shortcut Mapper, the indent stuff in the Edit menu now looks like this:
Note, no key mappings shown to the right of the commands.
So then it would seem that Tab and Shift+Tab are now totally unmapped.
BUT…trying them in an editor window I see that Tab still works. Curiouser and curiouser… Of course, maybe now it is at the point where it is just inserting a raw
\x09
. :-) -
@Alan-Kilborn said in Change Indent Keyboard Shortcut?:
Probably I am not adding new information, but the oddness of this situation made me do my own little investigation.
Thanks Alan - I surmised as much and it’s good to have someone confirm by actually looking at N++ source (I didn’t know where to begin to find it). I had assumed they were just calling the SCI_TAB and SCI_BACKTAB and looks like indeed that’s what they do.
You can find the SCI_TAB and SCI_BACKTAB in Shortcut Mapper as well as some shortcuts for Cut / Paste:
And notice the Edit menu for Cut / Paste:
The two (multiple) shortcuts are assigned, something you can’t do with Shortcut Mapper, so again, assuming N++ code is somehow adding the shortcuts based on Scintilla “defaults” or something?
So to sum up from our previous comments, it seems the Edit => Indent => menu items are not quite doing what they advertise. Not sure this is a bug, rather just a discrepancy between what the menu item implies and what it actually does.
To be honest, I never knew they were there but now that I do, I like the functionality I’ve implemented above with NppExec - it’ll come in real handy when editing Python where indents MATTER and i can’t just add or comment out loops (like with Perl) when troubleshooting - I need to also change indent levels. It’ll be easier now with my new shortcuts!
Cheers.
-
@Michael-Vincent said in Change Indent Keyboard Shortcut?:
You can find the SCI_TAB and SCI_BACKTAB in Shortcut Mapper
Yes, but if it was that alone we would be talking about “weirdness”. :-)
The two (multiple) shortcuts are assigned, something you can’t do with Shortcut Mapper…
You can assign multiple, but only for the Scintilla commands.
Edit => Indent => menu items are not quite doing what they advertise … I never knew they were there but now that I do, I like the functionality I’ve implemented above…
Yes, but it seems like standard Notepad++ could do better here. I mean, the stuff in the Edit -> Indent menu is rather pointless if it is just going to do the same thing as Tab or Shift+Tab.
Or maybe the idea is to give script writers a way to invoke these keys without calling a “key sender”?
I have no idea of the history on this.
But I do know that Increase Line Indent (and Decrease) surely seems misleading (unless you have a multiline selection).BTW, if you like source code references, here’s where in Scintilla the Tab keycombos get processed, and you can see the difference in the multiline versus non-multiline handling: https://github.com/notepad-plus-plus/notepad-plus-plus/blob/3b2d2bb300d01fe02091c837bc65f87d07cf6b8c/scintilla/src/Editor.cxx#L3953
-
I was browsing the code link in my post just above, and noticed that the Scintilla code handles multi-selections and rectangular selections as well as the more usual single selection.
I’m not much of a NppExec scripter, but I’m curious about what your scripts will do with those circumstances, so I may just try it out! :-)