Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

alias to enter text into prompt

161 views
Skip to first unread message

tod davis

unread,
Jan 12, 2017, 2:33:25 PM1/12/17
to
All,

I am trying to construct an alias with the following behavior.

It should output a predefined string to the bash prompt but, critically, not execute the code and leave the cursor on the same line of the output text.

The idea is that i would continue typing after the text was entered onto the line.

#echo, of course, does not work but this might help approximate what I am looking for
foo() { echo 'bar'; }
alias f=foo

#desired behavior
>f
>bar (cursor here is what I need)

#no
>f
>bar
>(cursor here is not want I need)

something that can be called form the bash_profile is ideal.

Thanks for any thoughts.

TD

hymie!

unread,
Jan 12, 2017, 3:15:55 PM1/12/17
to
In our last episode, the evil Dr. Lacto had captured our hero,
tod davis <todw...@gmail.com>, who said:

> It should output a predefined string to the bash prompt but, critically,
> not execute the code and leave the cursor on the same line of the
> output text.

Are you talking about changing the prompt? Or ...

> The idea is that i would continue typing after the text was entered
> onto the line.

are you talking about emulating a person typing text *at* the command
prompt?

I suspect the latter.

I've recently learned that screen has this feature. If you run
screen, then you are put in window 0. At that point, you can have
this function

l() { screen -x -p 0 -X stuff 'ls ' }

In my case, I get a screen artifact, but issuing the "l" command
does what you want. The word "ls" followed by a space is sitting at
the prompt waiting for me to type the rest of the command.

--hymie! http://lactose.homelinux.net/~hymie hy...@lactose.homelinux.net

tod davis

unread,
Jan 12, 2017, 4:55:10 PM1/12/17
to
hymie!

you are correct on which behavior I wanted.

that command works when added to my profile and sourced in an existing screen session. From bash, it throws a "no screen session" error which is expected i guess.

it's very close to exactly what I need. i hope someone else has a bash solution.

thanks

old...@get-off-my.lawn

unread,
Jan 12, 2017, 7:17:04 PM1/12/17
to
> I am trying to construct an alias with the following behavior. It
> should output a predefined string to the bash prompt but, critically,
> not execute the code and leave the cursor on the same line of the
> output text. The idea is that i would continue typing after the
> text was entered onto the line.
>
>#echo, of course, does not work but this might help approximate what I am looking for
>foo() { echo 'bar'; }
>alias f=foo
>
>#desired behavior
>>f
>>bar (cursor here is what I need)

I think you can just add the text _to_ the end of the PS1 prompt line in ~/.bashrc;
no alias needed.

ex)

#prompt _before_ tweak:
me@mypc ~ $

#edit ~/.bashrc:
..
# If this is an xterm set the title to user@host:dir
case "$TERM" in
xterm*|rxvt*)
PS1="\[\e]0;${debian_chroot:+($debian_chroot)}\u@\h: \w\a\]${PS1}foo "
;; # here be the edits ----------------------------------^^^^^^^^^^
*)
;;
esac
..

# re-load configs:
me@mypc ~ $ source ~/.bashrc

#prompt _after_ tweak:
me@mypc ~ $ foo

The above is from the default .bashrc on Linux Mint; yours might be more
simple, ie. no debian_chroot stuff.

HTH,
Jeff

tod davis

unread,
Jan 13, 2017, 6:00:21 AM1/13/17
to
On Thursday, January 12, 2017 at 2:33:25 PM UTC-5, tod davis wrote:
Thanks Jeff

The string to be entered is actually the first part of a command so changing the prompt doesn't quite get me there. Maybe my first example was poor. How about this one...

if I want to grep a file list, which we do al lot, i end up typing "ls -la | grep *something*" and the *something* changes. if i had an alias that would type the "ls -la | grep " for me, then i save a lot of key strokes over time

thanks for the suggestion

Chris Elvidge

unread,
Jan 13, 2017, 6:26:16 AM1/13/17
to
You don't need an alias, just a function.

Try this (in .bashrc or .bash_aliases):

