How to copy and paste within a terminal in macOS or Linux?

Sometimes we want to store some piece of information while using a terminal, for example, a result of an executed command. We usually save it into some temporary file which is going to be deleted after all. There’s a better way.

Table of contents

    Copying and pasting on macOS

    On macOS, there are two commands - pbcopy and pbpaste which leverage the system’s clipboard.

    The pbcopy command puts input into the clipboard.

    pbcopy “some text

    Piping output works surprisingly well:

    ps aux | pbcopy

    If you want to print the clipboard’s content all you need to do is run pbpaste.

    Let’s say you want to copy and execute the file’s content. Of course, you may just use source but for the sake of this tutorial let’s assume that scenario.

    First, create a file:

    cat > <<EOF
    echo "hello world"

    Then you can copy its content with pbcopy:

    pbcopy <

    The last step is to execute the clipboard’s content:

    pbpaste | zsh

    Copying and pasting on Linux

    You can create pbcopy and pbpaste commands on Linux. All you need to do is to add those two lines to the ~/.bashrc file and restart your terminal:

    alias pbcopy=’xsel --clipboard --inputalias pbpaste=’xsel --clipboard --output

    And… that’s it! I hope that those two commands will enhance your experience with the CLI.

    Mac pbcopy pbcopy debian copy and paste linux copy paste command in linux copy paste terminal linux terminal paste text paste multiple files paste commands overwrite existing files mac pbcopy mac copy file from mac to linux copy from terminal linux copy paste mac cat to clipboard remote server transfer files copy files gui file manager local directory local file

    Artur Ziętkiewicz
    Artur Ziętkiewicz Elixir & React Developer

    Read more
    on #curiosum blog

    TOP Elixir media and resources

    Top Elixir Learning Media & Resources in 2022

    Regardless of whether you've only just heard of the Elixir programming language and would like to learn it, or if you're a seasoned developer with years of experience, you need adequate learning resources to ensure steady progress in your career - and just equally as important is the need to be up to date with what's new & trending in the functional