5

Lets say I have a script, "myscript.sh", with contents being simply echo $PWD. I'd like to bind somehow this script to a key combo in bash (gnome-terminal) - so that when I press this key combination, the output of "myscript.sh" is inserted ("pasted") at the cursor position in the terminal.

Apparently, bash history and line manipulation is handled by readline - and the references I got for bash keyboard shortcuts, do reference readline:

I've also seen in Bash Reference Manual: Readline Init File Syntax that the key bindings for bash can be listed by using bind -p (see help bind [not 'man bind'] for more). So maybe this question would better be titled as "_binding macros to custom keyboard shortcuts in readline" :) But in any case, is what I want possible to do?

I guess an alternative would be to have the script be something like "pwd | xsel -b", and then I call it on terminal - and I can paste afterwards; but I'd still like a single keyboard shortcut instead, say like Ctrl-Alt-H (which seems to be not used for anything), which will immediately insert/paste script output when pressed.

Thanks in advance,
Cheers!

 
EDIT: Just to clarify - here is my use case where I'd like this facility. I'm usually cd'd in a project folder, usually named something like myproject-folder-0012a, which is under revision control by svn. And there is a bunch of these folders. So quite often, I do commits where the first word of the message is the directory name, as in:

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

But that is what I don't like - first I type 11 characters, which go rather fast:

svn ci -m "

And then, I cannot use autocompletion to get the name (i'm inside the folder) - which means I either have to fully type it (no way :)), or I copy paste it from the prompt (which requires selection - press mouse, drag, release mouse; then Ctrl+Shift+C, and then Ctrl+Shift+V, plus any left/right keys if I miss allignment - plus deletions and such if I make the copy wrong).

Meaning - so much work, just to get the bloody folder name for a bloody commit message :( I'd MUCH rather press something like (say) Ctrl-Alt-H, and have the folder name automatically inserted at cursor position, and be done with it :)

My suggestion for xsel is only because I could put it into a "global" script - say symlink it as /usr/bin/myscript (and obviously, the contents of the script are echo $(basename $PWD) rather than just pwd for my needs), and then I could do:

$ 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"

... which sort of makes the workload less, but still - then I have to remember what the script name is, and call it, before I type the svn command (and I don't always remember that)... And still - I have to call a command, and then press a key combo; why shouldn't I just press a key combo once, and be done with it ??! :)

Well, hope this clarifies my problem a bit better ....

 
EDIT2: However, another reason why a bash keyboard shortcut would be useful, is that then I could also "paste/insert current directory name" not only in shell commands - but also in terminal programs, say like nano (where it would, arguably, be more difficult to use bash script or function expansion directly).

sdaau
  • 36,975
  • 46
  • 198
  • 278
  • 2
    Sounds like something for SuperUser. – Sebastian Paaske Tørholm Nov 07 '10 at 22:16
  • 1
    You don't need to do the `pwd | xsel -b` and paste combo. If you surround something with backticks, the output from the backticked commands will be used as input on the executed command. – Reese Moore Nov 07 '10 at 22:20
  • HI Sebastian, thanks for that - can I move the post somehow easily - or do I have to open a new one there, and paste over? Thanks, cheers! – sdaau Nov 07 '10 at 22:20
  • Hi Reese, I'm not sure how the backticks would be applicable to my problem, so I tried to clarify my problem in the edit above... It's all about typing less :) – sdaau Nov 07 '10 at 22:42
  • 3
    As an alternative how about a shell function: `svnci() { svn ci -m "$PWD: $1" "${@:2}" ; }`. Used as `$ svnci "here a commit message" ` – camh Nov 07 '10 at 23:05
  • interesting suggestion, camh - thanks for that! I'll definitely take it into consideration... – sdaau Nov 07 '10 at 23:14
  • One other suggestion that comes to mind, just use $PWD in your commit message (if it's all on the command line). Its easier to type than the long directory and no other changes need to be made. Edit: Of course $PWD is the full path, not the last element. For that you need `${PWD##*/}` – camh Nov 07 '10 at 23:39
  • Maybe a silly question, but... *why do you want to put $PWD in your commit message anyway*? I've never used `svn`, but at least for `git` this would be pretty pointless – MestreLion Nov 22 '12 at 15:54

3 Answers3

10

Simple version:

This command at a shell prompt:

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

or this line added to your ~/.inputrc:

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

will cause Alt-e to insert the basename of the current directory on the command line. It requires that the default binding of the readline function shell-expand-line which is \e\C-e be present (this could be adapted if it's different). I'm also making the assumption that you're using Bash's emacs mode.

Unfortunately, it causes things that have already been typed to be expanded as well. One of the affects of this is that after having typed:

svn ci -m "

and pressing Alt-e, the quotation mark will have disappeared. There are a couple of ways to deal with this.

One, assume that all you'll lose is the quote and either manually add it back or have the readline macro add it for you:

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

which just isn't very satisfactory.

Advanced version:

Or, two, kill the line, do the insertion, then yank the line back:

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

or

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

This leaves the rest of the line intact (nothing else is expanded or deleted), but it uses the kill ring, so it may leave it in a state that's different than you expect (if you're using it). It also inserts a space after the inserted directory name (the spaces in the macro are used to ensure that older kill-ring contents are not regurgitated if the macro is executed at the beginning or end of the line). The macro should work regardless of the position of the cursor in the line. The insertion will be made at the cursor's position, leaving the cursor in the same position [in the first version].

Edit: The second version leaves the cursor after the dirname and space that are inserted.

Edit 2:

The readline function shell-forward-word (unbound) does a better job than forward-word (\ef) for this. You can make use of that like this:

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"'

By the way, you should know that Bash keyboard shortcuts are not active in other programs such as nano.

Community
  • 1
  • 1
Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Doing it this way makes the functionality available on the command line for any usage without having to modify and maintain multiple completion functions. – Dennis Williamson Nov 08 '10 at 03:21
  • Wow, Dennis, thanks for the AWESOME answer (_I would have never been able to read myself through the documentation for this :)_). Few things: first, Alt-e is not good for `gnome-terminal` - as it is bound to open Edit menu; Alt-o is availabe, though. I just tried first `bind` "at a shell prompt" with Alt-e, and nothing happened; and `bind -P` didn't get Alt-e listed. Then, I added `"\eo": "${PWD##*/}\e\C-e"` to a new `~/.inputrc`, started a new terminal, and Alt-o works (_NOTE: your entry has a `'` at start - which binds key `'` to commands, not Alt-e!_). But, it works - thanks! – sdaau Nov 08 '10 at 09:07
  • PS: `bind -P` will not get Alt-o (`\eo`, as I understand it) listed, even when Alt-o is active!? Can you recommend a way of how to check which keybindings are available to `bash`/`readline` - _including the custom ones_? Also, thanks for the note for `nano`.. Cheers! – sdaau Nov 08 '10 at 09:11
  • PSS: thanks for **edit 2** / `shell-forward-word` - works great :) Cheers! EDIT: found it, it is `bind -S` that lists keys bound to macros... – sdaau Nov 08 '10 at 09:17
  • PSSS: Just experimented with disabling and reenabling the `~/.inputrc` (_`mv ~/.inputrc ~/_inputrc` and restart terminal_), and now can see that when there are no macros defined there, then Ctrl+Alt+Left/RightArrow work fine (i.e. skip a word) - but when macro is enabled, this key combo doesn't skip words, but it generates "`;5C;5D`" instead ... – sdaau Nov 08 '10 at 09:29
  • PSSSS: Well, pasting the entries to `sudo nano /etc/inputrc` instead of `~/.inputrc` - preserves both Ctrl+Alt+Left/RightArrow, and works with the newly bound `shell-forward-word`... Nice ! :) Thanks again, cheers! – sdaau Nov 08 '10 at 09:46
  • 1
    @sdaau: Sorry about the typo of the stray single quote. Note that `man readline` says that if `~/.inputrc` exists, it is read. Only if it doesn't, then `/etc/inputrc` is read. You can include the latter in the former by making the first line of `~/.inputrc` say `$include /etc/inputrc` then doing overrides and additions after that. Here's a handy command to use when looking for unbound keystrokes: `bind -psv | LC_COLLATE=C sort | less`. – Dennis Williamson Nov 08 '10 at 11:13
