Wednesday, August 13, 2014

Another .inputrc feature

I'd also like to mention another useful application of .inputrc.
It's the ability to attribute a command to a key press: Command Binding.

The typical case is a frequent command and an unused function key.
Lets say, just for example, prtstat -J and the F5 key.
Initially, F5 is unbound.

As per my keyboard definitions, pressing ^V+F5 under an X terminal I get:
(under a sun-color console terminal the code is different: \e[244z)

# ^[[15~

NOTE
For the BASH READLINE, the sequence ^[ is denoted by \e. The codes that are generated may depend on many filtering layers. This can widely vary depending on particular configurations, resulting on a lot of frustration. For a sun-color terminal one way to better take control of this is presented here: Keyboard - Layouts.

The desired binding must be described on .inputrc:
(assuming that the keyboard was configured as on the above link)

# cat ~/.inputrc
...
$if term=xterm
...
"\e[15~": "prstat -J \n"
...
$endif
...
$if term=sun-color
...
"\e[244z": "prstat -J \n
...
$endif
...

Now, every time I press F5 that command will be executed:

   PID USERNAME  SIZE   RSS STATE   PRI NICE      TIME  CPU PROCESS/NLWP     
  1306 user1      11M 3312K cpu0     49    0   0:00:00 0.2% prstat/1
  1252 user1      19M 5032K sleep    59    0   0:00:00 0.0% sshd/1
  1253 user1      11M 2828K sleep    49    0   0:00:00 0.0% bash/1
   417 root       15M 3608K sleep    59    0   0:00:02 0.0% nscd/26
   445 root       11M 1996K sleep    59    0   0:00:01 0.0% VBoxService/7
     5 root        0K    0K sleep    99  -20   0:00:05 0.0% zpool-rpool/147
  1277 user1      11M 2944K sleep    59    0   0:00:00 0.0% bash/1
   223 root       14M 4432K sleep    59    0   0:00:01 0.0% devfsadm/8
    15 root       20M   19M sleep    59    0   0:00:41 0.0% svc.configd/26
   591 root     7132K 1980K sleep    59    0   0:00:00 0.0% sendmail/1
   598 root       99M   13M sleep    59    0   0:00:02 0.0% fmd/34
   476 root     4272K 2296K sleep    59    0   0:00:00 0.0% hald-addon-acpi/1
    96 root       10M 1488K sleep    59    0   0:00:00 0.0% in.mpathd/1
    13 root       28M   19M sleep    59    0   0:00:18 0.0% svc.startd/14
   469 root     8688K 6588K sleep    59    0   0:00:00 0.0% hald/4
   406 root     8848K  932K sleep    59    0   0:00:00 0.0% iscsid/2
   107 root     2696K 1424K sleep    59    0   0:00:00 0.0% pfexecd/3
    74 daemon     10M 1436K sleep    60  -20   0:00:00 0.0% kcfd/3
    92 root     4032K 2320K sleep    59    0   0:00:00 0.0% svc.periodicd/4
    67 netadm   4992K 3232K sleep    59    0   0:00:00 0.0% ipmgmtd/6
   169 root     2748K 1392K sleep    60  -20   0:00:00 0.0% zonestatd/5
PROJID    NPROC  SWAP   RSS MEMORY      TIME  CPU PROJECT                    
    10        5 6660K   17M   1.1%   0:00:00 0.3% group.staff                
     0       59  170M  116M   7.5%   0:01:13 0.1% system                     
     3        1  320K 1860K   0.1%   0:00:00 0.0% default  
                  


This is extremely handy, frequent commands with a single key!