Macro to switch indentation
-
Is it possible to make a shortcut macro to switch between tab and space identation? I’ve tried the following:
<Macro name="Tab (Ctrl + E)" Ctrl="yes" Alt="no" Shift="no" Key="69"> <Action type="0" message="2124" wParam="0" lParam="1" sParam="" /> </Macro> <Macro name="Space (Ctrl + R)" Ctrl="yes" Alt="no" Shift="no" Key="82"> <Action type="0" message="2124" wParam="0" lParam="0" sParam="" /> </Macro>
*2124 is SCI_SETUSETABS(bool useTabs)
-
@elusiveorganization said:
Is it possible to make a shortcut macro to switch between tab and space identation
It should be… but I couldn’t make it work, either. I believe the
bool useTabs
argument should be the wParam, not the lParam, but it didn’t work whether I used yours or whether I moved the1
to wParam instead. I don’t know what’s wrong with that.<Macro name="Tab (Ctrl + E)" Ctrl="yes" Alt="no" Shift="no" Key="69"> <Action type="0" message="2124" wParam="1" lParam="0" sParam="" /> </Macro> <Macro name="Space (Ctrl + R)" Ctrl="yes" Alt="no" Shift="no" Key="82"> <Action type="0" message="2124" wParam="0" lParam="0" sParam="" /> </Macro>
However, if I use the NppExec plugin, I can send the 2124 Scintilla message and have it toggle the mode, as shown in this animation.
To make those into keyboard-executable scripts in NppExec:
- Plugins > NppExec > Execute
- Make the Command(s):
sci_sendmsg 2124 1
- Click Save…, name it
use-tabs
, and click Save - Change the
sci_sendmsg 2124 1
tosci_sendmsg 2124 0
, and save again asuse-spaces
- Cancel
- Make the Command(s):
- Plugins > NppExec > Advanced Options, in the Menu Item section:
- Name =
Use Tabs
, Associated Script =use-tabs
, Add/Modify - Name =
Use Spaces
, Associated Script =use-spaces
, Add/Modify - Okay twice, then restart Notepad++
- Name =
- At this point, Macro > Use Tabs or Macro > Use Spaces will toggle.
- To set the shortcut, Macro > Modify Shortcut / Delete Macro
- Change to Plugin Commands tab.
- If available, set Filter to
NppExec
, otherwise just scroll down until you find the NppExec commands - At the end of the NppExec commands should be Use Tabs and Use Spaces.
- Click Modify on each, and set to Ctrl+E and Ctrl+R, or whatever keystrokes you want
- Close the shortcuts dialog when don.
- Keyboard shortcuts should now work (assuming there wasn’t a conflict with the shortcuts; in v7.6.6 and newer, shortcut conflicts should be highlighted, even with filters active)
Good luck.
- Plugins > NppExec > Execute
-
Another option would be LuaScript. Edit the startup file, add the following, and restart.
npp.AddShortcut("Toggle Indentation", "Ctrl+E", function() editor.UseTabs = not editor.UseTabs end)