0

Ok, not really an answer, but I'd just like to summarize the comments I got so far, which are useful for my problem. However, the question as it stands - in respect to bash keyboard shortcuts running arbitrary scripts - is still not answered (I'd still prefer doing all this with a single key combo :))

 
First, I can use a 'global' script like:

$ 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'

Or, I can add bash functions to my .bashrc (note: make sure you reload bash after you add these lines to .bashrc - for example, simply by typing bash in your current terminal):

$ 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

 
Basically, I misunderstood Reese Moore's suggestion originally - you can indeed use backticks - consider this command session (after the above commands have been ran):

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

This is what I needed to understand Moore's "the output from the backticked commands will be used as input on the executed command" (however, one also needs to take care to clean the line endings from the output); or, in my case, I can call

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

... or, I could follow camh's suggestion, and use svnci-m as a function (in my case, I almost never use additional arguments to svn ci, and so my version is slightly different). And to test whether arguments are passed correctly, I can use the svnci-test function:

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

 
Thanks for the comments so far,
Cheers!

sdaau
  • 36,975
  • 46
  • 198
  • 278
0

One way to do what you want with a single key press is to take advantage of programmable completion in bash. You possibly have some programmable completion set up with the bash_completion tool/package. If not, look into that to see the specifics of how it is done.

The idea is to have the programmable completion recognise when you have hit at the start of a svn commit message and then have it return a single completion which is the text you want to insert (the basename of the current directory).

I've only dabbled with programmable completion so I can't give you the details, but the above-mentioned bash_completion package or the subversion completion script may be a good start.

camh
  • 40,988
  • 13
  • 62
  • 70