Saturday, October 4, 2008

Compile Source and Run programs from Notepad++


Notepad++ is, unarguably, one of the most popular text editing tools of the moment! It supports syntax highlighting of all imaginable programming languages. Best of all, it is open-source software which can be used for free :)

Notepad++ itself is intended to be a text editor. However, we can configure it so it can run some external tools such as loading a web browser with the current file, or compile a Java/C++ source files. This can be done with the help of Notepad++'s built-in environment variables.

For example, the typical command for running a Ruby program is

ruby "C:\path\to\my\file\myfile1.rb"


To run this command from Notepad++ and applies path of current file instead of "C:\path\to\my\file\myfile1.rb", you can goto Run menu -> Run ... and type the following command in the only text box in the dialog.

ruby "$(FULL_CURRENT_PATH)"
Now you can try to run this by click on the "Run!" button of the dialog. The $(FULL_CURRENT_PATH) is one of the environment variables available. The others are

The 5 environment variables contain:

FULL_CURRENT_PATH : E:\my Web\main\welcome.html
CURRENT_DIRECTORY : E:\my Web\main\
FILE_NAME : welcome.html
NAME_PART : welcome
EXT_PART : html

However, there is a problem left. After you click the "Run!" button, a black command-line window appeared and disappeared immediately so you can't see the result.

You can prevent the command-line window from automatically closing by using this command.

cmd /K ruby "$(FULL_CURRENT_PATH)"


Hope this helps :)

Related pages:

16 comments:

Mike said...

Hi,

Thank you so much for sharing this tip! I've been looking for so long for a way to open a ruby file directly from notepad++!

Ross said...

Thanks for the info - exactly what I needed!

encompass said...

Thanks for the post. The /K was new for me as I hadn't used windows since 95. (Linux user.) And when I was helping my boss learn python scripting he needed soemthing to keep his commandline open. :)

Kayote_80 said...

Thanks for the help! cheers.

Nick said...

Thanks for the post, I was really looking for a way to do this. Now I can use Notepad++ instead of SciTe.

Anonymous said...

Thank you so much.......exactly what I needed :)

--Asif

Anonymous said...

Heve you ever tried NppExec? Using this plaugin you can add sth like "Start ruby" to macros. It also has very useful console which is integrated with Notepad++.

Dreasconse said...

This is not working at all for me. First, cmd does not automatically start ruby, so have to run setrbvars.bat first. My current command is cmd /E:ON /K C:\Ruby191\bin\setrbvars.bat ruby $(FILE_NAME) which just isnt working. Anyone know how to fix?

.:: m3rLinEz ::. said...

@Dreasconse

I have little experience with Ruby, but perhaps the setrbvars.bat only adds Ruby to the %PATH% variable. How about adding Ruby bin to the path yourself and try the /K switch again?

Dreasconse said...

@.:: m3rLinEz ::.
Thank you. That worked.

Jon said...

Thanks! now I can see the results of my cpp programs on the output window instead of opening in a command prompt (w/c as you all know, disappears quickly) now, notepad++ has fully replaced my long programming friend: Crimson editor::Borland compiler combo...

TC Emp said...

You can also cd to the working directory (use /d to switch regardless of drive letter):

cmd /K cd /d "$(CURRENT_DIRECTORY)" && ruby "$(FULL_CURRENT_PATH)"

Anonymous said...

Thanks!

sai said...

what is dis ruby?
can i get da result for c++ prog.. name with some 'sample'

Factopedia said...

it didnt work for me instead notepad++ run command browse for the file and run it with instant and didnt let the screen appears for more than one second

Anonymous said...

Thankx Worked Great