Repeat last command with '!' prefix

Posted by Kahenraz on Sat 13 Aug 2016 10:16 PM — 4 posts, 20,342 views.

#0
I use the Bash for a lot of command-line scripting which is kind-of-sort-of similar to Mush's command window.

One feature I wish Mush had would be to execute the last command as denoted by the prefix '!'.

For example,

HISTORY:
quest status 123
quest status 456
quest status 789

'!' would execute the last command where as whereas !q or any combination of characters matching up to the last match would execute that command from history.

'!q' in this example, would execute 'quest status 789' because it's the last command to being with this prefix.

This can be further filtered by adding additional characters.
Australia Forum Administrator #1
Before addressing your main question, note that Ctrl+R repeats the last command.
Australia Forum Administrator #2
This alias should do what you want:


<aliases>
  <alias
   match="^!([A-Za-z]*)$"
   enabled="y"
   omit_from_command_history="y"
   regexp="y"
   send_to="12"
   sequence="100"
  >
  <send>

local recentCommands = GetCommandList (0)

if not recentCommands  then
  ColourNote ("red", "", "No commands in command history")
  return
end -- if no commands

-- check for any argument, if none execute last command
if "%1" == "" then
  Execute (recentCommands [1])
  return
end -- if no argument

-- search backwards for matching command
for k, v in ipairs (recentCommands) do 
  if string.match (v, "^%1") then
    Execute (v)
    return
  end -- if a match
end  -- of for

-- nothing?
ColourNote ("red", "", "No command matching: %1")
</send>
  </alias>
</aliases>


Template:pasting
For advice on how to copy the above, and paste it into MUSHclient, please see Pasting XML.


I am assuming you want to match an earlier alphabetic command - if not change the regular expression in the alias, but be careful that it is being used as a regular expression in string.match further down.

The function GetCommandList gets all the command history into a table. If you don't specify an argument (ie. you type "!") then it executes the most recent one. If you do, it searches for a match, and executes that one if found.
Amended on Sun 14 Aug 2016 12:06 AM by Nick Gammon
#3
That works great. Thank you!