You are not logged in.
Hi,
is there a way to append the contents of a file to the command prompt? Say I have defined an alias and I want to change it on the fly. So I'd do
$ which <my_alias>
then mark it with the mouse, and paste it to the command line. What I would like to do instead, would be:
$which <my_alias> > some_temp_file
$vim some_temp_file #edit the command the way I want it
$ # somehow get the file contents into the prompt here
I did try to google it, but to no avail and I know, I could just create a script from some_temp_file but I want the content to appear at the command prompt instead. So is there a way?
regards,
mox
Last edited by Mox (2014-03-07 15:46:09)
Offline
I have no idea what do you want to do.
Can you explain it more + why would you do that / that way?
If you source the file where you have your prompt defined, that should do the trick.
Last edited by karol (2014-03-04 15:39:22)
Offline
Karol, I don't think the OP wants it in the *prompt* (though that's how I read the title), I think the OP wants the contents appended to the command line (e.g. pasted, ready for execution).
This isn't a full solution as I don't use which with aliases (and I can't seem to get it to work with aliases ... are you using bash's which, or which which?) But if you use vi(m)-mode for readline, then after writing to the tempfile you can do <esc> <v> to edit the command line in vim, and you could define a vim macro to read in the temp file ( :r /path/to/tmpfile )
EDIT: you may also consider xsel if this is in X. Rather than piping to a temp file, you can pipe to xsel, then paste directly onto the command line or into vim.
Last edited by Trilby (2014-03-04 15:58:23)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
You can periodically check if some_temp_file is empty, and paste the contents if it's not.
You can edit commands right in the command line, if you use bash. I think zsh may let you do that too.
Last edited by karol (2014-03-04 15:55:29)
Offline
In bash you can do samething like this:
read -ep "Edit and execute: " -i "$(command -v <something>)" COMMAND
eval "$COMMAND"
Last edited by progandy (2014-03-04 16:13:04)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Online
Hi, thanks for all the answers.
Trilby is right, I don't want it within the prompt, but where commands are entered.
@Karol: Of course I know, I can edit commands in the command line (and I use vi-mode and <esc>v ) but that will only edit commands I have already entered into the command line.
The use case I described is different: Say I have defined this alias:
$ which tce
tce: aliased to [[ -e /dev/sdb3 ]] && truecrypt -k "" --fs-options=users,uid=$(id -u),gid=$(id -g),fmask=0113,dmask=0000 --mount /dev/sdb3 /mnt/tce
Which works when sdb3 is formated to ntfs, but now I want to run the same command without the fs-options part. I cannot just edit some command in the command line, because it just is not there. So this is the real challenge: Get this command into the command line like this
$ [[ -e /dev/sdb3 ]] && truecrypt -k "" --mount /dev/sdb3 /mnt/tce
without using the mouse. And, if possible without doind periodic checking for file contents or so. I think this is just as inelegant as using the mouse.
@progandy: Yes, this might be a step in the right direction. But what do the -i and -v options do? I did not find any documentation in the manpage. And is there a way to expand $COMMAND in the command line before running $COMMAND?
Offline
But what do the -i and -v options do? I did not find any documentation in the manpage.
POSIX's read does not have those flags, but bash's builtin does, so check out the bash man page (line 29.27).
Pipe the result of `which` into xsel, then on the command line type Shift-Insert.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
@progandy: Yes, this might be a step in the right direction. But what do the -i and -v options do? I did not find any documentation in the manpage. And is there a way to expand $COMMAND in the command line before running $COMMAND?
Why do you want to have the command in a prompt twice? "read -er COMMAND && eval "$COMMAND"" creates a prompt-like environment and then executes the input string. What the options do you can read in the bash-internal help, e.g.:
$ LC_ALL=C help read
...
-e use Readline to obtain the line in an interactive shell
-i text Use TEXT as the initial text for Readline
...
-p prompt output the string PROMPT without a trailing newline before
attempting to read
-r do not allow backslashes to escape any characters
...
Here is an example:
[progandy@pamobile ~]$ alias some_test='echo -e Hello WORLD"!"'
[progandy@pamobile ~]$ command_to_edit="some_test"
[progandy@pamobile ~]$ read -erp "Edit and execute: " -i "$(command -v $command_to_edit)" COMMAND
Edit and execute: echo -e Hello WORLD"\t!"
[progandy@pamobile ~]$ ####### in the line above you edit the command and bring it in the final form with all necessary paramters.
[progandy@pamobile ~]$ echo -E " *~* Now running: $COMMAND"
*~* Now running: echo -e Hello WORLD"\t!"
[progandy@pamobile ~]$ eval "$COMMAND"
Hello WORLD !
[progandy@pamobile ~]$
Last edited by progandy (2014-03-05 05:21:39)
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Online
Why don't you just retrieve the command from your shell history and then edit it from there?
<Ctrl-R>[[ <Escape>v (edit away...) :wq
Offline
I forgot that there is a simple keybinding to expand an alias: enter the alias and press ctrl-alt-e
| alias CUTF='LANG=en_XX.UTF-8@POSIX ' |
Online
@progandy:
Well no, I did not mean to have the command in the command prompt twice. I just wanted to be able to change it before it is executed. So your example answers my question. Thank you very much!
The keybinding to expand aliases does not work for me, but I guess, I will have to search my bashrc, maybe something prevents it usage there. If I get this to work, it will be an even better solution for this use case, I think.
So I think this thread should be closed. Thank You all.
p.S.> I don't want to search the command from history, because I restrict my history to the current session so there is a good chance the command will not be in the history.
Offline
See the bind built-in in the Bash manual. You can use it to list existing readline functions and their mappings, and also to set them up.
"alias-expand-line" and "shell-expand-line" are (I think) the two you might be interested in.
"...one cannot be angry when one looks at a penguin." - John Ruskin
"Life in general is a bit shit, and so too is the internet. And that's all there is." - scepticisle
Offline
I use the X-only-terminal-script
#!/bin/bash
inp=`cat`;
read cmd < <(tr '\n' ' ' <<< $inp);
xdotool type --clearmodifiers --delay 0 "$cmd";
xdotool key --delay 0 ctrl+l
The last line is awful, but an output of the script without it is more awful :(
Offline
Instead of an alias, just use a simple bash script like the below which uses findmnt to determine the filesystem type, then executes the appropriate command based on that type.
#!/usr/bin/bash
partition="/dev/${1:-sdb3}"
[[ -e "$partition" ]] || exit
fstype=$(findmnt -no FSTYPE "$partition")
declare -A opts=([ntfs]=" --fs-options=users,uid=$(id -u),gid=$(id -g),fmask=0113,dmask=0000")
printf -v cmd 'truecrypt -k ""%s --mount %s /mnt/tce' "${opts[$fstype]}" "$partition"
$cmd
If you want to use this on a partition other than sdb3, you can pass the partition name as a parameter e.g.:
$ tce sdb4
Offline