Language Selection

English French German Italian Portuguese Spanish

Script KATE to Automagically Compile/Execute Programs

Filed under
Software
HowTos


Script KATE to Automagically Compile/Execute Programs

By Gary Frankenbery



I teach high school computer science and programming, and I'm trying to transition my computer lab from MS-Windows only to MS-Windows/Linux dual boot.

For Computer Programming, my students first learn to program with Ruby. I try to keep it simple for these fledgeling programmers, so we have been using the Crimson Text Editor under Microsoft Windows to enter/edit the Ruby source code. The Crimson Text Editor makes it easy to configure a macro to execute whatever Ruby script is being edited: that is, the Crimson macro saves the file, then calls up the Ruby interpreter, and Ruby executes the script. Students accomplish this by simply by pressing the F5 function key while in the Crimson Text Editor.

So, I started wondering if I could configure the KDE Advanced Text Editor (KATE) under Linux to do the same thing with Ruby scripts? Yes, I know that KATE has an embedded terminal where one can activate the terminal window, click in it, and then type, for example,"ruby factorial.rb" and press the enter key, to execute the current Ruby script being edited in KATE. But, I wanted to have the current ruby script in the KATE editor to be both automatically saved, and then executed in a separate Konsole window, all triggered with the simple press of a shortcut key--just like Crimson under MS-Windows.

Could I do this? I've got at least one thing going against me--I'm not a bash shell programmer--I don't really know much shell scripting at all. However, I've got some things working for me--I am a programming teacher so I can program, bash scripting is well documented, and I can be very tenacious. So, I believe that with effort, persistance, and and lot of trial and error, I can figure out how to do this with KATE and Linux. And, so, I did. Then, I wanted to do more.

Since my more advanced computer programming students program use Java (not my personal choice--it's the language mandated by the College Board in the U.S.), I figured out how to configure KATE to automatically save and compile a Java program, and then I finally worked out a script to have KATE call up the Java Run Time Environment to execute the Java program. Obviously, other programming languages (Python comes to mind) can be accommodated as well by customizing these scripts (this, as teachers so often (annoyingly) say, is left as an exercise for the reader Smile).

KATE does permit you to embed scripts, so I'll display some KATE screenshots along the way, and explain how it's done. We'll start by configuring three scripts: Ruby Execute, Java Compile, and Java Execute.Then we'll look at how to configure shortcut keys to activate the scripts under KATE.

  1. With KATE on your screen, select Settings -> Configure Kate... from the
    menu.







  2. In the Configure Dialog box shown below, click External Tools in the left panel, then then click the New... button at the bottom.









  3. The Edit External Tool Dialog comes up (below). Here, we're going to configure KATE for Ruby Program Execution. Type in a description Label for the script (I used Ruby Execute). Then type in the script itself, with all the single apostrophes, double quotes, spaces, and symbols. Because this is tricky, you can copy the text on the next line, and paste it into the script box:


    cd "%directory" && konsole -e sh -c 'ruby "%filename"; printf "%s"
    "Press any key to exit . . . "; read $dummy'







    Since the KATE External Tool scripts like an executable name, just type konsole into the Executable: text box. Next, click the drop down list arrow by the
    Save: button, and select Current Document. Click the OK button. This window will
    close. Click OK again. You should now be back at the KATE main window.


  4. Now, let's configure the Java Compile script. Starting at the KATE main window, do steps 1 and 2 again. This time, edit the External Tool Dialog to look like the screen clip below. Again, the script code needs to be exact. Here it is so you can copy and paste, if necessary:



    cd "%directory" && konsole -e sh -c 'javac "%filename"; printf "%s" "Press any key to exit . . . "; read $dummy'








    Click the OK button, and at the next window, click OK again. You should be back at the KATE main window.

  5. Time to configure the last script to execute the Java program. At the KATE main window, do steps 1 and 2 above one more time. You should edit the External Tool Dialog to look like the clip below. Again, here's the scipt command which you can copy and paste:


    cd "%directory" &&  CF="%filename" konsole -e  sh -c 'java ${CF%.*}; printf  "%s" "Press any key to exit . . . "; read $dummy'








    Finally, we are done with the scripting part. Next we'll setup shortcut keys to launch our scripts.

  6. To get started, at the main KATE window, click the Settings->Configure Shortcuts menu choices as shown.








  7. You will see the Configure Shorcuts dialog box window shown below.



    Click the Java Compile script, click the button next to Custom (at the bottom), and press the key(s) you want to launch this script. You can see that I chose the F12 key to compile the current Java program. Click OK



    For the Java Execute script, I chose Shift-F12.




    For executing a Ruby program, I chose I chose Alt+R.



    So, after you configure the three shortcuts, you're done! You can now edit your programming source code in KATE, and quickly save, compile, and execute your Ruby and Java source code with these scripts simply by
    pressing the shortcut key(s).







Script Kate Article

By default, Debian derived Linux distros execute sh scripts using dash rather than bash. Because dash won't work correctly for the above Kate scripts, just substitute bash in place of sh in the above scripts.

Gary Frankenbery
January 4, 2009

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

More in Tux Machines

Red Hat News

  • Red Hat's Results Underscore its Growing Focus on OpenStack
    Late last week, Red Hat reported earnings per share of 55 cents on revenue of $600 million, beating estimates of 54 cents and $590 million, respectively. One thing that went unsaid across much of the coverage is that the company is in the midst of a major shift in its strategy toward OpenStack-based cloud computing, and it looks like service revenues and positive momentum from that effort are starting to arrive. "Our growth was driven in part by expanding our footprint with customers as we closed a record number of deals over $1 million, up approximately 60 percent year-over-year," Red Hat CEO Jim Whitehurst said during his company's earnings call. Seven of the top 30 deals had OpenStack in there, nine had RHEV," Whitehurst said. "We had three OpenStack deals alone that were over $1 million. So I think we're seeing really, really, really good traction there."
  • Red Hat targets $5-b revenue in five years
    Open-source technology firm Red Hat Inc, which hit the $2-billion revenue milestone two quarters ago, is looking to achieve $2.4 billion in FY 2017 and $5 billion in the next five years. The company is betting on India, its second largest operation outside the US, as one of the key growth engines to help achieve its aspirational revenue goal of $5 billion by 2021. “India is a bright spot for Red Hat for three reasons,” Rajesh Rege, Managing Director, Red Hat India, told BusinessLine.
  • Red Hat Announces Ansible Tower App for Splunk, Enabling Intelligence and Automation Enhancements
  • Red Hat’s (RHT) “Outperform” Rating Reiterated at Raymond James Financial Inc.
  • Red Hat Inc. (RHT) PT Raised to $89.00

pump.io Servers Adoption

  • Adopt a pump.io server
    As most of you know, E14N is no longer my main job, and I've been putting my personal time, energy, and money into keeping the pump network up and running. I haven't always done a good job, and some of the nodes have just fallen off the network. I'd like to ask people in the community to start taking over the maintenance and upkeep of these servers.
  • Prodromou: Adopt a pump.io server
    There are currently around 25 servers in the federated network initially started by Prodromou, which does not count other pump.io instances. He notes that one important exception is the identi.ca site, which is significantly larger than the rest, and which he would like to find a trusted non-profit organization to maintain.

Black Lab Linux 8 Beta 3 Released

The development team is pleased to announce the new Beta release of Black Lab Linux 8 – our latest OS offering to bring the best Linux desktop distribution currently on the market. This release moves the kernel and application set away from the prior LTS 14.04 base to the new 16.04 LTS base. Black Lab Linux 8 will showcase 3 desktop environments : MATE, LXDE and GNOME 3. Other improvements include: Full EFI support Kernel 4.4.0-38 LibreOffice 5.2 GNOME Video Rhythmbox Firefox 49 Thunderbird GIMP Full multimedia codec support Read more

Intel Core i7 6800K Benchmarks On Ubuntu + Linux 4.8

While the Core i7 6800K has been available for a few months now, there hadn't been any review on it since Intel hadn't sent out any Broadwell-E samples for Linux testing this time around. However, I did end up finally buying a Core i7 6800K now that the Turbo Boost Max 3.0 support is finally coming together (at first, Intel PR said it wouldn't even be supported on Linux) so that I can run some benchmarks there plus some other interesting items on the horizon for benchmarking. Here are some benchmarks of the i7-6800K from Ubuntu 16.04 LTS with the Linux 4.8 kernel. Read more