December 7, 2014

Opening files in Notepad++ from Cygwin

Notepad++ is an improved text editor instead of standard Vim in the command line, and especially improves upon using classic Notepad. It is superior, even if only for the syntax highlighting making things more readable. This is an alias function that seeks to implement a GUI-based text editor into Cygwin shell, and to run it as a daemon in the background, instead of seizing control over the terminal window for as long as the editor is open.

np ()
{
/cygdrive/c/Program\ Files/Notepad++/notepad++.exe `cygpath.exe -w "$*"` &
}

Note that it needs to be a function, and not a vanilla alias, because aliases don't allow interpolated arguments. What this function does is open the executable Notepad++ file, then takes a path argument that has been converted from a Cygwin path to a Windows path. $* stores the aformentioned arguments that were entered on the command line, and the & lets the process run in the background. This lets you use Notepad++ from Cygwin without having the shell wait for an exit code. See here for bash scripting info.

Reload the shell:
. ~/.bashrc

Now we can open any text file by passing the filename as an argument along with our new alias function. Instead of using vim or another editor we can simply use Notepad++ like so:
np ~/mycooltextfile.txt

You can even create text files this way - by specifying a filename that doesn't exist yet, it will be created for you!

No comments :

Post a Comment