Scheduled Maintenance: We are aware of an issue with Google, AOL, and Yahoo services as email providers which are blocking new registrations. We are trying to fix the issue and we have several internal and external support tickets in process to resolve the issue. Please see: viewtopic.php?t=158230

 

 

 

Customizing the URXVT tab bar

Share your HowTo, Documentation, Tips and Tricks. Not for support questions!.
Post Reply
Message
Author
User avatar
dbbolton
Posts: 2129
Joined: 2007-06-20 08:17
Location: Iapetus

Customizing the URXVT tab bar

#1 Post by dbbolton »

If you have installed rxvt-unicode, or built it from source with Perl support,
you can use its tab bar. This is a nice and simple alternative to a multiplexor
like screen, or a tabbed terminal that uses a widget toolkit to create tabs
(such as konsole, xfterm4, and so on).

I didn't like the default appearance of URXVT's tab bar, so I decided to figure
out how to change it.

1. Colors
This is an easy one. You can add the following resources to your
$HOME/.Xdefaults file:

Code: Select all

URxvt.tabbed.tabbar-fg: 3
URxvt.tabbed.tabbar-bg: 0
URxvt.tabbed.tab-fg:    0
URxvt.tabbed.tab-bg:    1
These are the default values (as far as I can determine). Of course, you can
set the color numbers to whatever you want, but hexadecimal values will not
work.

!!! NOTE: The changes below are SYSTEM-WIDE. !!!

2. The '[NEW]' button
This is not so easy. You'll have to edit the Perl file that is used to create
the tabs. First, make a backup copy:

Code: Select all

cp /usr/lib/urxvt/perl/tabbed  $HOME/urxvt_perl_tabbed.original
chmod a-r $HOME/urxvt_perl_tabbed.original
cp $HOME/urxvt_perl_tabbed.original $HOME/urxvt_perl_tabbed
$EDITOR  $HOME/urxvt_perl_tabbed
We actually made two copies, one of which is read-only, and the other one will
be opened for editing. Scroll down to about line 13 and you should see
something like:

Code: Select all

   substr $text, 0, 7, "[NEW] |";
   @$rend[0 .. 5] = ($self->{rs_tab}) x 6;
   push @ofs, [0, 6, sub { $_[0]->new_tab }];

   my $ofs = 7;
   my $idx = 0;
This is where that '[NEW]' button comes from. Change the "[NEW] |" string to
whatever you want, and note the number of characters. In my case, I chose

Code: Select all

substr $text, 0, 7, " + |";
So, my resulting substring is 4 characters in length. Now, you have to edit
the following non-zero integers in this section. In my example, my string was
3 characters less than the original, so, in order to line everything up, I have
to subtract 3 from those integers, and I end up with:

Code: Select all

   substr $text, 0, 4,  " + |";
   @$rend[0 .. 2] = ($self->{rs_tab}) x 3;
   push @ofs, [0, 3, sub { $_[0]->new_tab }];

   my $ofs = 4;
   my $idx = 0;
That wasn't so bad, was it?

You can of course apply my patch in one fell swoop:

Code: Select all

cp /usr/lib/urxvt/perl/tabbed /usr/lib/urxvt/perl/tabbed.orig ; curl https://github.com/dbbolton/urxvt-stuff/raw/cc1e79668b8d18955ad8720295915f42b4086fc2/tabbed.patch | patch -p0


3. The tab names
This one isn't exactly a picnic either. Scroll down to around line 20, and you
should see this:

Code: Select all

   for my $tab (@{ $self->{tabs} }) {
      $idx++;

      my $act = $tab->{activity} && $tab != $self->{cur}
                ? "*" : " ";

      my $txt = "$act$idx$act";
      my $len = length $txt;
As you might have guessed, $txt is where the tab names come from. The first one
is 1, the next 2, and so on.

This is where you have some options. If you simply put the post-incrememnt,
$txt++;, after the line where $txt is set, your tabs will start at 0 and go up.
If you wanted to be a bit more creative, you could make a simple hash to store
custom names for the tabs. Here is an example:

Code: Select all

my %tabnames = (
    '1' => 'α',
    '2' => 'β',
    '3' => 'γ',
    '4' => 'δ',
    '5' => 'ε',
);
(I chose Greek letters solely for the sake of Unicode.) I also instantiated a
global variable to hold the current tab's name:

Code: Select all

my $tabtxt;
Next, you'll want to add a safety net in case you end up using more tabs than
you planned:

Code: Select all

if ($tabnames{$idx}) {
    $tabtxt = $tabnames{$idx};
}
else {
    $tabtxt = $idx;
}
I chose to put this if/else lock right above the line on which $txt is defined.
All in all, my changes look like this:

Code: Select all

substr $text, 0, 4,  " + |";
@$rend[0 .. 2] = ($self->{rs_tab}) x 3;
push @ofs, [0, 3, sub { $_[0]->new_tab }];

my $ofs = 4;
my $idx = 0;

my %tabnames = (
    '1' => 'α',
    '2' => 'β',
    '3' => 'γ',
    '4' => 'δ',
    '5' => 'ε',
);

my $tabtxt;

for my $tab (@{ $self->{tabs} }) {
    $idx++;

    my $act = $tab->{activity} && $tab != $self->{cur}
        ? "*" : " ";

    if ($tabnames{$idx}) {
        $tabtxt = $tabnames{$idx};
    }
    else {
        $tabtxt = $idx;
    }

    my $txt = "$act$tabtxt$act";
    my $len = length $txt;
4. The Final Product
Just move the edited file back to the default location (as root):

Code: Select all

# mv -i $HOME/urxvt_perl_tabbed /usr/lib/urxvt/perl/tabbed
A screenshot for you visual creatures:
Image
GitHub | zsh docs in Letter PDF
Telemachus wrote:Put down the CGI.

User avatar
dbbolton
Posts: 2129
Joined: 2007-06-20 08:17
Location: Iapetus

Re: Customizing the URXVT tab bar

#2 Post by dbbolton »

I made a patch file that changes [NEW] to + and also starts the tab numbering at 0: https://github.com/dbb/urxvt-stuff/blob ... bbed.patch
GitHub | zsh docs in Letter PDF
Telemachus wrote:Put down the CGI.

User avatar
kabniel
Posts: 224
Joined: 2007-07-10 21:03
Location: Sweden

Re: Customizing the URXVT tab bar

#3 Post by kabniel »

Nice howto :)
Here are some changes i have made to my tab bar that you may or may not want to cover (i have mostly ripped stuff out):
  • Removed [NEW] button
  • Removed mouse support
  • Changed prev/next/new tab keys from Shift + Left/Right/Down to Ctrl + PgUp/PgDn/Down
  • Removed movable tabs functionality
Sorry for the messy diff, should be fairly obvious though, original code is commented out.

Code: Select all

12,17c12,19
< 
<    substr $text, 0, 7, "[NEW] |";
<    @$rend[0 .. 5] = ($self->{rs_tab}) x 6;
<    push @ofs, [0, 6, sub { $_[0]->new_tab }];
< 
<    my $ofs = 7;
---
> # Remove NEW button
> #   substr $text, 0, 7, "[NEW] |";
> #   @$rend[0 .. 5] = ($self->{rs_tab}) x 6;
> #   push @ofs, [0, 6, sub { $_[0]->new_tab }];
> 
> # Change tabs offset to fill empty space of removed NEW button
> #   my $ofs = 7;
>    my $ofs = 0;
188,205c190,207
< 
< sub on_button_press {
<    1
< }
< 
< sub on_button_release {
<    my ($self, $event) = @_;
< 
<    if ($event->{row} == 0) {
<       for my $button (@{ $self->{tabofs} }) {
<          $button->[2]->($self, $event)
<             if $event->{col} >= $button->[0]
<                && $event->{col} < $button->[1];
<       }
<    }
< 
<    1
< }
---
> # Remove mouse support
> #sub on_button_press {
> #   1
> #}
> #
> #sub on_button_release {
> #   my ($self, $event) = @_;
> #
> #   if ($event->{row} == 0) {
> #      for my $button (@{ $self->{tabofs} }) {
> #         $button->[2]->($self, $event)
> #            if $event->{col} >= $button->[0]
> #               && $event->{col} < $button->[1];
> #      }
> #   }
> #
> #   1
> #}
309,310c311,315
<    if ($event->{state} & urxvt::ShiftMask) {
<       if ($keysym == 0xff51 || $keysym == 0xff53) {
---
> # Change keys from Shift + Left/Right to Ctrl + Pgup/Pgdn
> #   if ($event->{state} & urxvt::ShiftMask) {
> #      if ($keysym == 0xff51 || $keysym == 0xff53) {
>    if ($event->{state} & urxvt::ControlMask) {
>       if ($keysym == 0xff55 || $keysym == 0xff56) {
313,314c318,322
<          --$idx if $keysym == 0xff51;
<          ++$idx if $keysym == 0xff53;
---
> # Change keys from Left/Right to Pgup/Pgdn
> #         --$idx if $keysym == 0xff51;
> #         ++$idx if $keysym == 0xff53;
>          --$idx if $keysym == 0xff55;
>          ++$idx if $keysym == 0xff56;
325,337c333,346
<    elsif ($event->{state} & urxvt::ControlMask) {
<       if ($keysym == 0xff51 || $keysym == 0xff53) {
<          my ($idx1) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
<          my  $idx2  = ($idx1 + ($keysym == 0xff51 ? -1 : +1)) % @{ $self->{tabs} };
< 
<          ($self->{tabs}[$idx1], $self->{tabs}[$idx2]) =
<             ($self->{tabs}[$idx2], $self->{tabs}[$idx1]);
< 
<          $self->make_current ($self->{tabs}[$idx2]);
< 
<          return 1;
<       }
<    }
---
> # Remove movable tabs functionality
> #   elsif ($event->{state} & urxvt::ControlMask) {
> #      if ($keysym == 0xff51 || $keysym == 0xff53) {
> #         my ($idx1) = grep $self->{tabs}[$_] == $tab, 0 .. $#{ $self->{tabs} };
> #         my  $idx2  = ($idx1 + ($keysym == 0xff51 ? -1 : +1)) % @{ $self->{tabs} };
> #
> #         ($self->{tabs}[$idx1], $self->{tabs}[$idx2]) =
> #            ($self->{tabs}[$idx2], $self->{tabs}[$idx1]);
> #
> #         $self->make_current ($self->{tabs}[$idx2]);
> #
> #         return 1;
> #      }
> #   }

Post Reply