1834935 Members
2445 Online
110071 Solutions
New Discussion

Re: Alias for ^P

 
PatRoy
Regular Advisor

Alias for ^P

Hi all.

I've been something rather weird at work.

My boss wants me to find out how I can setup an alias that would call up my history file, much like a ^P would do..

like this:

cup = previous command...
cdn = next command...

Is this even duable? Can this be done?

Thanks. Patrick
12 REPLIES 12
Michael D. Zorn
Regular Advisor

Re: Alias for ^P

I'm using ksh. I have in .kshrc a line

alias h='fc -l'

So

h (from the command line prompt)

Shows the last N commands.

From the keyboard, [esc]k goes back through the list, and [esc]j goes forward from where [esc]k left off.

Bill Hassell
Honored Contributor

Re: Alias for ^P

Here's script that you can name 'h' (for history). Without any options (just type h) it will show the most recent history to the length of the current screen. This is really useful if you have longer screens or resize your terminal screen. You can also type h ### to show the most recent ### lines.


Bill Hassell, sysadmin
PatRoy
Regular Advisor

Re: Alias for ^P

For the 'fc -l' solution.. it isn't really what I need. I really need to cmds to show one by one on the cmd line ready to just hit return and it will be executed...

Bill, you haven't posted the attachement / your script you're talking about.. I can't see it at least...

Thanks. Patrifck
MarkSyder
Honored Contributor

Re: Alias for ^P

I may be missing something, but it appears to me that unix already provides what you want. Replace ^P with esc k and replace cup and cdn with k and j respectively.

Mark Syder (like the drink but spelt different)
The triumph of evil requires only that good men do nothing
PatRoy
Regular Advisor

Re: Alias for ^P

Well, yeah I knew about theses...

Okay, I was kinda of embarrassed to say this... but here's the scope of the request :

it's my damn boss who wants this for his blackberry. He wants to be able to do this in his SSH client within the blackberry. I know.. it's freaking stupid if you ask me!! All this just because he doesn't want to have to hit a few more keys to get the special character (i.e. ESC or CTRL)...

I told him I'd quickly looked into it, but I won't try to reinvent the wheel here...
OldSchool
Honored Contributor

Re: Alias for ^P

AFAIK this won't work in ksh...

Sounds like your boss has seen "bash". it can do this. you would need to set him up so his login shell is bash and then take a look at the inputrc and bashrc files. I used to have the settings to do this by I can't locate them. Basically, in the inputrc file you map the up-arrow (esc-[-something] to the command for one line back in history.

FWIW: these may be the default assignments w/ bash now, as they work on my redhat boxes and I haven't done any config of the rc files mentioned

OldSchool
Honored Contributor

Re: Alias for ^P

and, of course, that wouldn't be 'in his crackberry', it would be on the system he connected to.
Bill Hassell
Honored Contributor

Re: Alias for ^P

Ooops, forgot the attachment. It's short so here it is:

#!/usr/bin/sh

# The h command - short for history

# Adapts to $LINES or $1 as needed
# make negative as required and reduce by 2 if no param used
# so evrything is shown including the last prompt line

if [ $# -gt 0 ]
then # specified line count
HISTLINES=$1
[ $HISTLINES -gt 0 ] \
&& let HISTLINES=HISTLINES*-1
else # use $LINES
[ $LINES -gt 5 ] \
&& let HISTLINES=0-$LINES+2 \
|| let HISTLINES=0-$LINES
fi

fc -l $HISTLINES

So your boss has shell access. Wow, I hope it is not a root user ID. Sounds like the boss still wants to be a sysadmin and not the boss. What you might offer is a menu script that replaces shell access. Then single characters can be used to run lots of command (limited in scope by prudent sysadmins that write restricted menu scripts).


Bill Hassell, sysadmin
Matti_Kurkela
Honored Contributor

Re: Alias for ^P

Note that any aliases, shell functions and the like are written to the command history as commands. Keystrokes like ^P are not.

To browse the history, you really need something that is _not_ considered a command and does not change what you're browsing.

To minimize the need of special keys, let me suggest something that may not seem very much 21th century... teach him to use the Vi mode of the shell! (set -o vi)

Using the Esc key is still required at some points: for example, to browse the command history, you need just one press of Esc to switch from "insert mode" to "history browsing mode", but after that single press of Esc you can use plain k/j keys to get previous/next command in the history file, and h/l to move left/right on the current command line.

If you're limited to whatever a "stock" HP-UX contains, this might be a good option for you. Your boss can enable the Vi mode himself whenever necessary, so you don't need to install any non-default shells or copy any special configurations to all hosts.

See also this thread:
http://forums1.itrc.hp.com/service/forums/questionanswer.do?threadId=865134
It contains some examples for re-mapping keys with HP-UX sh/ksh.

I haven't had a chance to try out a BlackBerry (for some reason, Nokia Communicators are used instead here in Finland :-)
but I understand it has an ALT key in its keyboard. Find out - by experimentation, if necessary - what characters are produced by the BlackBerry's terminal emulator when Alt+ combinations are used. Then use the tips in the above-mentioned thread to map them to useful functions.

MK
MK
Dennis Handly
Acclaimed Contributor

Re: Alias for ^P

>Bill: It's short so here it is:

Mine is real short, the same as Michael's. ;-)

>Matti: teach him to use the Vi mode of the shell! (set -o vi)

Or he can use emacs mode and just type ^P.
PatRoy
Regular Advisor

Re: Alias for ^P

Well, all solutions leads to using a special character, like CTRL or ESC, which wasn't what I needed...

I told my boss that he's going to live with having to fetch the CTRL + char in his blackberry! The hell with this! It's a freaking waste of time anyways!

Thank you all for your suggestions!! :)

Patrick
OldSchool
Honored Contributor

Re: Alias for ^P

note that the BASH solution I mentioned does not require the additional control characters. once properly configured, you can move thru the history file w/ arrow keys. still a lot of work however