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

KDE/GNOME Events: Linux Applications Summit and KDE e.V. board meeting in Berlin

  • Some GNOME / LAS / Wikimedia love

    About LAS 2019: Linux App Summit Linux App Summit 2019 And about the previous LAS format: Libre Application Summit Libre Application Summit 2018

  • Linux Applications Summit 2019 activity

    And finally, I helped the local team with some minor tasks like moving items and so. I want to congratulate all the organization team and specially the local team for the results and the love they have put in the event. The results have been excellent and this is another strong step for the interweaved relations between opensource development communities sharing very near goals. My participation at the conference has been sponsored by the GNOME Foundation. Thanks very much for their support.

  • KDE e.V. board meeting in Berlin

    Just back from Barcelona – with a brief pause at home for some mathematical cabaret, a board meeting for my local badminton club, music lessons and an afternoon of volunteering at a local charity second-hand shop – and I’m off to Berlin again. The train is not particularly fast, but it’s a relatively predictable six hours from here to HBf and I do have some “home” feeling in Berlin. At least I have a favorite chocolate place. In Berlin we’re combining a KDE e.V. board meeting with the KDE Frameworks 6 sprint. Both events are basically “planning for the future” on an organizational and technical level.

Yocto-based Torizon distro adds OTA updater

Toradex has released an experimental version of an OTA updater for its new Torizon embedded Linux distribution. Torizon OTA offers fault-tolerant features and supports web-based remote management including grouping of devices into fleets. Read more

Growth of Kubernetes

  • Just how popular is Kubernetes?

    In its study of usage data from thousands of companies and more than 1.5 billion containers, the company found "roughly 45% of Datadog customers running containers use Kubernetes, whether in self-managed clusters or through a cloud service." Not bad for a technology that's just over five years old. What's more telling though is that almost half of all Datadog container users have already turned to Kubernetes. It's Kubernetes' growth rate that really tells the story. In the last year, Kubernetes' numbers of users grew by 10%. In the meantime, other container orchestration programs, such as Marathon and Docker swarm mode, have simply not caught fire. Indeed, their parent companies, D2iQ, formerly Mesosphere, and Docker both started offering Kubernetes to their customers. Need more be said? Datadog also found that Kubernetes is very popular on the public cloud. In particular, managed Kubernetes services such as Google Kubernetes Engine (GKE) dominates the Google Cloud Platform (GCP). Since Kubernetes ancestry goes back to Google that comes as no surprise.

  • Rancher CEO on k3s: Kubernetes is the new Linux; you run it everywhere

    Once, Kubernetes was just some geeky cloud-native project for orchestrating containers (a virtualized method for running distributed applications). Isn’t it funny how it’s worked its way into practically every tech conversation in just a few years? In fact, thanks to technologies that shrink and simplify it, Kubernetes is about to find its way into even more use cases. With the technology and its uses expanding so rapidly, how do we even define it anymore? Sheng Liang (pictured), co-founder and chief executive officer of Rancher Labs Inc., has an idea: “Kubernetes is the new Linux, and you run it everywhere.” Cloud, on-premises data center, bare metal, internet of things edge, Raspberry Pi, surveillance camera? Check. The developer ecosystem is invading more and more spaces through tweaks that make Kubernetes easier than ever to deploy.

Screencasting with OBS Studio on Wayland

For the past few months, I’ve been doing live coding sessions on YouTube showing how GNOME development goes. Usually it’s a pair of sessions per week, one in Brazilian Portuguese so that my beloved community can enjoy GNOME in their native language; and one in English, to give other people at least a chance to follow development as well. We are quite lucky to have OBS Studio available for screencasting and streaming, as it makes our lives a lot easier. It’s really a fantastic application. I learned about it while browsing Flathub, and it’s what actually motivated me to start streaming in the first place. However, I have to switch to X11 in order to use it, since the GNOME screencast plugin never really worked for me. This is annoying since Mutter supports screencasting for years now, and I really want to showcase the latest and greatest while streaming. We’re still not using the appropriate APIs and methods to screencast, which doesn’t set a high standard on the community. So I decided to get my hands dirty, bite the bullet, and fix this situation. And so was born the obs-xdg-portal plugin for OBS Studio! The plugin uses the standard ScreenCast portal, which means it should work inside and outside the Flatpak sandbox, in Wayland and X11, and on GNOME and KDE (and perhaps others?). Read more