LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to store the text (selected my mouse button) as a string into a variable? (https://www.linuxquestions.org/questions/linux-newbie-8/how-to-store-the-text-selected-my-mouse-button-as-a-string-into-a-variable-672681/)

santosh045 09-27-2008 05:04 AM

How to store the text (selected my mouse button) as a string into a variable?
 
Hell all,
I want to write a shell script where i will be using the text copied (when a text is selected by mouse button,it gets copied) by mouse.
I just want to know how to store that copied text or string into a variable.

I don't know how to do that.
so you peoples help are required.
plz give the suggesions.

thanx

niceguy_81333 09-27-2008 05:56 AM

hi
I think "paste" command should work for you.
rgds
bil

unSpawn 09-27-2008 07:04 AM

AFAIK the selected text gets copied to some buffer and as such 'paste' wouldn't be able to retrieve it from that buffer automagically?..

john test 09-27-2008 09:26 AM

if you are in "terminal" and you highlight some text at the cli, then it is on the clipboard.
To paste the contents of the clipboard, either "Right Click" or Center Click". This seems to work at the cli as well as in an editor where you would be creating a script

lwasserm 09-27-2008 01:37 PM

You might try a prgram called "xclip" which provides a command line interaface to the X clipboard. It can write clipboard text to/from a file so it should be possible add some scripting that would set a shell variable to the clipboard text. Who knows what would happen however if you had an image or something currently on the clipboard?

santosh045 09-29-2008 12:11 AM

hello all,
i often have to open files with gvim.
for that i first select the file name with mouse.then at the terminal command promt i write gvim and then press mouse middle button.after that hitting the enter will open the file.

i want to write a shell sript and will assign a key combination to that script.
then after selecting the text if i hit the key combination it will open the file.life will become easier for me.

shell script will be like below:


#!/bin/csh

$filename=<here will be the command or procedure which will return the slected text with mouse button>
gvim $filename


I think now my problem will get understood by everyone.

so plz suggest any method so that i can take the selected text into the variable.

regards
santosh

David the H. 09-29-2008 12:35 AM

Yep, you want to use either xclip, as mentioned above, or xsel, which does pretty much the same thing. I prefer xsel myself because it seems to do a more reliable job in some cases.

But in this case, with either one you'll simply want to output the "primary" selection (which is the default value) to the script. You don't even need to use a variable; you can output it straight to the command itself.

Code:

gvim "$(xsel -o)"

or

gvim "$(xclip -o)"

Indeed, in most cases you don't even need a script. You can create a simple menu shortcut and put this line directly in the shortcut command line. Do note that if you don't have a proper filename in the selection buffer, gvim will still open up anyway.

santosh045 09-29-2008 01:59 AM

when i type xsel on comman prompt,it says
xsell:command not found

same with xclip


so the above technique will not work.
is there any other method or i should stop doing such thing.

suggest plz

regards

santosh

David the H. 09-29-2008 02:32 AM

You need to install the program(s) first. They don't come standard.

They should be available in the installable software repositories of your distro, whatever that is.

santosh045 09-29-2008 07:09 AM

hi

thanks for u people's reply.
i got to know many things

regards
santosh


All times are GMT -5. The time now is 11:07 AM.