1

I have to, a lot of times, get things onto the clipboard from the terminal. I do it like this:

echo "something"|xclip -selection clipboard

Doing this every time is way too lame, and I want to make a shortcut. How do I do it?

  • Do you mean a real shortcut (that is, instead of pressing enter you press ctrl+... and then the current command line is executed and its output written to the clipboard)? – Socowi Nov 11 '19 at 20:11
  • Can you clarify your question by saying whether you'd like a shorter version of `xclip -selection clipboard` or just an improved way to copy in the terminal? – ben lemasurier Nov 11 '19 at 20:14
  • basically. I want to make a script, named `cpstring`. If I go and type `cpstring "anything"`, `"anything"` gets copied – prokopvictor Nov 11 '19 at 20:37
  • @prokopvictor What's stopping you? Is your question "How do I access command line parameters in a shell script?" – that other guy Nov 11 '19 at 20:40

2 Answers2

1

You could define a function:

clip() {
    echo "$@" | xclip -selection clipboard   
}

add it to your initialization script (~/.bashrc), then use it:

clip something
clip "one two"
builder-7000
  • 7,131
  • 3
  • 19
  • 43
  • 1
    I think `echo "$@"` would be a bit better. That way you can use `clip` just like `echo`. For instance, you could write `clip a b` instead of `clip "a b"`. – Socowi Nov 11 '19 at 21:24
0

You could use alias. Add below line to ~/.bash_aliases.

alias my_alias_name="xclip -selection clipboard"

Then you go like this:

echo "something" | my_alias_name

You have to come up with good name for that on your own.


Answering your comment:

...the little script i could make is named cpstring and I go cpstring "anything". Is there a way to do this with bash scripting?

Create cpstring file, put it somewhere visible by your $PATH:

#!/bin/bash

echo "$@" | xclip -selection clipboard

Remember to add exe rights to that file: chmod +x cpstring

Dawid Fieluba
  • 1,271
  • 14
  • 34
  • I will probably do this, but, let's say, the little script i could make is named `cpstring` and I go `cpstring "anything"`. Is there a way to do this with bash scripting? – prokopvictor Nov 11 '19 at 20:30