#!/bin/bash
f() { [ "$1" != "" ] && ls -lA | grep "$@"; }
export -f f
#

Then 'f bash' would output something like:
-rw-rw-rw- 1 chris chris 4.9K Aug 10 13:13 .bash_aliases
-rw------- 1 chris chris 11K Jan 12 19:35 .bash_history
-rw-r--r-- 1 chris chris 220 Aug 9 16:44 .bash_logout
-rw-r--r-- 1 chris chris 12K Jan 3 11:57 .bashrc


--

Chris Elvidge, England

tod davis

unread,
Jan 13, 2017, 7:17:59 AM1/13/17
to
Thanks Chris - I'm beginning to think functions are the only option. grep is not the only command i want to "macro" into the command line. there are 50 other command parts.

hdfs dfs -ls blah
scp me@somewhere:/home/me blah
rsync blah

Thanks everyone for the input

Kenny McCormack

unread,
Jan 13, 2017, 8:06:26 AM1/13/17
to
In article <19b164a4-d7e8-4534...@googlegroups.com>,
tod davis <todw...@gmail.com> wrote:
...
>Thanks Jeff
>
>The string to be entered is actually the first part of a command so
>changing the prompt doesn't quite get me there. Maybe my first example
>was poor. How about this one...
>
>if I want to grep a file list, which we do al lot, i end up typing "ls
>-la | grep *something*" and the *something* changes. if i had an alias
>that would type the "ls -la | grep " for me, then i save a lot of key
>strokes over time
>
>thanks for the suggestion

I get what you want (I suspect some of the other posters did not), but,
alas, the normal Unix shells don't really speak to this sort of
interactivity. There isn't any facility in bash (or any other shell to the
best of my knowledge) to "push" characters into the input stream.

The Unix-shell-ish way is, as others have described, to make a function or
alias that does the first part of what you want, and then use that, passing
the additional args on the command line, but that doesn't have the visual
appeal of having the characters from the function or alias actually pushed
into the input stream and thus visible to you on the command line.

Note that you can get somewhere close to where you want to be, by using the
up arrow key and scrolling back through previous commands. You can also
search the history and bring up previous commands - using keystrokes that
depend on which editing more you are in. I use the vi mode, so for me, it
looks like:

$ set -o vi
$ <esc>/string2LookFor

But, my real suggestion in all of this, is to look into the 'fc' command.
This is a little known bash command that allows you to visually edit (in
full screen vi) a command or group of commands, and then execute them. I
have a feeling that that may work for you. You'll have to try it out and
get used to it - it is a little weird - but is pretty nice once you get
used to it.

--
Pensacola - the thinking man's drink.

tod davis

unread,
Jan 13, 2017, 9:15:47 AM1/13/17
to
On Friday, January 13, 2017 at 8:06:26 AM UTC-5, Kenny McCormack wrote:
> In article <19b164a4-d7e8-4534...@googlegroups.com>,
> tod daviswrote:
Thanks Kenny for the excellent proper explanation of what I meant. I will give fc a try as well.

Another possible option, since I use iTerm2 on Mac is an AppleScript but that really does not help me during an ssh session.

old...@get-off-my.lawn

unread,
Jan 13, 2017, 11:29:05 AM1/13/17
to
> > The string to be entered is actually the first part of a command
> > so changing the prompt doesn't quite get me there. Maybe my first
> > example wa s poor. How about this one... if I want to grep
> > a file list, which we do al lot, i end up typing "ls -la | grep
> > *something*" and the *something* changes. if i had an alias that
> > would type the "ls -la | grep " for me, then i save a lot of key
> > strokes over time
>> >
>> > thanks for the suggestion
>> >
>> You don't need an alias, just a function.
>>
>> Try this (in .bashrc or .bash_aliases):
>>
>> #!/bin/bash
>> f() { [ "$1" != "" ] && ls -lA | grep "$@"; }
>> export -f f
>> #
>>
>> Then 'f bash' would output something like:
>> -rw-rw-rw- 1 chris chris 4.9K Aug 10 13:13 .bash_aliases
>> -rw------- 1 chris chris 11K Jan 12 19:35 .bash_history
>> -rw-r--r-- 1 chris chris 220 Aug 9 16:44 .bash_logout
>> -rw-r--r-- 1 chris chris 12K Jan 3 11:57 .bashrc

