richard_ba
richard_ba

Reputation: 13

How to insert code with shortcuts with notepad++?

I am trying to use notepad++ as a TeX editor, as such I would like to add custom shortcuts which would insert code or even surround with a selected block, however the snippet plugins don't offer custom shortcut AFAIK

I would like to press control + i and it would insert \textit{}

Is it possible to achieve?

Upvotes: 1

Views: 4305

Answers (2)

Prahlad Yeri
Prahlad Yeri

Reputation: 3653

A bit late to answer this but what I usually do is record a Macro. For example, click the Macro => Start Recording menu option and then type the following:

def main():
    pass

if __name__ == "__main__":
    main()

After that, click Macro => Stop Recording. Now you have successfully created a basic snippet which is used in most python scripts! To test, simply create a new file and play the recorded macro by pressing Ctrl+Shift+P. To save this recorded macro permanently and assign a shortcut to it, click Menu => Save Current Recorded Macro.

You can do all kinds of wonderful things with macros, creating code snippets is just one application.

Upvotes: 3

Lars Fischer
Lars Fischer

Reputation: 10129

If the Snippet Plugin is not working for you I can describe a very laborous way using the NppExec Plugin. For each Shortcut you would create a new NppExec script and then assign this script a shortcut.

  1. install NppExec Plugin (e.g. through PluginManager)
  2. select Plugins -> NppExec-> Execute...
  3. enter these lines and save it as e.g. Insert_TextIt

    NPP_CONSOLE OFF
    SCI_SENDMSG SCI_INSERTTEXT -1 "\textit{}"
    SCI_SENDMSG SCI_WORDRIGHT
    SCI_SENDMSG SCI_WORDRIGHT
    SCI_SENDMSG SCI_CHARRIGHT
    
  4. Now select Plugins -> NppExec -> Advanced Options ...

  5. Chose a new Menu Item name in the lower left, e.g. TextIt and select the Insert_TextIt script in the checkbox Associated script
  6. click Add/Modify
  7. Alas you have to restart Notepad++, before the new Menu Item TextIt is available
  8. After restart of Notepad++, select Settings -> Shortcut Mapper ...
  9. on the "tab" Plugin Commands you will find the Menu Item TextIt, it is probably one of the last dozend entries in the list.
  10. select it and assign a new shortcut to it.

The last three commands SCI_WORDRIGHT and SCI_CHARRIGHT position the cursor inside the braces. For different text snippets you might have to experiment with the cursor position. See Scintilla Documentation for more possible commands.

Upvotes: 1

Related Questions