2

I'm often compiling stuff and therefore repeating the same command over and over, so I'd like to benefit from my "visual memory" and have a sort of "repeat last command" button attached to a terminal. More precisely, here is how I see it:

  • My "special" terminal would be just like a standard terminal emulator, with an extra input line at the bottom, which would have a "run" button attached to it. Clicking the button would run the command without erasing it.
  • Maybe even an arbitrary number of such commands could be stacked (since I often need to alternate between 2 or 3 commands).

I know I can use just Enter with the keyboard to redo the last command, but what often happens is that I forget I just had to type another command (to fix something, or whatever) so will not bring the right command from the history. So instead of a reflex, I have to actually read (at least skim) the command before pressing Enter.

Is there such a contraption somewhere? I often use several terminals precisely to make sure each always has the "right" command as last, but my Alt+Tab skills usually make me tab to the wrong window. And I need terminal information, such as the environment and cwd, so I cannot just try and "integrate" it in my code editor. I mean, using Alt+F2 and then Enter would not work (since I also need the read the output from the terminal). I just want to be able to use the best of both keyboard and visual memory (via clicks) at the same time.

bahamat
  • 39,666
  • 4
  • 75
  • 104
anol
  • 713
  • 1
    How about an alias with your compile command - a very short alias, like two or three letters? – Emanuel Berg Jul 18 '12 at 16:09
  • 2
    How would your button know which command you need if "up-Enter" isn't always what you want? Most shells have sophisticated tools for recalling and executing commands in the history, in bash !m would recall the last command entered starting with m (e.g. make). – donothingsuccessfully Jul 18 '12 at 17:37
  • 1
    Have a look at difference between termian and a shell. The problem is that the terminal typically knows nothing about your shell, e.g. it doesn't know in which directory (or host) you currently are and can't execute the command. You should try to enhance your setup and build from within your ide/editor. – Ulrich Dangel Jul 18 '12 at 18:00
  • Thank you very much for your ideas! I'll read them in detail. Anyway, I think a "fake screenshot" could help explain what I was thinking about. (Images are taken from the internet BTW). This is how I'd see my "extended terminal":

    My Terminal

    Just a plain terminal attached to a "command bar" at the bottom. The idea is: in the event of a click on the "Run" button, the current command line in the shell is removed and the text in the "command bar" becomes the current command, which is sent to the terminal. Very simple, no autocompletion or anything necessary.

    – anol Jul 20 '12 at 15:44
  • (sorry, I cannot post longer messages). Also, when the button is clicked, the text remains in place, so I don't have to type it again. The idea would be to later make some kind of "hover" bar (like on this fake screenshot: IDE example that stays on top of the IDE. Its position is more or less fixed, so I can use my visual memory to execute the command. Even if there are several of them, I find it easier to manage than the history (though those advanced commands may help). – anol Jul 20 '12 at 15:48
  • To sum up, I was thinking about some sort of "Eclipse Ant build button", but independent of any IDE. I once coded a simple version in Java, so I thought there should already be something like that somewhere, my question was mainly to help me find it. I hope it's clearer now... by the way, a command bar without the attached shell is less useful, since I have no way to read the output (e.g. in case of an error). So all it is, in fact, is an alternative way to input text into a shell using the mouse. Nothing more, nothing less... – anol Jul 20 '12 at 15:51
  • This is perhaps similar to http://unix.stackexchange.com/questions/147563/how-do-i-repeat-the-last-command-without-using-the-arrow-keys – quant Jan 07 '17 at 12:11

4 Answers4

2

I think that history expansion might be very helpful here. For example, typing !p will execute the last command starting with 'p'. Similarly !vi will execute the last command starting with 'vi', and so on. More info here. If you are not sure which of those was the last one, you can set shell option histverify (shopt -s histverify), so you can still edit (or inspect) the command before you execute it. More on this here.

Alternatively, you can use the history search functionality. In bash, hit Ctrl+R and type the command you want to find in the history (starting with the most recent one that matches the criteria). Hitting Ctrl+R repeateadly then will bring back older commands that match your criteria. More info here and here

If you are keen, you can modify your readline's inputrc, such that the up/down arrows do the history search for you. More info here and here.

Wojtek
  • 2,330
1

Take a look at the history functionality of your shell. In my zsh:

!!

repeats the last command.

Type history to take a look at the last few entries.

To get your desired buffer in the shell:

man zshzle

or

man readline

Also, you can tab expand the completions

make long_target
ls some_path
...
make l<TAB> # prints 'make long_target' without running it

use wildcards, expansions... not one bit of your use case lies outside of this functionality.

chiggsy
  • 293
  • 2
  • 8
0

Using zsh you can do just that with Esc+A. Type a command then press Esc+A instead of Enter. The command will be executed and the same command will be on the line buffer after the command completes.

bahamat
  • 39,666
  • 4
  • 75
  • 104
-2

It looks like you need a very simple answer. Since you mentioned Alt+Tab, I assume you are using (X)windows. You highlight the command with the mouse before you press "Enter". It is automatically copied to the clip board when you highlight it. To paste it, press the middle mouse button (scroll wheel) or type Shift+Insert.

bahamat
  • 39,666
  • 4
  • 75
  • 104
dadinck
  • 127
  • 2
    I got the impression that he has the habit of hitting that command often, and, if so, using the mouse isn't a good idea. – Emanuel Berg Jul 18 '12 at 19:44