This tutorial assumes:
- You are interested in running external commands from within Komodo.
- You are not running Windows 98 or ME. Running interactive
commands (especially
command.com ) on Win98/ME through the
Komodo Run Command feature can cause Komodo to hang. Therefore, the Run
Command Tutorial is not supported on Windows 98 or ME.
This tutorial introduces you to the Komodo Run Command feature. You will
learn how to run simple and complex custom commands (such as grep ,
make , and perl ); use these commands to process and
analyze files; save commands to run with a single keystroke; and use commands to
make Komodo a more powerful editor. In this tutorial you will:
- Run simple commands
using the Komodo Run Command feature.
- Use advanced command options
to control how and where a command is run.
- Save commands in the
Toolbox and assign keyboard shortcuts.
- Use command shortcuts
to customize commands for reuse.
- Use command query codes
to have your commands prompt you for information before running.
- Parse command output
into a list of results by specifying a regular expression.
From the File menu, click Open|Project
and navigate to the runcmd_tutorial.kpf project file on your file
system (<install dir>\Komodo x.x\samples\runcmd_tutorials in Windows and
<install dir>/Komodo x.x/samples/runcmd_tutorials in Unix). All
files included in the tutorial project are displayed on the
Projects tab in the Left Pane.
The Komodo Run Command feature offers another way to run commands that
would otherwise be run on the system command line. This section starts with a
simple echo command.
- Select Tools|Run Command to open the Run Command dialog box.

- In the Run field, enter
echo Hello World .
- Click Run. The results are displayed
on the Command Output tab.
Output from commands is displayed on the Command Output
tab.
Use the Command Output tab to interact with commands;
if the command accepts input, enter it directly into the command on the
Command Output tab. The Command Output tab has
the following features:
- Output written to
stderr (standard error output) is
displayed in red at the top of the Command Output tab.
- To terminate a running command, click the
button in the
upper right-hand corner of the tab.
- Many keyboard shortcuts available in the Komodo editor can also be
executed on the Command Output tab. For example,
'Ctrl'+'Shift'+'8' displays white space and 'Ctrl'+'Shift'+'7' displays
line endings (if the default
key binding
scheme is in effect).
The Toggle Raw/Parsed Output View button
is discussed in the Parsing Command Output section of
this tutorial.
Insert command output into a document using the
Insert output option.
- On the
Projects tab, double-click the file play.txt. The file opens
in the
Editor Pane; a tab at the top of the pane displays its name.
- Select Tools|Run Command.
- In the Run field, enter the command
dir
(on Windows) or ls -al (on Linux).
- Select the Insert output check box, and then click
Run. The contents of Komodo's current directory are inserted
into play.txt.
The Pass selection as input option passes selected text to
the specified command. Use this option together with the
Insert output option to filter selected regions of a
document.
- Open play.txt from the Run Command tutorial project (if it is
not already open).
- Select all six lines containing the word
frog .
- Select Tools|Run Command.
- In the Run field, enter the command
sort (on Windows) or sort -n (on Linux).
Note that the Pass selection as input and
Insert output options are selected automatically.
If one or more lines are selected in a document, the Run Command expects
to filter the selected lines.
- Click Run to sort the list of frogs.
Use the grep command line utility to filter lines of
text. Use grep to filter out all but the red frogs from the list.
This tutorial assumes the grep utility is installed on your
system and is in your system's PATH. Grep is a Linux utility that searches for
text and characters in files. Windows operating system users may not have a
grep installation. There are a number of free versions available on
the Web. Search using the keywords grep for Windows .
- Open play.txt from the Run Command tutorial project (if it
is not already open).
- Select the "5 red frogs" and "6 green frogs" lines.
- Select Tools|Run Command.
- In the Run field, enter the command
grep red .
- Click Run to remove all but the red frogs.
Clicking the More button in the Run Command dialog box
reveals a number of advanced options.

To set the current working directory for a command:
- Select Tools|Run Command. Click More to
display Advanced Options.
- In the Run field, enter the command:
dir
(on Windows), or ls -al (on Linux).
- In the Start in field, enter C:\ (on Windows),
or /home (on Linux).
- Click Run to generate a C:\ directory listing.
Specify which environment variables to set for a command. For example, use
this feature for setting PERL5LIB or PYTHONPATH when
running Perl or Python scripts.
- Select Tools|Run Command.
- In the Run field, enter the command:
set .
- Click New... to add a new environment variable. For
the variable name, enter:
PERL5LIB
- Click Add Path... to choose a value for
PERL5LIB
(the actual value you choose does not matter for this example). Click
OK.
- Click Run to display all environment variables. Scroll
through the results on the Command Output tab until the
PERL5LIB setting is located.
Run GUI programs outside of the Command Output tab by changing
the Run in option to No Console.
- Select Tools|Run Command.
- In the Run field, enter the command:
mozilla
If the Mozilla browser is not installed on your system, choose another GUI
application to run. For example, on Windows, try running either the
iexplore or notepad command.
- From the Run in drop-down list, select
No Console (GUI Application).
- Click Run to open the GUI application rather then the
Command Output tab.
To run commands in a new console window:
- Select Tools|Run Command.
- In the Run field, enter the command:
dir
- From the Run in drop-down list, select
New Console.
- Click Run to execute the command and open a new console
window.
Save frequently used commands for quick access and reuse.
Select Tools|Recent Commands to rerun recently run
commands.

