TLDR:
Create this alias into your .aliases/.zshrc/.bashrc or where ever you store aliases for easier use later.
alias cpwd="pwd | tr -d '\n' | pbcopy && echo 'pwd copied to clipboard'"
Recently I have been spending a little more time using the terminal for work and have had a few instances where I needed to copy the current directory I am in. Now in most cases you could just use the mouse to highlight and copy the directory you need, but I am pretty lazy motivated to try and make this a little easier as I suppose there could be a time where using a mouse is not an option.
Parts Breakdown
pwd: print working directory
$ pwd
/User/(username)
Prints the current directory path you are in.
tr: translate characters
tr -d '\n'
This will copy standard input to the standard output with substation or deletion of selected characters.
The pwd command above will produce the working directory string but it will have a line return character '\n' appended to it.
We use the -d command to signal that we want to delete a character off the string and '\n' to signal which character we wish to delete.
Copying to system clipboard
Depending on if you are on OSX or Windows the copy command could be different
Windows
- clip
OSX
- pbcopy
this will copy text to the system clipboard (by default).
echo: Writes to the standard output.
echo 'pwd copied to clipboard'
This part is not needed I just personally like to have something in my commands that will tell me that the command was at least executed.
Pipe it all together
All of these above commands are used with a pipe '|' which will feed the 'output' of one command to the next.
So the flow will be the following.
- pwd prints out the working directory
- That output is now fed to our tr argument which will remove the trialling newline character
- finally that entire string will be placed into our clipboard for use with the help of pbcopy/clip.
- Then an echo at the very end will just print to the screen that the command has been executed
Final
Now this is far from the only way to do this, I believe a popular way would be to just use xclip but that may take an install and I tried to find a way to use only what is available by default.
I hope that someone may find his useful. What are some of the things you have learned in the terminal that has saved you time/made things easier? Let me know! đŸ˜ƒ
Top comments (7)
You don't need the
tr
command. Just use the$PWD
variable like this:Good to know! Thanks for sharing I should have known there was an easier way.đŸ˜ƒ
Yeah. It's the
-n
option ofecho
that removes the newline.Nice. But what clipboard copy analog for Ubuntu / Deb based distributives?
I think a common solution would be to use xclip, but that may have to be installed, I am not positive if that comes along by default, and I do not know what the alternative to clip/pbclip for Ubuntu/Deb if there is one. But the command should still be relatively similar.
Yea unfortunately it doesn't come by default, but I do recommend installing it, it's very useful in my day to day
I think it depends whether you're using X, doesn't it :)
So in an SSH session to another device, a virtual terminal, VM, a Docker container, WSL, or any of the other places people use Linux, it won't work.
I suspect most people who use a Windows or MacOS machine as their daily driver doesn't have X installed on any of the things they use Linux on.