Notepad++ : Custom Syntax Highlighting for .txt files - syntax-highlighting

I keep code samples that I find useful as text files on my computer. I store them as txt files as opposed to the language in which they are written, so that they will open in Notepad++ instead of the editor (i.e. I don't want my c++ examples to open in an IDE, just Notepad).
Is there a way I can have Notepad++ apply appropriate syntax highlighting to the text file by reading a special code in the text file itself?
For example if I had some sql, the first line of the text file could read like this:
##Language=SQL
... my sql code properly highlighted as sql ...
Thanks in advance. I realize I could just choose the language after opening the file (i.e. Language > SQL), but it would be much more convenient if it could do it automatically.

No, it can't. You can choose it manually or use special file type extensions which you then associate with Notepad++ and tell it to highlight the files as the appropriate language.
For example, use .txtsql files for SQL, .txtcpp files for C++ and so on.

I ended up writing it myself:
You need the Python plugin
Add the code below to your startup.py file
Switch your Python Initialization setting from "LAZY" to "ATSTARTUP"
#if found determine the menu command and switch language in NPP
def switch_language_view(args):
notepad.activateBufferID(args["bufferID"])
lineone = editor.getLine(0)
if '##' in lineone:
lineone = lineone[lineone.rfind('##'):].replace('##', '')
lineone = "MENUCOMMAND." + lineone.upper()
try:
notepad.menuCommand( eval(lineone) )
except:
pass
#command to link notification
notepad.callback(switch_language_view, [NOTIFICATION.FILEOPENED])

I'd suggest giving them the proper file extensions, then import something like this into your registry:
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\*\shell\NotepadPlusPlus]
[HKEY_CLASSES_ROOT\*\shell\NotepadPlusPlus\command]
#="C:\\path\\to\\notepad++.exe \"%1\""
Then you can open your files in NP++ with a quick right-click, and NP++ will be able to auto-detect the right language based on the file extension.

Manual selection is a much simpler way. Store all the files in .txt format (irrespective of java or C or C++). Open the file in Notepad++ and select the corresponding language in the Menu. e.g. Language --> Java.

You could try some npp scripting,
python
lua
and/or hacking macros. you could make the script start conditionally, check your special string and select the language for you.

Related

Add context menu options for .hex files

I'm trying to add to Windows context menu of .hex files only a python script, but I can't find .hex extension in the "HKEY_CLASSES_ROOT". Currently, I have the script in the context menu for any kind of file but would like to limit it to only appearing when interacting with hex files.
What's the correct way of doing this?
Can this configuration be handled via a python script?
Follow the documentation, this stuff has not changed much since 1995.
HKCU\Software\Classes\.hex
""=myhextype
HKCU\Software\Classes\myhextype\shell\open\command
""="c:\path\myapp.exe" "%1"

How to create a .vm (velocity template file) from command line in Windows?

How to create a .vm (velocity template file) from command line in Windows
Based on you comment I assume what you really mean is how to made file which by default opens with some specific program you have. Here is several thing to be taken into account:
To create .vm file from command line you can use this question
By default windows doesn't show file extensions in explorer - so you won't actually see that it is .vm file in explorer. Reffer this guide to show file extensions.
There is such thing as file associations - e.g. default program to be use when you open file (for example double click). Also this affect icon shown for the file in explorer.
So to summarize - file created from command line(using method from referred question) have .vm extension. You don't see this cause your windows settings doesn't show extensions. You assume it is text file cause notepad++ icon shown for it as well as it opens with notepad++. That's a wrong assumption - simple you have notepad++ associated to open .vm files. If you want to use different program - you need either:
Change file associations (see link above).
Use open with option to open file with another program (right click menu option)
Use Open function from inside your program.

Maintain VBS colors for HTA in Notepad++

