Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manage code snippets in Notepad++

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

like image 855
MohamedKadri Avatar asked Feb 28 '11 20:02

MohamedKadri


People also ask

Where can I store my code snippets?

There are various ways you can store a code snippet, either by keeping it in a GitHub repository or can use a smart code snippet manager, like Codiga's Coding Assistant. Coding Assistant allows you to store and reuse code snippets.

How do I set up code snippets?

With a code file open in the editor, choose Snippets > Insert Snippet from the right-click menu, then My Code Snippets. You should see a snippet named Square Root. Double-click it. The snippet code is inserted in the code file.


2 Answers

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.

like image 128
Francisco Alvarado Avatar answered Oct 17 '22 08:10

Francisco Alvarado


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 
like image 34
u8it Avatar answered Oct 17 '22 09:10

u8it