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

Microsoft Windows Server Benchmarked Against Six Linux Distributions

While it was not too long ago that Microsoft Windows Server 2019 began shipping and that we conducted some end-of-year benchmarks between Windows and Linux, with being in the process of running a number of Windows and Linux benchmarks as part of our ongoing 10GbE OS performance testing, I also took the opportunity to run some other benchmarks on Windows Server 2016 and 2019 as well as a set of Linux distributions. With carrying out the fresh OS installations anyways for the network testing, with recently having brought over some more Phoronix Test Suite test profiles with Windows support, I decided to run some fresh Windows Server vs. Linux benchmarks anyways. Granted, not all of the tests are server-oriented and not all of the traditional Linux server distributions were used. Just take this as you wish of some fresh Windows vs. Linux performance benchmarks. Read more

Games: Lutris, Little Mouse's Encyclopedia, Team Fortress 2 and More

Roundup of Wine 4.0 Release Coverage

  • Wine 4.0 Released
    The Wine team is proud to announce that the stable release Wine 4.0 is now available.
  • Wine 4.0 Officially Released with Vulkan & Direct3D 12 Support, HiDPI on Android
    The Wine project proudly announced today the general availability of the Wine 4.0 release, a major version of the open-source software that lets Linux and macOS users install and use Windows apps on their computers. Wine 4.0 comes about a year after the Wine 3.0 release, which was the first to introduce an Android driver to allow users run Windows apps and games on devices powered by Google's Android mobile OS, Direct3D 11 support by default for AMD Radeon and Intel GPUs, a task scheduler, as well as AES encryption support on macOS. With Wine 4.0, the team continues to improve the free and open-source compatibility layer that allows Windows program to run on Linux and Mac computers, adding new features like support for the next-generation Vulkan graphics API, Direct3D 12 support, HiDPI (High-DPI) support on Android, and support for game controllers.
  • Wine 4.0 Released With New Features: Run Windows Apps On Linux Efficiently
    With Microsoft’s initiative to bring Linux Bash Shell on Windows 10, the Windows users are now able to run their favorite Linux tools on their current operating system. But what if you need to run full-fledged Windows apps and games on a Linux distro? In that case, a software like Wine is really helpful. The developers of this utility have recently released the new version, i.e., 4.0, with lots of features. Wine 4.0 is the result of a year of development effort.
  • Wine 4.0 Released With Vulkan Support, Initial Direct3D 12 Support, CSMT Enabled By Default
    After being in development for a year, Wine 4.0 is now available for download. The new stable Wine release includes important changes like support for Vulkan, Direct3D 12 and game controllers. For those that might not be familiar with it, Wine is a Windows compatibility layer for Linux that lets you run Windows applications and games on Linux, macOS, and Android (experimental). Wine is used by Proton, Valve's Steam Play compatibility layer that allows playing Windows games on Linux, and by CrossOver, a commercial Microsoft Windows compatibility layer for macOS and Linux, among others.
  • Wine 4.0 is Here with Significant New Features
    Not everyone prefers to use Wine. But, if you have a favorite app/service that is not yet available for Linux, you can try Wine in order to run Windows apps or games. For those who are not aware of Wine, it’s a software that lets you run Windows-only applications and games on Linux. Want iTune on Linux, Wine is your best bet.
  • Wine 4.0 Released With Vulkan Support, Initial Direct3D 12 and Better HiDPI
  • Wine 4.0 Officially Released With Vulkan Support, Initial Direct3D 12 & Better HiDPI
    Wine 4.0 is now officially available as the new annual stable release to Wine for running Windows programs and games on Linux and other operating systems. Following seven weekly release candidates, Wine 4.0 was ready to ship today as judged by Wine founder Alexandre Julliard. Wine 4.0 is a big release bringing initial Vulkan graphics API support, Direct3D CSMT is enabled by default, early Direct3D 12 support via VKD3D, continued HiDPI work, various OpenGL improvements, multi-sample D3D texture support, 64-bit improvements, continued Android support, and much more... See our Wine 4.0 feature overview to learn more about this big update.
  • Just over a year after the last main release, Wine 4.0 is officially here
    You might want to grab a glass for this one, no not that dusty old thing, one of the nice ones. The ones at the back of the cupboard for special occasions! Wine 4.0 is officially here. Comparing Wine 3.0 to 4.0, naturally it's a pretty huge release. Although, most people have likely been using the development builds for some time.

today's howtos