I normally write code with tabs but many python libraries use spaces. Is there any way for Notepad++ to automatically detect how the file is formatted and have it automatically switch to using spaces when the file is already formatted that way?
BTW, I know there was already an SO question on how to change Notepad++’s tab format. But it would be better if it automatically changed based on the current file’s formatting.
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.pyscript will run.Find
startup.pyand edit it. On my PC its path isc:\Program Files\Notepad++\plugins\PythonScript\scripts\startup.py, add the following code tostartup.py.The function
buffer_active()is called every time when you switch tab, andguess_tab()checks whether the text is using tab indent or not. You can show the Python console to debug the code.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.