Linux Fu: A Little Help For Bash

It isn’t uncommon these days for a programmer’s editor to offer you help about what you are typing, ranging from a pop up with choices to a full-blown code template. If you have written a million lines of code in the language, this might even annoy you. However, if you use it only occasionally, these can be very helpful. I’ve used Unix and Linux for many years, but I realize that there are people who don’t use it every day. With the Raspberry Pi, Linux servers, and Windows 10 having a bash shell, there are more people using a shell “every once in a while” than ever before. Could you use a little help? If so, you might try bashelp: a little something I put together while writing about bash completion.

There’s good news and bad news. The good news is that Unix has a built-in help command — man — and has for some time. The bad news is that you need to stop what you are typing and enter a man command to use it. Man, by the way, is short for manual.

There are GUI front-ends to man (like yelp, on the left) and you can even use a web browser locally or remotely. However, none of these are connected to what you are typing. You have to move to another window, enter your search term, then go back to your typing. That got me to thinking about how to get a sort of context-sensitive inline help for bash.

As with tab completion, there are two parts to consider in creating a help function: a shell function to get help, and a means of telling readline to call that function. The bashelp.sh script provides both. However, it only works if $DISPLAY is set, indicating you are using a GUI terminal. The Mac, by the way, doesn’t do GUI this way, so if you are trying to get it to work on a Mac, you’d need to change that in a few places. If you do and you get it to work, I’d love to see a pull request or a fork.

The script lets you set a “help” character. I picked Control+Y which the bind command thinks is “\C-Y.” You’ll find that near the top of the script and you can change it if you want. There are also a few configuration items so you can run man in a new terminal or you can run a graphical man replacement. See the readme for details on configuration.

The function is pretty simple.  It bails if you don’t have X (again, looking at $DISPLAY). It also expects line data from readline in $READLINE_LINE and $READLINE_POINT. If there’s no line, the script quits. The function then takes the first word of the line up to a space as a token and then parses all the options to launch the right man viewer.

Of course, this is just one thing you could do by subverting readline. It is a handy trick to have in your toolbox if you use or write scripts for bash.

If you want more bash customization, you can always pick out a new prompt. If you’d like to make your help script bounce the focus back to your original window or close under certain circumstances, you might want to read up on commanding GUI windows.

23 thoughts on “Linux Fu: A Little Help For Bash

    1. That is great. I need this badly for all things keyboard input. I’m a terrible coder because my brain can easily absorb information but likes to put fuzzy labels on it where the connections to other information is more important than the exact format. I have done projects

  1. Default binding is C-y? Paste in shell broken? “Great”
    man bash, search READLINE, scroll down until the default are listed… pick something not listed. C-x h (as in help) or C-x m (as in man) or C-x C-h (emacs style).

    And now you can write a Hackaday article about copy paste in shell (bash readline C-w C-w … then C-y), terms and such. If you consult the JWZ document about X11 Clipboard and Primary (and Secondary), even better, so you cover X11 side too (urxvt lets you operate on all them, quickly with keys, unlike other terminals), and discover why sometimes us X11ers like it (it’s darn fast interaction).

    But there is an even better way for what you propose when running in GUIs: NeXTStep Services and tools that mimic them (Sawfish window manager has one such module). Select some text, hit some keycombos and the system launches the proper tool using the selection. Another win for X11 selection.

    1. Wow. Well, I don’t need to explain to you why I picked ^Y — there are historical reasons I liked it myself, however I do show you how to change it so knock yourself out. If you are talking about that breaking ^Y in emacs key bindings (it is hard to tell what you are talking about) then perhaps you should realize that not everyone uses emacs keybindings and most people are smart enough to change it to what they want anyway.

      Otherwise, I have no idea what you are talking about with the copy/paste stuff. Yes, there are lots of ways to do that and it does depend on your setup. I use rxvt myself, by the way.

      As for NeXTStep, um… yeah well there are a lot of arguments like that. Instead of a bicycle get a jeep! Yeah, sure. I actually like the NeXT and occasionally still miss the shelf. But to tell everyone who uses bash to switch to NeXTStep is… um… well…. it just isn’t what I wanted to accomplish here.

      I mean, seriously, pick your own key combo and set it up. For the record, since this was aimed at showing how to do it, I didn’t want to do a two key mapping even though I could do it. I also didn’t want the obvious Control-H because that’s backspace.

      Hey, here’s an idea. It is on Github. Fork it and do whatever you want with it.

      1. C-y in bash readline is default bindings. Write some words, C-w to “delete”, then C-y. Magic, you paste what you deleted (it was cut, not just delete). Trashing defaults is bad practize when there are other options avaliable.

        With urxvt, try select some text in one app with mouse, select with keyboard (important) and copy command in a third X11 app, then Shift-Insert (insert Primary). Compare to M-C-v (paste Clipboard, at least with default config in this distro).

        It was not about switching to NeXT, but to getting tools inspired in it (IIRC MacOSX also got Services, or can get them… and probably someone coded a stand alone tool for X11 instead of integrated in a WM… or maybe hacked one up via wmctrl, xdotool, xsel, zenity and the rest of forgotten tools). Computers to be used and create, not to be consumer widgets that get less and less useful, no matter how much some of the FOSS comunity seem to be forgotting that, or worse, pushing towards braindead tools.

        So you know C-h is backspace and avoided it. Now you now C-y too. ;)

        1. OK, found the Mac OS X services thing:
          https://www.howtogeek.com/205686/use-your-mac%E2%80%99s-services-menu-to-perform-quick-actions/

          And the “With urxvt” line is about three apps, two in which you pick text differently (mouse selection vs keyboard cut/copy), and one urxvt in which you compare Shift-Ins (= mouse button 2 click) to M-C-v. Some apps are forgetting selection (Firefox, I’m looking at you), and only use clipboard (at random in FF case, which makes the inconsistency a nightmare), so being able to insert their text in terms matters (or you have to use “xsel -o -b | xsel -i -p” first).

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.