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

GNOME News: Black Lab Drops GNOME and Further GNOME Experiments in Meson

  • Ubuntu-Based Black Lab Enterprise Linux 11.0.1 Drops GNOME 3 for MATE Desktop
    Coming about two weeks after the release of Black Lab Enterprise Linux 11, which is based on the Ubuntu 16.04.2 LTS (Xenial Xerus) operating system using the HWE (hardware enablement) kernel from Ubuntu 16.10 (Yakkety Yak), Black Lab Enterprise Linux 11.0.1 appears to be an unexpected maintenance update addressing a few important issues reported by users lately.
  • 3.26 Developments
    My approach to development can often differ from my peers. I prefer to spend the early phase of a cycle doing lots of prototypes of various features we plan to implement. That allows me to have the confidence necessary to know early in the cycle what I can finish and where to ask for help.
  • Further experiments in Meson
    Meson is definitely getting more traction in GNOME (and other projects), with many components adding support for it in parallel to autotools, or outright switching to it. There are still bugs, here and there, and we definitely need to improve build environments — like Continuous — to support Meson out of the box, but all in all I’m really happy about not having to deal with autotools any more, as well as being able to build the G* stack much more quickly when doing continuous integration.

Fedora and Red Hat

Debian and Derivatives

  • Reproducible Builds: week 108 in Stretch cycle
  • Debuerreotype
    The project is named “Debuerreotype” as an homage to the photography roots of the word “snapshot” and the daguerreotype process which was an early method of taking photographs. The essential goal is to create “photographs” of a minimal Debian rootfs, so the name seemed appropriate (even if it’s a bit on the “mouthful” side).
  • The end of Parsix GNU/Linux
    The Debian-based Parsix distribution has announced that it will be shutting down six months after the Debian "Stretch" release.
  • Privacy-focused Debian 9 'Stretch' Linux-based operating system Tails 3.0 reaches RC status
    If you want to keep the government and other people out of your business when surfing the web, Tails is an excellent choice. The Linux-based operating system exists solely for privacy purposes. It is designed to run from read-only media such as a DVD, so that there are limited possibilities of leaving a trail. Of course, even though it isn't ideal, you can run it from a USB flash drive too, as optical drives have largely fallen out of favor with consumers. Today, Tails achieves an important milestone. Version 3.0 reaches RC status -- meaning the first release candidate (RC1). In other words, it may soon be ready for a stable release -- if testing confirms as much. If you want to test it and provide feedback, you can download the ISO now.

OSS Leftovers

  • Chef expands its cloud and container menu
    Chef, a leading DevOps company, announced at ChefConf 2017 that it was adding new capabilities to it flagship Continous Automation/DevOps program, Chef Automate. This enables enterprises to transition from server- and virtual machine- (VM) based IT systems to cloud-native and container-first environments with consistent automation and DevOps practices.
  • Nextcloud 12: The bigger, better, in-house small business cloud
    It's not even been a year since Frank Karlitschek, co-founder and former CTO of ownCloud, forked ownCloud into Nextcloud. Since then, this do-it-yourself, open-source Infrastructure-as-a-Service (IaaS) cloud has become increasingly popular. Now, its latest version, Nextcloud 12, the program is adding more Software-as-a-Service (SaaS) features.
  • The Spirit of Open Source
  • What happened to Mastodon after its moment in the spotlight?
    More than a month later, the buzz over Mastodon has quieted. But though it may not be making headlines, the service continues to grow.
  • Mozilla: One Step Closer to a Closed Internet
    We’re deeply disheartened. Today’s FCC vote to repeal and replace net neutrality protections brings us one step closer to a closed internet. Although it is sometimes hard to describe the “real” impacts of these decisions, this one is easy: this decision leads to an internet that benefits Internet Service Providers (ISPs), not users, and erodes free speech, competition, innovation and user choice.
  • The eternal battle for OpenStack's soul will conclude in three years. Again
    After six years as a formal project, OpenStack has survived numerous raids and famines and now finds itself in a not-too-weird space of being boring, on-premises infrastructure. That is, “boring” in the good way of focusing on what users want and fixing existing problems, only chasing shiny objects – cough, PaaS, cough, containers, cough, orchestration – as much as needed.
  • With version 2.0, Crate.io’s database tools put an emphasis on IoT
    Crate.io, the winner of our Disrupt Europe 2014 Battlefield, is launching version 2.0 of its CrateDB database today. The tool, which is available in both an open source and enterprise version, started out as a general-purpose but highly scalable SQL database. Over time, though, the team found that many of its customers were using the service for managing their machine data and, unsurprisingly, decided to focus its efforts on better supporting those clients.
  • NewSQL CockroachDB Ready for Prime Time
    There's a new open source database on the block. Although it has a name that will most likely make you cringe for the first dozen or so times you hear it -- CockroachDB -- I have a feeling that if it isn't already on your radar, it will be soon.
  • Windows 10 S Won't Support Fedora, SUSE Linux, and Ubuntu
  • Manage Linux servers with a Windows admin's toolkit [Ed: Well, the solution is learning GNU tools, not relying on proprietary stuff with back doors from Microsoft]
  • FreeBSD quarterly status report
  • openbsd changes of note 622
  • Book Review: Relayd and Httpd Mastery

    Overall an excellent book which is typical Michael W Lucas writing style. Easy to follow, clear cut instructions, and tons of new stuff to learn. If one must use OpenBSD or FreeBSD, then the chances are high that one will stick with the defaults that come with OpenBSD. No need to use fat Apache, or Nginx/Lighttpd web server especially when httpd and relayd audited for security by OpenBSD core team.

  • Guix System Distribution (GuixSD) 0.13.0 GNU/Linux OS Supports 64-bit ARM CPUs
    The GNU Guix and GuixSD 0.13.0 releases are here about five months after the December 2016 launch of version 0.12.0, and it appears to be a major milestone implementing a few important changes. First off, this release can now be installed on computers powered by AArch64 (64-bit ARM) processors.
  • The Good And Bad In WikiTribune, Wikipedia Founder's Open-Source News Site
    Countering the fake news threat has become a real challenge for social media platforms, which also serve as avenues of news dissemination along with the traditional media outlets.
  • Android Studio 3.0 Canary 1
  • Jaded by Java? Android now supports Kotlin programming language
  • Rcpp 0.12.11: Loads of goodies
    The elevent update in the 0.12.* series of Rcpp landed on CRAN yesterday following the initial upload on the weekend, and the Debian package and Windows binaries should follow as usual. The 0.12.11 release follows the 0.12.0 release from late July, the 0.12.1 release in September, the 0.12.2 release in November, the 0.12.3 release in January, the 0.12.4 release in March, the 0.12.5 release in May, the 0.12.6 release in July, the 0.12.7 release in September, the 0.12.8 release in November, the 0.12.9 release in January, and the 0.12.10.release in March --- making it the fifteenth release at the steady and predictable bi-montly release frequency.
  • Master Haskell Programming with Free Books
    Haskell is a standardized, general-purpose, polymorphically statically typed, lazy, purely functional language, very different from many programming languages. Recent innovations include static polymorphic typing, higher-order functions, user-definable algebraic data types, a module system, and more. It has built-in concurrency and parallelism, debuggers, profilers, rich libraries and an active community, with approximately 5,400 third-party open source libraries and tools.
  • [Older] Manifesto: Rules for standards-makers

    If we work together on a project based on open tech, these are the principles I will try to stick to. I wanted to put all this in one place, so I can pass it along to future software developers.