The Run Command dialog box contains an option for saving commands in the
Toolbox for reuse. A command saved in the Toolbox is indicated with the
icon.

- Select Tools|Run Command.
- In the Run field, enter the command:
echo Hello World
- Select the Add to Toolbox check box.
- Click Run. Notice that a command named
echo Hello World is added to the Toolbox.
- Double-click the
icon
next to echo Hello World to rerun the command.
Commands can also be stored in a Komodo Project.

For example, the Run Command Tutorial project includes a Command to
echo "Hello World".
Consider adding commands to your projects to run make or other
command line tools. There are two ways to add commands to a project:
- Right click on a project and select Add New Command....
- Drag and drop a command from the Toolbox tab or another open project
onto the Projects tab.
Edit command properties in the Command Properties dialog box.

To open this dialog box, right click on any saved command and select
Properties.
Run Command shortcuts insert interpolation codes for
filenames, directory names, paths and other arguments into commands as
variables. This creates commands that are more generic and useful. Enter command
shortcuts in the Run and Start in fields, or select them from the drop-down
lists to the right of the Run and Start in fields. Windows users should enclose
all Komodo shortcuts (with the exception of %(browser) ) in double
quotation marks to ensure that spaces in filenames or file paths are interpreted
correctly.
Click the arrow button to the right of the Run field to
view a list of Run Command shortcuts.

The string %F in a command expands the full path of the
current file.
- On the Projects tab, double-click the file
play.txt. The file opens in the Editor Pane; a tab at the top
of the pane displays its name.
- Select Tools|Run Command.
- In the Run field, enter the command:
echo "%F"
- Click Run.
Change the current file status from "writable" to "read-only".
- Open play.txt (if it is not already open).
- Select Tools|Run Command.
- In the Run field, enter the command:
attrib +R "%F"
on Windows, or:
chmod u+w "%F"
on Linux.
- Click Run. The result is displayed at the top of the
Command Output tab.
To open a current HTML file in a Web browser, combine %F with the
%(browser) shortcut.
- On the Projects tab, double-click the file
index.html.
- Select Tools|Run Command.
- Click the arrow to the right the Run field to display the
shortcuts drop-down list. Select %browser, press the space bar,
and then select %F. Enclose the
%F in double
quotation marks.
- From the Run in drop-down menu, select
No Console (GUI Application).
- Click Run.
The %s, %S, %w and %W codes insert current selections, or the current
word under the cursor, into commands. This shortcut helps when running
utilities like grep , or for searching the Web.
- On the Projects tab, double-click the file
index.html.
- Position the cursor over the word "PHP" in index.html.
- Select Tools|Run Command.
- In the Run field, enter the command:
%(browser) http://www.google.com/search?q="%W" .
- Select the Add to Toolbox check box to save this command.
- Click Run to search for "PHP" with Google.
Now that you have searched for a word or selection in Google,
try the following shortcut to search for PHP methods.
- Open index.html.
- Select the text
mysql_info methods in the file.
- Select Tools|Run Command.
- In the Run field, enter the command "
%(browser) http://www.php.net/manual-lookup.php?pattern=%S ".
- Select the Add to Toolbox check box to save this command.
- Click Run to search
mysql_info methods
in PHP's online manual.
These two commands are built into Komodo. 'Ctrl'+'F1' (if the default
key binding
scheme is in effect) starts a Google search for the current selection.
'Shift'+'F1' in a Perl, Python or PHP file starts a help
search appropriate for that language. Customize searches in the Preferences
dialog box (Edit|Preferences|Language Help).
Run commands from the directory where the current file is stored, rather then
the current directory. For example, use the command %(perl) "%F"
to run the current file with a configured Perl interpreter.
- On the Projects tab, double-click the file
hello.pl.
- Select Tools|Run Command.
- In the Run field, enter the command:
%(perl) "%F"
- In the Start in field, enter:
"%D"
- Click Run.
This example assumes a perl interpreter is configured on your system.
If a perl interpreter is not configured (the required file is perl.exe), an
error message displays at the top of the Command Output tab.
Alternatively, run the command dir (Windows) or ls (Linux)
to display a list of files and folders beneath the current directory.
Run Command query shortcuts prompt for command input data via a dialog box.
These queries can be configured with default values and/or prompt the user if no
value could be determined automatically (e.g. a command to search Google for the
current selection that prompts for a search term if nothing is selected).
The %(ask) shortcut always prompts the user for data. Other
shortcuts can use the orask modifier to prompt the user if no valid
value could be determined.
Windows users should enclose all Komodo shortcuts (with the exception of
%(browser) ) in double quotation marks. This is necessary to ensure
that any spaces in filenames or file paths are interpreted correctly.
The full syntax of the %(ask) shortcut is:
"%(ask[:NAME:[DEFAULT]])"
where NAME is an optional name to use when prompting
in the dialog box and DEFAULT is an optional default value to
place in the dialog box.
For example:
- Select Tools|Run Command.
- In the Run field, enter the command:
echo Your favorite number is "%(ask)"
- Click the Run button to run the command. The
Interpolation Query dialog box is displayed.

