Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

How to write out a Perl script and run it in as few keystrokes as possible.

by MrSnrub (Sexton)
on Sep 24, 2012 at 22:38 UTC ( #995449=perlquestion: print w/replies, xml ) Need Help??
MrSnrub has asked for the wisdom of the Perl Monks concerning the following question:

Hello! I am looking to speed up my Perl programming. At my Linux prompt I type vi to begin editing my Perl file. Then I make whatever changes I need to make, and then when I'm done I type :wq which takes me back to my shell prompt. I type perl -Wall, and then usually I want to re-edit my file because I got some errors, so I push the Up arrow on my keyboard to cycle through my history and the first thing I get is vi, which I can enter in to take me back to Vim.

This process is repeated like a thousand times a day. My question is: Is this really as fast as it can possibly get? Is there any way to just compile and run the program from within Vim? Preferably with just one function-key keystroke?

Replies are listed 'Best First'.
Re: How to write out a Perl script and run it in as few keystrokes as possible.
by tobyink (Abbot) on Sep 24, 2012 at 22:59 UTC

    You can execute a shell command in vim using:


    Vim substitutes % in the command for the currently edited file, thus the following can be used to run the current script (assuming it's chmodded correctly and has a shebang):


    Via .vimrc you can create a shortcut for that.

    ... or to create a shortcut which saves first, then runs:

    command R w | !./%

    And another (hey, TIMTOWTDI):

    nmap <F5> :w<cr>:!./%<cr>

    (which maps the F5 key to do the duty.)

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: How to write out a Perl script and run it in as few keystrokes as possible.
by johngg (Abbot) on Sep 24, 2012 at 22:54 UTC

    Unless you are running on a dumb terminal, why not have two windows open. Do your edits in one and just do :w to save the file without quitting the editor. Run the script in the other window. Repeat as required.



      I would think that would actually increase the number of keystrokes needed by one:

      Original Process

      (UP)vim (2 keystrokes)
      (make changes) (0 keystrokes)
      :wq(ENTER) (4 keystrokes) *
      (UP)(UP)perl -Wall (3 keystrokes)
      GOTO Start

      Total: 8 keystrokes

      Two-Window Process

      (edit .pl file in vim, window1) (0 keystrokes)
      :w(ENTER) (3 keystrokes) *
      Alt-Tab to window2 (2 keystrokes)
      (UP)perl -Wall (2 keystrokes)
      Alt-Tab back to window1 (2 keystrokes)
      GOTO Start

      Total: 9 keystrokes

      * : only counts as one keystroke because in my .vimrc I have ; mapped to : and vice-versa so I don't need to press the Shift key
        the first process can even be shortened by using ZZ instead of :wq and enter.
        but one advantage of the second is, you still have the vim history and can undo the recent changes easily. another is, you can still see the output of your script when continuing with editing.
        Enable sloppy focus/focus-follows-mouse and both alt-tab instances go away, reducing it to 5 keystrokes - just drift your mouse pointer over the other terminal window. (And, yes, I'm pretty sure both Windows and OSX have options/plugins/tweaks that let you use sloppy focus. It's not an X-only thing.)

        Even without that, you forgot to account for needing to go back twice in command history to return to editing the source after running it. Include that forgotten "(UP)", and it's a tie at 9 and 9.

        More importantly, as already mentioned, keeping your vi(m) session open lets you keep your undo history, which can be far more valuable than shaving off a keystroke here and there.

        Using multiple windows/tabs also generalizes nicely to working with multiple source files (you do organize your code into modules rather than making it all one huge file, don't you?), since you can view and switch between the different source files trivially rather than having to try to find the correct file in your command history.

        (Am I the only one who thinks this entire quest is a bit silly, though? I know that the primary bottleneck in my coding is not the number of keystrokes I spend in the shell... Not even close...)

        I think that your single window method is also going to be 9 keystrokes because you will need 2 up-arrows to get back to vim once you have run your script.

        When given the choice I prefer to use a GUI editor, nedit, and I have set up my window manager so that focus follows the mouse without the need to click in a window. I also use emacs-style command history navigation as I was an Emacs user on Pr1mos long before I ever saw vi so am more familiar with it. My process is as follows:-

        • Start:
        • Edit script with nedit - (0 keystrokes)
        • Ctrl-S to save (2 keystrokes)
        • Move mouse over xterm window (1 wrist twitch)
        • Ctrl-P then Enter to re-run script (3 keystrokes)
        • Move mouse back to nedit window (1 wrist twitch)
        • GOTO Start

        I make that 5 keystrokes and 2 wrist twitches.



Re: How to write out a Perl script and run it in as few keystrokes as possible.
by Old_Gray_Bear (Bishop) on Sep 24, 2012 at 23:49 UTC
    I normally run with three windows (Linux terminal sessions) --
    1. my source code
    2. my input data
    3. the Perl debugger
    Save the source; step through the code with the debugger; determine where I went wrong; fix the source code; repeat loop until done.

    I Go Back to Sleep, Now.


Re: How to write out a Perl script and run it in as few keystrokes as possible.
by tinita (Parson) on Sep 24, 2012 at 23:21 UTC
    lots of options. I usually use a vim mapping like shown by tobyink, or another terminal window (or another gnu screen window), and sometimes I just put vim into the background with ctrl-z, type the command, and get back to vim after it.
    and as soon as you have to edit more files I can recommend vim tabs. you might also want to check out some plugins on
Re: How to write out a Perl script and run it in as few keystrokes as possible.
by jmlynesjr (Hermit) on Sep 25, 2012 at 01:57 UTC

    I run gedit in one(linux) window and a terminal in a second window. Edit, click-save, click-terminal window, up-arrow, enter, click- gedit window. Do forever...

Re: How to write out a Perl script and run it in as few keystrokes as possible.
by SuicideJunkie (Vicar) on Sep 25, 2012 at 16:28 UTC

    You can reduce it to a mere two keys (CTRL-S) with the use of eval.

    EG: Coding for OpenGL "live".

    Put simply, have one extra script to run your main script repeatedly. As soon as you save the changes to disk, the next iteration of the looping run will show your changes.

    If you want to get fancy, you can have the eval loop wait until the timestamp of your source file changes before re-running the script.

Re: How to write out a Perl script and run it in as few keystrokes as possible.
by protist (Monk) on Sep 25, 2012 at 05:57 UTC
    While not an answer to what you asked specifically, something I do, that I think you would enjoy, is allow vi-like editing from the bash prompt, so that I can move around and look through my history without using the (dreaded) arrow keys. (wow that sentence was long)
    from the bash prompt: set -o vi or to set this option to automatically load when you start your termin +al: cd ~ && echo 'set -o vi' >> .bashrc && echo "success\n"
    I love being able to use vi commands from the bash prompt, and I think you will, too.
Re: How to write out a Perl script and run it in as few keystrokes as possible.
by Voronich (Hermit) on Sep 26, 2012 at 20:41 UTC

    While this is perhaps particular to my programming style, I have a window open that runs a "runtests" script on a loop. Inside 'runtests' (a plain old sh script) I run whatever I'm working on (usually a perl script) and end with a 5 second delay.

    This allows me to change the delay or even the script arguments without interrupting the test loop script.

    I find it's a remarkably handy way of keeping focus in the editor and looking over to see if tests are failing or the script is blowing up.

Re: How to write out a Perl script and run it in as few keystrokes as possible.
by GrandFather (Sage) on Sep 26, 2012 at 23:33 UTC

    I use Komodo IDE so "run the script" is F5. Set a break point is a single mouse click. Examine the contents of a variable when stopped in the debugger is hover over the variable or look in the "Locals" tab of the debug window.

    I use Komodo under Windows, although it is available for Linux and Mac where I guess it works much the same. You may find that Padre works in like manner for you, although I've not used it to any extent myself. Note that both of these work in a GUI.

    Update: Oh, I should have mentioned that the script is saved before it is run, with or without confirmation (your option).

    True laziness is hard work

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://995449]
Approved by davido
[oiskuu]: ... getlogin refers to tty owner; if you close STDIN, then getlogin returns undef. I get undef in my shell (running under X)
[tye]: Then there is a more persistent big of data similar to getlogin(). I don't believe even daemonizing drops it. So you can tell services that were started at boot time from those started by a person.
[tye]: I'll have to look into that later. getlogin() suites my current needs.

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (9)
As of 2017-06-23 19:19 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (554 votes). Check out past polls.