Code Snippets

From Notepad++ Wiki
Jump to: navigation, search
Code Snippets - How to build and use personal code libraries


If you want to create quick and easy ways of inserting code snippets Notepad++ has this covered through the QuickText plugin. The following example is for adding a QuickText shortcut for PHP but it is very similar for most other language options.

It all starts with going into the Options for QuickText. QuickTextMenu.png

  1. Highlight PHP on the left hand side.
  2. In the Tag Name field you place the name of the shortcut you want. I will use the example: php1
  3. Type something like the following in the Substitution Text box:
     <?php
     include('myincludefile.php');
     \$myvar = '$';
     ?>
    
  4. Then click the Add/Modify button. (This is very important). Add-php1.gif
  5. Now click OK to return to Notepad++.
  6. Make sure you are working in PHP (in the menu Language > PHP).
  7. Type php1 into your notepad++ and then press CTRL+Enter.
  8. This should make the little code snippet appear that we typed above.

You will note that the $ symbol is used in QuickText to place the cursor where required so you can just start typing away if you always type in a certain place. You have to excape a $ symbol by usinbg \$ to get it to actually show int the text. In the code example above the cursor will sit in between the two single quotes.

QuickText saves me so much time it is not funny! Common things like if..else statements or functions etc are easily programmed into QuickText.

A slightly more advanced feature of QuickText is the fact that it can have multiple cursor points.

For example if you used the following code snippet.

if ($)
    {
    $
    }
else
    {
    $
    }

You would firstly see the cursor in between the left and right () parenthesis after the start of the IF statement.

Now you would type in some kind of validation check in PHP :

if ($myvar == 10)
    {
 
    }
else
    {
 
    }

Now if you press CTRL+Enter again the cursor will move down to where the next $ symbol is in the code snippet. In this example between the first two {} parenthesis.

Very cool!