If you install the "Python Script" plugin for Notepad++, you can write code to automatically switch between tabs and spaces.
Here's how:
In the menu: Plugins -> Python Script -> Configuration, and set Initialization to ATSTARTUP. When Notepad++ starts, the startup.py
script will run.
Find startup.py
and edit it. On my PC its path is c:\Program Files\Notepad++\plugins\PythonScript\scripts\startup.py
, add the following code to startup.py
.
The function buffer_active()
is called every time when you switch tab, and guess_tab()
checks whether the text is using tab indent or not. You can show the Python console to debug the code.
def guess_tab(text):
count = 0
for line in text.split("\n"):
indents = line[:len(line)-len(line.lstrip())]
if "\t" in indents:
count += 1
if count > 5:
return True
else:
return False
def buffer_active(arg):
editor.setBackSpaceUnIndents(True)
use_tab = guess_tab(editor.getText())
editor.setUseTabs(use_tab)
sys.stderr.write( "setUseTabs %s\n" % use_tab )
notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED])
notepad.callback(buffer_active, [NOTIFICATION.BUFFERACTIVATED])
This is only an example, feel free to make guess_tab()
better yourself, maybe use a global dict to cache the result and speedup the callback function.