Bash: invoking a script using special keyboard shortcuts?

Suppose I have a script, " myscript.sh", and the content is simple echo $PWD. I would like to somehow attach this script to a key combo in bash( gnome-terminal) - so when I press this key combination, the output is " myscript.sh" inserted ("inserted") at the cursor position in the terminal.

Apparently the bashstory and line manipulation are handled by readline - and the links I got for the bashshortcuts make a link readline:

I also saw in the bash Reference Guide: The syntax of the Readline initialization file that key bindings for bashcan be listed in using bind -p(see help bind[not 'man bind'] for more). Therefore, perhaps this question is better called "_binding macros to custom key combinations in readline" :) But in any case, is this what I want to do?

I suppose an alternative would be to have a script something like " pwd | xsel -b", and then I call it on the terminal - and then I can paste it; but I still would like to use only one key combination, for example Ctrl-Alt-H (which, it seems, is not used for anything), which immediately inserts / inserts script output when pressed.

Thanks in advance,
Hooray!

 
EDIT: - , . cd 'd , - myproject-folder-0012a, svn. . , - , :

svn ci -m "myproject-folder-0012a: here a commit message"

, - 11 , :

svn ci -m "

, ( ), , ( :)), ( - , , , Ctrl + Shift + C, Ctrl + Shift + V, /, - , ).

- , :( - () Ctrl-Alt-H , , :)

xsel , "" script - , /usr/bin/myscript (, , script echo $(basename $PWD), pwd ), :

$ myscript       # this puts directory name in clipboard
$ svn ci -m "[CTRL+SHIFT+V TO PASTE HERE]myproject-folder-0012a[NOW TYPE]: here a commit message"

... , - , script, . svn )... - , ; , ?!:)

, , ...

 
EDIT2: , bash , , "/ " , , , nano ( , bash script ).

+3
3

:

:

bind '"\ee": "${PWD##*/}\e\C-e"'

​​ ~/.inputrc:

"\ee": "${PWD##*/}\e\C-e"

Alt - e, . readline shell-expand-line, \e\C-e ( , ). , Bash emacs.

, , , . , :

svn ci -m "

Alt - e, . .

, , , , , readline:

bind '"\ee": "${PWD##*/}\e\C-e\eb\"\C-e"'

.

:

, , , , :

bind '"\ee": " \C-u \C-a\C-k${PWD##*/}\e\C-e\C-y\C-a\C-y\ey\b"'

bind '"\ee": " \C-u \C-a\C-k${PWD##*/}\e\C-e\C-y\C-a\C-y\ey\b\ef\C-f"'

( ), , , ( ). ( , kill-ring , ). . , [ ].

: .

2:

readline shell-forward-word (unbound) , forward-word (\ef). :

bind '"\ew":shell-forward-word'
bind '"\ee": " \C-u \C-a\C-k${PWD##*/}\e\C-e\C-y\C-a\C-y\ey\b\ew\C-f"'

, , Bash , nano.

+8

, , , , . , bash , , - ( :))

 
-, script :

$ sudo bash -c 'cat > /usr/bin/bpwd <<EOF
#!/bin/bash
basepwd=\$(basename \$(pwd))
echo -n \$basepwd                 # suppress line ending
# exec 1>/dev/null               # debug: redir stdout to null
echo -n \$basepwd | xsel -i -b    # suppress LF, and make xsel read from stdin 
# exec 1>/dev/tty                # debug: restore stdout
EOF
chmod +x /usr/bin/bpwd'

bash .bashrc ( : , bash .bashrc - , bash ):

$ echo '
bpwd2() { basepwd=${PWD##*/} ; echo -n $basepwd | xsel -i -b ; echo -n $basepwd ; }
svnci-test() { echo -n "$(bpwd2): $*" ; }
svnci-m() { svn ci -m "$(bpwd2): $*" ; }' >> ~/.bashrc

 
, Reese Moore - backticks - ( ):

$ bpwd
Desktop\
$ bpwd2
Desktop\
$ echo `bpwd`
Desktop
$ echo "`bpwd2` 2"
Desktop 2

, Moore, " " ( , ); , ,

svn ci -m "`bpwd`: my message here"
# svn ci -m "${PWD##*/}: my message here" # alternatively

... camh svnci-m ( svn ci, ). , , svnci-test:

$ svnci-test "my message"
Desktop: my message\

 
,
!

0

, , - bash. , / bash_completion. , , , .

, , svn, , , ( ).

I was only dealing with programmable completion, so I cannot give you the details, but the above bash_completion package or the completion of a subversion script may be a good start.

0
source

Source: https://habr.com/ru/post/1773544/


All Articles