Shell function to create a menu of items which may be inserted into the X paste buffer.

smenu() ( IFS=',' ; select x in $*; do echo "$x" | xsel -i; done )
The function will take a comma separated list of items to be 'selected' by xsel -i: smenu "First item to paste,Paste me #2,Third menu item" You will then be prompted to choose one of the menu items. After you choose, you will be able to paste the string by clicking the middle mouse button. The menu will keep prompting you to choose menu items until you break out with Control-C.
Sample Output
> smenu "First item to paste,Paste me #2,Third menu item"
1) First item to paste
2) Paste me #2
3) Third menu item
#?

2
By: bartonski
2010-02-08 15:33:53

What Others Think

You can override the default select prompt (#?) by setting the PS3 variable in the subshell. Also, you need a break in your select command if you want it to ever exit. Something like this: smenu() ( IFS=',' ; PS3='Select an item to insert into the paste buffer: ' ; select x in $*; do echo "$x" | xsel -i ; break ; done ) Or, you could even have "Quit" be one of the selections: smenu() ( IFS=',' ; PS3='Select an item to insert into the paste buffer: ' ; select x in $* Quit; do [ "$x" == "Quit" ] && break ; echo "$x" | xsel -i ; done )
eightmillion · 592 weeks and 5 days ago
8M: For some reason, I never broke out of the select loop in your first version. I was expecting to enter a value, press enter, then exit the loop on the 'break' statement. It never exited. The second version worked like a charm.
bartonski · 592 weeks and 5 days ago
That's weird. What shell are you using? I didn't have any problems with bash, zsh, or ksh.
eightmillion · 592 weeks and 5 days ago
bash version 3.1.17... Hmmm. Tried it again. worked. Go figure.
bartonski · 592 weeks and 5 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this?

commandlinefu.com is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands



Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: