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

Mint 18 Released, No GUI Please, Atomic Host 7.2.5

Today in Linux news, the Red Hat announcements kept on coming including the release of Red Hat Atomic Host 7.2.5. Elsewhere, Mint 18 in Cinnamon and MATE flavors was announced by Clement Lefebvre as promised. Bryan Lunduke just finished up 10 days using only a Linux terminal saying it "was too painful" and Eric Grevstad said using Linux and LibreOffice will change your life. Read more

July 2016 issue of The PCLinuxOS Magazine released

The PCLinuxOS Magazine staff is pleased to announce the release of the July 2016 issue. With the exception of a brief period in 2009, The PCLinuxOS Magazine has been published on a monthly basis since September, 2006. The PCLinuxOS Magazine is a product of the PCLinuxOS community, published by volunteers from the community. The magazine is lead by Paul Arnote, Chief Editor, and Assistant Editor Meemaw. The PCLinuxOS Magazine is released under the Creative Commons Attribution- NonCommercial-Share-Alike 3.0 Unported license, and some rights are reserved. In the July 2016 issue: * Seven Years Later: A Look Back * Installing A Seeburg 1000 On PCLinuxOS * ms_meme's Nook: Anytime * PCLinuxOS Family Member Spotlight: tuxlink * GIMP Tutorial: Engraved Text * Game Zone: Funklift * PCLinuxOS Recipe Corner * Tip Top Tips: A Simple HTTP Server * PCLinuxOS Puzzled Partitions * And much more inside! This month’s magazine cover image was designed by Meemaw. Download the PDF (8.3 MB) http://pclosmag.com/download.php?f=2016-07.pdf Download the EPUB Version (6.6 MB) http://pclosmag.com/download.php?f=201607epub.epub Download the MOBI Version (7.6 MB) http://pclosmag.com/download.php?f=201607mobi.mobi Visit the HTML Version http://pclosmag.com/html/enter.html

4MLinux 18.0 Distro Released with Support for LibreOffice 5.2, Thunderbird 45.1

4MLinux developer Zbigniew Konojacki has just informed Softpedia today, July 1, 2016, about the immediate availability for download of the final release of the 4MLinux 18.0 operating system. Read more

GNU/Linux Leftovers

  • Not Love
    I had seen GNU/Linux once before in my life. At a previous school, the husband of one of the teachers installed it on a PC in my presence. He couldn’t get it working…. Still, I read that GNU/Linux did not crash. I needed that. I was willing to make the effort to download and install GNU/Linux if I could have only that. Our Internet connection was a few KB/s on dial-up… I spent two weekends and five evenings downloading an .iso CD-image with FileZilla or something on a Mac in the lab. I had never burned a CD before but tried once copying the file to the CD. That wouldn’t boot. I discovered CD imaging… So, on the second try, I had a CD that would boot on the machines. I first did one machine and it wouldn’t start X. Having never seen X before, this was a problem but it turned out all I needed was the scanning frequencies for the CRT in a configuration file. Google helped me find those for each of my five different kinds of monitors. Suddenly, the PCs were useful with GNU/Linux.
  • Linux Under the Hood: Silence of the RAM
    Now that I see the events of the last week chronicled clearly in front of my very eyes, maybe the disparaging old junk man was right after all. I’m shameless enough to admit my own idiocy as long as it leads to learning from my mistakes. Maybe Linux isn’t rocket science, but installing RAM was sure beginning to feel like it.
  • Check out our new issue plus win an ebook bundle!
  • 30 days in a terminal: Day 10 — The experiment is over
    When I set out to spend 30 days living entirely in a Linux terminal, I knew there was a distinct possibility I would fail utterly. I mean, 30 days? No GUI software? No Xorg? Just describing it sounds like torture. And torture it was. Mostly. Some moments, though, were pretty damned amazing. Not amazing enough to help me reach my 30-day goal, mind you. I fell short—only making it to day 10.
  • Bad Voltage Episode 70 Has Been Released: Delicious Amorphous Tech Bubble
  • Tokyo: Automotive Linux Summit
    Engineers will gather in Tokyo July 13-14 for the annual Automotive Linux Summit, a conference where auto-industry stakeholders discuss the adoption of an open-source Linux-based platform for in-vehicle infotainment. The two-day summit brings together automotive systems engineers, Linux experts, developers and other players.
  • Oxenfree, an adventure game with supernatural elements, available on Linux
    This well-received indie title has been ported over to Linux. Combining plenty of elements of 80s teen movies and packaging them in a polished adventure, Oxenfree may be worth checking out if you’re a fan of adventure games.
  • Space station management game, The Spatials: Galactology, is confirmed to be coming for Linux
    This is an expanded and reimagined version of the management sim, The Spatials. It’s yet to be released but the developers have confirmed that a Linux version is in the works.
  • Red Hat Storage VP sees different uses for Ceph, Gluster
    Red Hat Storage showed off updates to its Ceph and Gluster software and laid out its strategy for working with containers at this week’s Red Hat Summit in San Francisco.