2

I want to run a linter on my source code when I save a file (CTRL-S). Is it possible to run an external command on a file when it is saved?

1 Answer 1

4

As far as I am aware, in the strictest sense, there is no direct option run a program directly after saving. That said, it's likely you can get (functionally) similar behavior when pressing Ctrl+S with the steps below...

Save and Execute With Ctrl + S

Install NppExec

Normally, a regular Notepad++ macro combined with an item from the Run menu would likely be the simplest route to the desired behavior. However, commands from the Run menu don't currently seem to be allowed as macro actions by default.

To bypass this issue, we need a plugin called NppExec. This plugin allows us to write moderately improved macros (command scripts) for running programs from Notepad++. Particularly, it allows us to combine saving a document and running a program (which normally isn't possible, as noted).

To install NppExec:

  • Open the Plugin Manager (PluginsPlugin ManagerShow Plugin Manager).

  • Find NppExec in the plugin list and mark it.

  • Click Install.

After installation, you will need to restart Notepad++ as indicated.

Create A Command Script

Once Notepad++ has been restarted, open the PluginsNppExecExecute... window ( F6 ). In the text field, place the commands you wish to execute (in order) on separate lines:

NPP_SAVE
# ... Another command here ...
# ... Another command here ...

As a small aside, it isn't exactly clear to me what you wish to do with the file (regarding your linting process), so my apologies that the example above isn't more complete.


Note that each # ... Another command here ... is simply a placeholder for this example. Importantly, # signs do not actually represent comments in NppExec... this is just my shorthand. =)


Available Commands

Given that we know we wish to save first, we can use NPP_SAVE to start the command script. NPP_SAVE is an NppExec-specific command which tells Notepad++ to save the current document.

In general, for the remaining commands, you will likely either invoke those commands directly (assuming they are NppExec commands or items registered at the command line, whose output matters little) or use e.g. NPP_RUN cmd /k somecommand.exe (or other variations on NPP_RUN cmd) to spawn a new cmd window (which can often be preferable).

Anything that can be run from the command line (including batch files and scripts) are likely viable options for your commands. Notepad++ environment variables are also available. The most common one you are likely to use is $(FULL_CURRENT_PATH) (best referenced with double quotes i.e. "$(FULL_CURRENT_PATH)"), which gives the full Windows path to the saved version of your current document.

It's worth mentioning that, similar to Run in the normal Run... dialog, OK in the Execute... window will perform your commands immediately. This allows you to test their functionality prior to saving them. Likewise, the Command(s) field is persistent between runs.

Once you are happy with the command script, you will want to choose Save..., give the script an appropriate name and select Save again to keep it for later use. Close the Execute... window when finished.

Adding The Command Script To A Menu

We can now add our NppExec command script to a Notepad++ menu so it can be assigned a shortcut:

  • Go to PluginsNppExecAdvanced Options....

  • Select the command script in the Associated script: dropdown menu in the lower left.

  • Click Add/Modify to add it to the Menu items field just above.

  • Optionally, mark the checkbox next to Place to the Macros submenu to add the command script to the main Macro menu. Otherwise, it will appear as an option under PluginsNppExec.

  • Click OK to finish.

Restart Notepad++ as indicated and verify that the command script now appears as an menu entry at the bottom of either the Macro menu or PluginsNppExec.

Adding Hotkeys To The Command Script

Finally, to reassign Ctrl+S:

  • Open SettingsShortcut Mapper....

  • On the Main menu tab, highlight the entry for "Save". Click Clear at the bottom of the window to clear the hotkey settings for that item ( Ctrl+S ).

  • Select the Plugin commands tab. Scroll down and highlight the entry for your NppExec command script (identified by name). It will likely be the last item in the block for the NppExec.dll entries.

  • Click Modify and set the appropriate hotkey options (mark Ctrl and choose S from the dropdown).

  • Click OK to save the new shortcut settings and Close the Shortcut Mapper....

Your command script should now be trigged by Ctrl+S (no restart necessary).


You should note that, regardless of the hotkey combination you assign, the normal version of Save will still be associated with the File → Save menu item and toolbar icon (the single blue diskette).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.