I've got an HTA that uses a VBS script.
When I save the file as .hta though all the format coloring for my VBS script turns white.
Does anyone know a way to make it so NPP will recognize both languages and format the style coloring accordingly? It's nuts that the bulk of my script has no color formatting simply because it's wrapped in something else.
As a .HTA can contain more than one script language (even at the same time) and Notepad++'s syntax highlighting seems to be triggered by extension only (no special syntax switching markup; evidence), I would tend to associate .HTA with HTML and move all code to external files (src attribute of the script tag).
What I wouldn't do (to much work for little gain and risk of failure), is to experiment with combining the Style Configuration for HTML and VBS (Javascript/JScript, Perl, ...) into a new Style and associate that with .HTA.
Addition:
My prefered editor - Komodo (since 5.2) - allows to specify the 'language' for individual files; so I can switch syntax highlighting for mixed language sources on the fly.
Settings -> Style configurator -> Language:VB/VBS -> User Ext.: hta
The accepted answer misses some great options (I'm not sure if either or both were available with the same capabilities in 2014)...
Both the free text editor NotePad3 and the lifetime subscription IDE VBSEdit properly syntax highlight HTML, VBScript, and JScript in a single HTA file.

How to create a md file under windows

I am using git bash under windows because I am having some troubles with Linux right now. I have to make a commit but I have no clue how to create a file with extension .md under the windows platform.
could you please tell me?
Thanks
You doing the Coursera data science course as well? :)
Syntax is:
touch filename.md
Good page I have bookmarked:
http://www.typecastexception.com/post/2012/09/08/Basic-Git-Command-Line-Reference-for-Windows-Users.aspx
An .md (short for MarkDown) is just a text file with some fancy formatting options, like making text between two sets of ** bold (GitHub, e.g., has a pretty comprehensive guide).
You can use any text editor, even Notepad, to create such a file.

Pasting diff output into Microsoft Outlook with syntax highlighting

How can I copy diff output (diff old-version.cpp new-version.cpp) into an Outlook email so I can send it to other people with syntax highlighting?
I'd either like to pipe diff output to a program that will copy it to the clipboard with formatting (p4 diff file.cpp | rtfpatch) or have a plugin for Outlook that lets me select some text, click a button, and it gets colorized.
I use Windows (XP and Vista), Perforce, Visual Studio, Beyond Compare 3, Outlook 2007. Anything using a combination of those tools would work great (I'm not looking to change my main diff program, etc...).
You can use Beyond Compare's "Text Compare Report" command in the Session menu to do this. Use the "Interleaved" layout style, the "HTML Report" output style and the "Copy to Clipboard" command and it will copy it to the clipboard as colored HTML. I don't have Outlook to test with, but it certainly works pasting it into Word.
Another decent solution I've found is a vim plugin. cliphtml.vim gives you the :ClipHtml ex command that will copy the whole file or the selected region to the clipboard with vim's highlighting.
Requires python.
Many editors have the ability to export syntax-highlighted files as HTML. From there, you can paste the HTML into Outlook. For example, to export a file to HTML in Vim, use :TOhtml.
This Visual Studio addon offers the "export to HTML" functionality as well. It's worth giving it a try.
To paste the html into into outlook you should try an past it into th source of the msg.
Right click the body of the HTML message and the select View source, then past your html into that.
The other way would be to script it in a batch file using and set the Message html body to equal your html text and send. There are quite a few examples of sending email via script on stackoverflow. There are a number of ways to do it depending what you have installed etc. one example is
Send mail from a Windows script
using CDO
I figured out a solution to make a batch file that diffs files from Perforce using the p4diff.exe program.
The problem with it is p4diff outputs the whole file, not only the changed sections (I'd also prefer unified diff). Also, diffing specific revisions requires calling rtfdiff from command line (the custom tool just diffs against HEAD).
p4v custom tool definition (write this to tool.xml and then import it in p4v's Manage Custom Tools menu):
<CustomToolDef>
<Definition>
<Name>RTF Diff</Name>
<Command>c:\scripts\rtfdiff.bat</Command>
<Arguments>%f</Arguments>
</Definition>
<AddToContext>true</AddToContext>
</CustomToolDef>
where rtfdiff.bat is
:: Use p4diff to get copy-pasteable diff output.
:: setlocal so we use the default after script terminates
setlocal
set P4DIFF=c:\Perforce\p4diff.exe
:: Diff all inputs to allow multiple revisions (must be in increasing order)
p4 diff %*
That will let you right click on a file and select "RTF Diff" or call rtfdiff.bat via command line (rtfdiff.bat file.txt#1 file.txt#2).

Resources