- Enter your favorite number and click OK to finish
running the command.
Refine this shortcut by adding a more meaningful name than "Value 0" and a
more appropriate default value.
- Select Tools|Run Command.
- In the Run field, enter the command:
echo Your favorite number is "%(ask:Fav Number:42)"
- Click the Run button to run the command. The Interpolation Query dialog box will now
look like this:

- If your favorite number does not happen to be 42, enter a different
number and click OK to finish running the command.
Any Run Command shortcut can be modified to prompt the user for a value if one
cannot be determined automatically. The full syntax of the modified shortcut is:
"%(SHORTCUT:orask[:NAME])"
where NAME is an optional name to use when prompting in
the dialog box.
In the previous step we created a shortcut
to search for the selected word on Google with the command:
%(browser) http://www.google.com/search?q="%W"
However, if nothing has been selected and there is no word under the cursor,
the command fails. In this case, it would be better if the command prompted
you for a search term.
- Be sure your cursor is not positioned over a word.
- Select Tools|Run Command.
- In the Run field, enter the command:
%(browser) http://www.google.com/search?q="%(W:orask:Search for)"
- Click Run. The Interpolation Query dialog box
prompts for a search term.
Use Run Commands to specify a regular expression to parse
filename and line number information from lines of output. The parsed results are
displayed in a table, to quickly identify the desired file.
Explore this usage by creating a "Find in Files" command later in this section.
To parse command output, specify a regular expression. Each line of
output from the command sent to the Command Output tab is
also passed through the regular expression. If the regular expression matches,
an entry is added to the list of parsed results.
Python's regular expression syntax is used to parse command output. Its
regular expression syntax is largely identical to Perl's except for one relevant
exception: named groups. Named groups are covered briefly in this tutorial
because they are important for effectively parsing command output. For a more
thorough reference, visit
python.org.
Komodo Tip: Use the
Komodo Rx Toolkit
to build, edit, or test regular expressions. New to regular
expressions? The
Regular Expressions Primer is a tutorial for those wanting to
learn more about regex syntax. |
To parse the following line of output generated by running grep:
hello.pl:5:print "Hello, frogs!\n";
Output lines are of the form:
<file>:<line>:<content>
An appropriate regular expression to match this line could be:
(.+?):(\d+):(.*)
However, when parsing this line, Komodo requires you specify which group
(i.e., which parenthesized section) is a filename, which is a line number,
and which is content. This is where Python's named groups come in. Using
Python's regular expression syntax, for example,
(?P<file>.+?) instead of just (.+?)
to assign a name to whatever matches .+? .
When parsing output from the Run Command, Komodo searches for the names
file, line, column and content to determine
which part of a matched output line to put into which field in its table of
results. Note that because a column is not included in
this particular line of output, Komodo does not assign a column name.
The regular expression can be extended to:
(?P<file>.+?):(?P<line>\d+):(?P<
content>.*)
When parsing the line above, Komodo determines that hello.pl
is the file, 5 is the line and print
"Hello, frogs!\n"; is the content. Click the link below to see
the result highlighted in the parsed output.

Create a "Find in Files" command using all information presented in this
tutorial.
- On the Projects tab, double-click the file
hello.pl.
- Position the cursor over the word frogs.
- Select Tools|Run Command.
- On Windows, enter the command:
findstr /s /n /c:"%(w:orask:Search Term)" "%(ask:File Pattern:*.*)"
Or on Linux enter the command:
find . -name "%(ask:File Pattern:*)" | xargs -l grep -nH "%(w:orask:Search Term)"
Note that findstr is a Windows command line utility that searches
for strings in files.
- Select the Add to Toolbox check box to save this command.
- In the Start in field, enter:
%(ask:Start Directory:%D)
(When the command is run, Komodo should prompt for the "Start Directory"
using the directory of the current file, or %D as the default
value).
- Select the Parse output with check box and enter:
^(?P<file>.+?):(?P<line>\d+):(?P<content>.*)$
as the regular expression with which to parse.
- Select the Show parsed output as a list check box.
- Click Run. The Interpolation Query dialog
box is displayed.

- Click OK to run findstr. A list of all
occurrences of "frogs" in the files of the Run Command tutorial
project is displayed on the Command Output tab.

- Double-click on a parsed result to jump to a specific file and line.
- Click the
button to toggle back to the Command Output tab.

Alternatively, double-click on lines in the raw output view to
jump to that file and line.
|