I tend to use stand-alone shell scripts (under ~/bin) for this sort of
thing instead of putting them in ~/.bashrc . In any event, both are better
than trying to put something in the prompt string and risking a broken
shell environment.

For stuff you want to run in it's own desktop terminal window you can
something like 'gnome-terminal -x bin/myscript' ; the terminal will close
as soon as the script is done so you'll need to put in a pause if you want
to keep it open. Seems each terminal emmulator works differently so you'll
have to read up on yours to find out which option passes an executable to
the terminal.

Icarus Sparry

unread,
Jan 13, 2017, 2:25:07 PM1/13/17
to
On Fri, 13 Jan 2017 06:15:41 -0800, tod davis wrote:

> On Friday, January 13, 2017 at 8:06:26 AM UTC-5, Kenny McCormack wrote:
>> In article <19b164a4-d7e8-4534...@googlegroups.com>,
>> tod daviswrote:
>> ...
>> >Thanks Jeff
>> >
>> >The string to be entered is actually the first part of a command
>> >so changing the prompt doesn't quite get me there. Maybe my first
>> >example was poor. How about this one...
>> >
>> >if I want to grep a file list, which we do al lot, i end up typing
>> >"ls -la | grep *something*" and the *something* changes. if i had an
>> >alias that would type the "ls -la | grep " for me, then i save a lot
>> >of key strokes over time

I think you want this

# Read a command with bash editing and execute it
# Icarus Sparry, Jan 2017
editrun() {
eval $( read -ei "$*" ; printf "%s\n" "$REPLY" )
}

Then you can say

editrun 'ls -la | grep '

use your editing skills to change the presented command and hit enter and
it will run.


Editorial.
The standard comments about functions vs aliases apply. The bash manual
says 'For almost every purpose, aliases are superseded by shell
functions'. I suspect that aliases are part of folklore due to them being
available in csh. Therefore if you want to do this a lot it would be
better to say

lag(){
editrun 'ls -la | grep '"$@"
}

than

alias lag="editrun 'ls -la | grep '"

although for this simple case an alias would work.

tod davis

unread,
Jan 16, 2017, 10:23:43 AM1/16/17
to
On Thursday, January 12, 2017 at 2:33:25 PM UTC-5, tod davis wrote:
All,

Thanks again for the replies. Each one of them helped me come up with (or search) my final solution.

I started withj "read -e- i" from hymie! post but first I had to upgrade bash on my mac. the -i option was not available in bash 3.2 which ships with sierra. I upgraded to bash 4.4 and implemented the following in bash_profile


#set alias for paste function
alias p=paste

paste() {

echo "enter text or [Ctrl] + C to do nothing"
case $1 in

g) # g is for grep
run_cmd "ll | grep "
;;

h) # h is for hdfs
run_cmd "hdfs dfs -ls /choa/ "
;;

f) # f is for find
run_cmd "find . -type f -name '*.sh' "
;;

*)
echo $"Usage: enter p + [space] + < [g]rep | [h]dfs | [f]ind>"
;;

esac

}


run_cmd() {
read -e -i "$1"
eval "$REPLY"
}


to run, simply type "p" + space + option from the case statement

p g
p h
p f


Icarus Sparry

unread,
Jan 16, 2017, 4:13:33 PM1/16/17
to
On Mon, 16 Jan 2017 07:23:40 -0800, tod davis wrote:

>
> All,
>
> Thanks again for the replies. Each one of them helped me come up with
> (or search) my final solution.
>
> I started withj "read -e- i" from hymie! post but first I had to
> upgrade bash on my mac. the -i option was not available in bash 3.2
> which ships with sierra. I upgraded to bash 4.4 and implemented the
> following in bash_profile
>

hymie! post?

If you are stuck on something as old as bash 3.2 it would help to mention
it, bash 4 was first released just under 8 years ago, and has things like
associative arrays that we would assume.
0 new messages