A Few Tricks in Notepad++ a PHP-developer Should Know September 1, 2012Posted by Tournas Dimitrios in PHP.
My preferred IDE when building heavy-weight PHP applications is NetBeans . This tool is valuable when developing applications that are build on top of a Framework (like the Zend Framework) , though , NetBeans demands the .Net Framework , it isn’t portable and consumes noticeable resources from the computer where it is installed on .
When building light-weight applications , Notepad++ is probably the best alternative on the Windows platform . It’s not computer resource-intensive , tens of plugins can extend its functionality and most importantly , its size is lesser than 25MB (loaded with a “ton” of plugins and code-snippets) . Although I’ve been on the “road” for a few Years with Notepad++, at random intervals , new features and functionality immerge , just because I had not discovered yet . This article will skip the basics , it assumes that You already know how to copy/paste text or open a new document via the key-board . Certainly , it will not even mention how to install new plugins or how to configure basic functionality . Initially this article had the title of “advanced tricks and tips for Notepad++ ” , but just before publishing , its title was changed . You might already know these tips or probably You have discovered even more tricks , well done to you . Somehow , this tool gives me the impression that its functionality is infinite . Don’t hesitate to add your own tips into the comments section , so that other readers gets benefit from Your experience .
The following picture demonstrates my favorite Notepad++ set-up . Follow instructions of my previous article to configure the same “Theme” on Your machine .
Shortcuts for every-day use :
- Bookmarks : Hit Ctrl+F2 to set a bookmark on any line of code and then hit F2 to jump between bookmarks. Shift+F2 goes back a bookmark.
- Goto : Ctrl+G lets you jump to a line number quick . Nice feature if your script has hundreds lines of code .
- Find or Find/Replace of keywords : CTRL+H . The UI-panel of this functionality has a lot of customization (from Match-case to Regular expressions ) . The next paragraph demonstrates basic Regex functionality .
- Toggle Full-screen : F11 ( tabs of all open files are visible on the top ) , F12 (only current file) .
- Go to matching brace : Double clicking on a function’s curly-brace , highlights it’s complementary part . CTRL+B has the same functionality .
- Navigating on lines , deleting lines : “HOME” and “END” jumps the cursor to the beginning or to the end of the line respectively . CTRL+SHIFT+DEL deletes the whole line (first set cursor at the beginning of the line) .
- Duplicating of a whole line : CTRL+D . Setting the cursor at the end of any line and pressing this combination , duplicates the current line (the new line is positioned under the current line) .
- Finding variables and functions : Double-click on a variable / function-name should highlight all statements with the same name . Navigate UP/DOWN with CTRL+F3 or SHIFT+F3 .
- Copy current’s file path : Edit->Copy to Clipboard->Current Full Path to Clipboard
- Enable clickable links : Settings->Preferences->Misc->Enable clickable link
- Moving whole code-block up/down : First highlight the code-block (via drag/drop or via SHIFT+arrow-keys) , then move the whole block (up or down) with CTRL+SHIFT+arrow-keys .
Regular expressions into Notepad++ :
This concept is a thousand-page book by itself . Its origin is from PERL , but today almost all programming languages have this kind of functionality (advanced text manipulation) . For basic “find , find-and-replace , delete” functionality the basic replace-UI is most likely more than adequate (accessed via CTRL+H) . However , advanced matching criteria can only be achieved with Regex characters . The following table introduces some of them with practical examples . But before starting make sure that you change the Search Mode from Normal to Regular expression in the replace-UI (CTRL+H) .
|The meaning of Regex in Notepad++|
|[ ]||The square brackets can be used to match ONE of multiple characters. For instance, [abc] matches any of the characters a, b or c . Hence, gra[ae]y will match words like graey and graay, but not graaet or gry. Ranges can also be used, [a-z] is any lower case character and so on .|
|( )||The square brackets can be used to — group– ONE of multiple characters . For example , The sentence “We’re so used to being the big boys .“If you write (used) in the search and xyz in the ‘Replace with’ filed, the result will be “We’re so xyz to being the big boys .“|
|+||The plus sign matches 1 or more times . For example , Helo+s matches Heloos , Helooos , Heloooooos etc.|
|*||The asterisk or star sign matches 0 or more times. For example, Ma*m matches Mm , Mam , Maaam etc.|
|\d||Matches any single digit.|
|\s||Matches white spaces including tabs and line breaks.|
|\<||Matches the start of a word . For example, \<be matches bedroom but does not matches cube .|
|\>||Matches the end of a word . For example , be\> matches cube but does not matches bed .|
|\||The backslash is for escaping special characters . For example 4\*3 = 12 . See above , ass the asterisk has actually another meaning .|
|\s||Matches : white-space , tab or line-break .|
|.||The dot matches any character.|
|$||The dollar sign matches the end of a line|
|^||Be careful with this symbol , the Caret . It’s normal use is to match the start of a line . If it’s used inside the square brackets then it exclude characters . For example [^A-Za-z]digital which will exclude all alphabetic characters (lower and upper case ) from the beginning of the word digital . The word could be 2digital , &digital , /digital …… etc ….|
Running macro’s into Notepad++ :
What are macro’s ? : In simple words , “No more text editing repetitive actions” .
Basically Notepad++ can quickly and conveniently record keys you are pressing and then repeat them (set number of times, in quick succession if necessary) . Steps are :
Hit Start Recording , Do what you need , Hit Stop Recording , Run once or multiple times or until file ends .
Note : When your macro involves moving around from line to line it is best to use keys like Home or End to be sure your cursor is really at the beginning / end of the line .
Notepad++ Encoding configuration :
If your applications are only targeted to visitors from Anglo-Saxon countries , most likely the only character encoding system you will use is the ANSI (which is the default on Notepad++) . All other countries should use encoding systems that support their alphabetical characters and punctuation marks . As a general rule , UTF-8 supports most of the needs for a global-scale text-document exchange (besides all , on the front , websites are nothing more than HTML text-files that use a specific encoding system to express the language of their audience) . Actually PHP doesn’t really support Unicode , but because of the way strings are saved you can actually use UTF-8 documents without problems if you save them in the proper format (encoding system) .
As a general rule :
- HTML code should be declared as UTF-8 (doc-type and via meta-tag
<meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″/> )
- Mysql Database : “MySQL charset: UTF-8 Unicode (utf8)” and “MySQL connection collation: utf8_general_ci”.
- Notepad++ has the “Encode UTF-8 without BOM” configuration . Via “Encoding” tab . Read this link , Display problems caused by the UTF-8 BOM .
Viewing two files side by side in Notepad++ (Multi-view) :
You can right-click on a tab and select “Move to Other View” , another option is to select “Clone to Other View” if you want to have the same file open more than once and see different parts of it . You can right-click the divider line and rotate the views to be either side-by-side or top-and-bottom .
A nice setting is : View -> Synchronise Vertical/Horizontal Scrolling , to keep the windows aligned that way if you wish .
Session Manager :
Sessions keep track of what files are opened in Notepad++ when working on a project . If the current project’s state (session) is saved before closing it , when you restore the session , all the files that you had open will be restored instantly allowing you to resume your work right where you left it off . Sessions also retain extra information like file positions and highlighter for the file .
By default , Notepad++ restores the last session that was active (before closing the editor) , though , this configuration can be changed by “Settings -> Preferences -> MISC -> Remember current session for next launch ” .
Important notice : Sessions do not backup files — This setting is activated from ” Settings -> Preferences -> Backup/Auto-Completion ” .
Storing the current session is simple as : File -> Save session and give it a name ( for later reference , a good choice would be to mirror the name of the current project ) . Restoring a session is even simpler : File -> Load session .
Session files are plain xml files with a .xml extension , but you can change that from Settings -> Preferences -> MISC -> Session file ext (look in the lower right corner) .
Yes , there is something still missing: the ability to see the contents of all session file as an Explorer-like tree view (NetBeans has this feature by default ) .
Run applications from within Notepad++ :
Notepad++ can be configured to start any external application via a custom Key-combination . For example :
- NetBeans could be started by clicking ALT+SHIFT+4 :
1) Go to Run ->Run
2)Navigate to the location where the .exe file of the applications is located . Most likely , it’s the “Program Files” directory . If you know the location , write it directly into the input field . Caution : If the path contains spaces (“Program Files” ) , enclose the whole path into double quotes . For example on my machine , it’s “C:\Program Files\NetBeans 7.1.2\bin\netbeans.exe”
3)Press “save” : Set a Name and a combination of buttons . Finally , press “cancel” .
4) Now , NetBeans will start-up , any time you press the combination of buttons (from previous step) .
- The current script could be viewed into a browser (the source code) .
A similar approach (as previous example) has to be followed . Just in step2 , pass some extra command-arguments . For example , open current file into the Chrome browser (display the source code)
“C:\Documents and Settings\a\Local Settings\Application Data\Google\Chrome\Application\chrome.exe” “$(FULL_CURRENT_PATH)”
Notice: The path and the command-argument are set into individual double-quotes and separated by a space !!!!!!
- The current script could be run via WAMP .
A similar approach (as the first example) has to be followed . Just in step2 ,
pass this extra command-argument .
“C:\Documents and Settings\a\Local Settings\Application Data\Google\Chrome\Application\chrome.exe” “localhost/testcode/$(FILE_NAME)”
Notice : The “localhost/testcode/” denotes that the script is located in WAMP’s public folder and one directory down : “C:/wamp/www/testcode/” .
Other command-arguments can be found on this link .
A few must-see plugins :
- SnippetPlus : 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 . My favorite plugin , over the last Years I have collected over 600 code-snippets . This plugin is only available from Sourceforge (can’t be done via Notepad’s Plugin-manager) . This excellent plugin deserves a separate article .
- SourceCookifier : This is a neat plugin that lets you jump to the definition of a function by hitting Ctrl+Shift+Enter . It also provides a nice sidebar with a breakdown of your code(functions , vars , Classes , methods etc.) . This excellent plugin deserves a separate article .
- NppExport : It is a true WYSIWYG exporter . It allows you not only to save your source code as a html/rtf file, but also to copy your source code in the clipboard in RTF/HTML format , so you can paste it into your word processor (MS Word, Abiword, openoffice.org Writer) to get the same visual effect . Installation can be done via Notepad’s Plugin-manager .
- DBGP Plugin : This PHP debugger , or XDebug , speaks the DBGP protocol . This plugin allows you to alter your Notepad++ into a PHP IDE . A simple workflow is as follows :
Web browser <==> Port 80 <==> Apache + PHP + Xdebug extension <==> Port 9000 <==> DBGp Plugin-notepad++
This excellent plugin deserves a separate article .
- Explorer : Open your file in Notepad++ quickly using Explorer plugin . It has two sections : Folder tree box(to see directories-sub directories) and File box. Using bottom filter box , You can filter files to find in large listing . Installation can be done via Notepad’s Plugin-manager .
- Compare : No need to install other app to compare files , You can do it easily with Notepad++ itself . Open two files you wish to compare . Click Plugins > Compare > Compare , you’ll get differences highlighted . To off compare screen and back to editor window , Plugins > Compare > Clear Results . Installation can be done via Notepad’s Plugin-manager .
I had to skip many concepts or detailed explanations , the article had to be kept into reasonable size . Future articles will go in more details on some of the aforementioned plugins . Happy Notepad-ing :) .