You are not logged in.

#1 2013-04-25 19:41:07

sigma91
Member
Registered: 2010-07-31
Posts: 18

Urxvt: copy current line without mouse

I want Urxvt to copy whatever I just wrote in the terminal without using the mouse, I just want to type a single combination such as Ctrl-m. I'm not sure if there's any easier way to do it, so I tried writing a perl function:

sub tox {
    my ($self) = @_; 
    my $text;
    my $PS1='>>> ';

    # get the last non-empty line
    for (my $i = 0; $i < $self->nrow; $i ++) {
        my $line = $self->line($i);
        next if ($line->beg != $i);
        next unless $line->t;
        $text = $line->t;
    }   

    # remove PS1, rprompt and copy to clipboard
    $text =~ s/$PS1//;
    my $rprompt = ( ${$self->env}{'PWD'} eq ${$self->env}{'HOME'} ? 
        '~' : ${$self->env}{'PWD'} );
    $text =~ s/\s*$rprompt$//;      # remove spaces and rprompt
    system( "echo -en \"" . $text . "\" | xsel -i" );
}

The problem is, I'm not sure how to remove the rprompt, which shows the working directory. I tried to obtain the working directory with ${$self->env}{'PWD'}, but the value seems to be the working directory of the parent terminal. e.g., when I open urxvt, cd to /tmp and open urxvt inside this terminal, then PWD is /tmp. If I cd to /home, PWD remains /tmp. Same problem with $HOME.

If anyone has an idea how to obtain the actual working directory or maybe knows another possibility, I'm glad to hear it.

Last edited by sigma91 (2013-05-02 16:59:47)

Offline

#2 2013-04-25 19:55:59

jasonwryan
Forum & Wiki Admin
From: .nz
Registered: 2009-05-09
Posts: 19,042
Website

Re: Urxvt: copy current line without mouse

Moving to programming and scripting...


Arch + dwm   •   Mercurial repos  •   Github

Registered Linux User #482438

Offline

#3 2013-05-02 09:15:39

ap0calypse
Member
From: Austria
Registered: 2012-03-12
Posts: 54
Website

Re: Urxvt: copy current line without mouse

If you want to copy text from/to yout clipboard (which is what I understood so far), You should look at:

http://search.cpan.org/~king/Clipboard- … ipboard.pm

You can then retrieve the clipboard with Clipboard->paste() and write to it with Clipboard->copy('foo');

Maybe it helps!


projects: whistle | bazinga
open minds, open sources, open future.

Offline

#4 2013-05-02 16:57:37

sigma91
Member
Registered: 2010-07-31
Posts: 18

Re: Urxvt: copy current line without mouse

The above code uses xsel to paste to clipboard:

system( "echo -en \"" . $text . "\" | xsel -i" );

So the perl module doesn't solve my problem.

Here's an example of what I'm trying to achieve:
I'm in the folder /tmp and I just typed 'fdisk -l'. The terminal looks like this:

>>> fdisk -l				/tmp

>>> is my PS1, on the right is my rprompt, which shows the current directory (/tmp). The variable $text gives me this whole string, but I just want to copy 'fdisk -l'. The PS1 can easily be deleted using

$text =~ s/$PS1//;

my question is, how to delete the rprompt?

Offline

Board footer

Powered by FluxBB