Manage code snippets in Notepad++
Asked Answered
F

3

43

Is there any way to manage code snippets with backup for the database and code highlighting in Notepad++?

Friesen answered 28/2, 2011 at 20:27 Comment(0)
A
25

There is a good plugin to manage code snippets:

SnippetPlus
.NET 3.5 Required! Code snippet and Surround With plugin for Notepad++. Write snippet name and replace it with real code or select some text and surround it with something like IF,TryCatch,Table,Div or whatever.Will give you hint if you don't remember the snippet name Note that the latest version may be shown, even though you have an older version installed. Reinstall to ensure you have the latest version.
Author: Rajesh Kumar
Homepage: http://sourceforge.net/projects/snippetplus/

You can install it from Notepad++ Plugin Manager, and activate dock by clicking the black m-like symbol in the toolbar; then select one of the bundled snippets and paste it to editor by double-clicking it.

For example, if you have a Visual Basic file and you need to include a try/catch block which contains an if-elseif-else structure, you must follow these steps indicated in the image:

SnippetPlus example

  1. Put your cursor where you want the new code snippet to be at, then activate dock from toolbar: this will open the docked dialog marked in yellow.
  2. Double-click the option ifelseif in the Snippets section of the dock: this will insert the code block marked in blue.
  3. Select all the code you want to be inside the try/catch block and Double-click the option TryCatch in the SurroundWith section of the dock: this will replace the selected code with all the code block marked in red.

If you want to modify existing snippets or include yours, use the GUI editor:
GUI Editor

Click the snippet you want to modify/delete: it will be shown at right. The active section (either Snippets or SurroundWith) is highlighted with a dark red background:

GUI Editor

You can add custom snippets too: just select the proper language and click any snippet from the section you want the new snippet to belong (either Snippets or SurroundWith), then write the name of the new snippet you want to create (e.g. sample): the button Modify will change to Add New
Add new snippet

You can insert snippets to documents also by using keyboard shortcut Ctrl+Shift+ENTER:

  1. You can write directly snippet name and then press keyboard shortcut, or
  2. You can invoke popup pressing keyboard shortcut, and then pressing keyboard shortcut again to replace snippet name with snippet content.

insert snippet by keyboard shortcut

Notes:

  • This plugin keeps snippets (including custom ones) between restarts.
  • Important Note: If you add/modify/delete snippets, only after a restart of Notepad++ the changes will be applied (including contents of lists at sections Snippets and SurroundWith). Be careful with this to not duplicate or add incomplete snippets.
  • To apply code highlighting for language you're working, just select it from the menu language as is done traditionally:
    code highlight
  • The code snippets shown on Ctrl+Shift+ENTER menu, depend on the language highlighting currently applied to the document. The only code snippets shown globally are whose belonging to category GENERAL.

I hope this information will be helpful for you.

Autogenesis answered 18/4, 2011 at 6:4 Comment(6)
I almost gave up on this plugin, but didn't thankfully since you mentioned "This plugin keeps snippets (including custom ones) between restarts. If you add/modify/delete snippets, only after a restart of Notepad++ the changes will be applied". I thought it was either broken or I did something wrong until I read that. Thanks for mentioning it.Addie
the fact you have to reload all the time sucks pretty bad! i don't know if he did all well, end if i have a lil change i allways have to restart npp. when i add a couple of snippets i can't see what i already added until i restart npp. seems like the programmer got lost of interesst finishing this plugin. its very useful but looks like im better whit putting all into a textfile ^^Dor
Even with reloading, this does not work. Usability of this plugin really sucks.Crawford
This plugin is pretty sucky. Add new snippet (well hidden) doesn't work at all.Anguilla
The author has been aware of the reloading bug for 4 years and counting too sourceforge.net/p/snippetplus/bugs/3/#ffb9Keilakeily
Please everyone pay attention to Notes :)Nor
T
1

This answer is not specifically about how to manage np++ code snippets, but this might be a good alternative since it looks like that plug-in doesn't work very well. I've found a very flexible system wide solution is to use AHK. You can then get snippet like functionality, plus a lot more, by customizing hotkeys and text expansion.

For instance, a one liner AHK script like this :*:123::123456789 will automatically expand typing "123" to "123456789", regardless of your current editor (np++ or VS, whatever). To limit this to specific applications, just add another line like this #IfWinActive, ahk_exe devenv.exe (Full example here).

More on AHK for plaintext


Simple AHK Script to Manage Code Snippets


I just whipped up a simple solution to manage a CodeSnippet database. A little more work and this could be a lot slicker.

What it does

Provides a code snippet list shown by hitting Ctrl+Alt+S. Double click a snippet and it will paste in your application. Snippets are simply a list of files and the contents of the file provide the code to the snippet.

How to setup it up

1.) Requires a folder named "CodeSnippets" under your MyDocuments directory. Create your code snippet files in this folder.

2.) Use AHK to run the following script. To "set and forget", have the script run on Windows startup. From any application in Windows, hit Ctrl+Alt+S to select a snippet.

#NoEnv

HotKey, ^!s, ShowList
return

ShowList:
Gui, Add, ListView, r20 w300 gMyListView, Name
Loop, %A_MyDocuments%\CodeSnippets\*.*
    LV_Add("", A_LoopFileName)

Gui, Show
return

MyListView:
if A_GuiEvent = DoubleClick
{
    LV_GetText(RowText, A_EventInfo)
    FileRead, Snippet, %A_MyDocuments%\CodeSnippets\%RowText%
    Clipboard := Snippet
    Gui, Destroy
    Send ^v
}
return

GuiClose:
Gui, Destroy
return
Teocalli answered 11/1, 2018 at 23:33 Comment(0)
S
1

There is also FingerText. It installed and ran fine from NPP's new Plugins Admin. If you give it a shot, you should probably go to Plugins | FingerText | Install Default Snippet Package to get a default set of snippets. It is vintage 2012, but still works. It expands snippets via the <TAB> key, a la TextMate, by default. It has expansion macros, such that $[![(key)DATE:yyyy/MM/dd]!] represents the current date for its insert date snippet. If you insert e.g. a hyperlink, it highlights the text part, so it will replace its placeholder for the text when you start typing, and then you can hit <TAB> and the URL placeholder will be highlighted. This is reminiscent of the way Emmet is supposed to work, as well. You can of course, add new and edit snippets.

Perhaps its greatest oversight is it doesn't break snippets up into categories; so you get just one long list for your snippets. Snippets are aware of file extensions, however. It doesn't seem to have autocompletion. Documentation can be found in its {NPP}\plugins\FingerText directory, in "README.rdoc".

It is evidently a win32 project, and thus would not require .NET 3.5. The developer indicates that it is in alpha stage; but it seems reasonably stable, and seems more mature than an alpha stage, IMHO. It is open source, and available from GitHub at "erinata/FingerText: Tab triggered snippet plugin for Notepad++". My guess is that if you want an x64 version, you'll have to compile it yourself; but there is a Visual Studio project.

Synonymous answered 29/4, 2019 at 12:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.