Notepad++: automatic configuration of tabs vs. spaces

Notepad++ is a great text editor with lots of features out of the box, but I miss particularly one: the ability to use automatically tabs or spaces for indentation according to the content of an existing file when opening it.

Luckily enough, it’s easy to add this feature using the Python scripting plugin.

  • Install the Python scripting add on for Notepad++ with the plugin manager
  • Open C:\Program Files (x86)\Notepad++\plugins\PythonScript\scripts\startup.py
  • Append the code below at the bottom of the file
  • Save the file
  • Open Notepad++
  • Choose Plugins -> Python Script -> Configuration
  • Ensure Initialisation is set to ATSTARTUP and save
  • Restart Notepad++
from Npp import *

def indent_auto_detect(arg):
    for i in range(editor.getLineCount()):
        pos = editor.positionFromLine(i)
        indent = editor.getLineIndentPosition(i)-pos
        if indent > 0:
            if ord('\t') == editor.getCharAt(pos):
                console.write("Indentation: Tabs\n")
                editor.setUseTabs(True)
                return
            elif indent in [2, 3, 4, 8]:
                console.write("Indentation: %d spaces\n" % indent)
                editor.setUseTabs(False)
                editor.setIndent(indent)
                return

notepad.clearCallbacks([NOTIFICATION.BUFFERACTIVATED, NOTIFICATION.READY])
notepad.callback(indent_auto_detect, [NOTIFICATION.BUFFERACTIVATED])
notepad.callback(indent_auto_detect, [NOTIFICATION.READY])
console.write("Automatic indentation detection started\n")
indent_auto_detect(None)

The code comes from: https://gist.github.com/patstew/8dc8a4c0b816e2f33204e3e15cd5497e

Advertisements
This entry was posted in Uncategorized. Bookmark the permalink.

One Response to Notepad++: automatic configuration of tabs vs. spaces

  1. chantivlad says:

    And the code uses some good python bad practice: from module import *

    😉

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s