DEV Community

Cover image for Quickly copy path to clipboard
Marcell Cruz
Marcell Cruz

Posted on

Quickly copy path to clipboard

It's very common to have a terminal window opened in a specific folder, and then you realize that you need to do something else in that same folder, then you open a new terminal window and have to navigate to the folder that you were in the previous window, it's happens a lot and it's annoying.

There's a quick and easy way to solve this problem that works in any platform, terminal emulator etc..

Just create a script called pwdc place the following code

#!/usr/bin/bash

pwd | xclip -selection clipboard
Enter fullscreen mode Exit fullscreen mode

What this script does is to copy your current directory to your clipboard
pwd means (print working directory), that's why the name of the script is pwdc, the c stands for copy, but of course, you can name the script whatever you want, after creating the script you need to make it executable

chmod +x ./pwdc
Enter fullscreen mode Exit fullscreen mode

and you also need to install xclip if you don't already have, after that you need to move the script to your path, so you can call it anywhere, and that's it, now you can easily copy you current directory open a new terminal window and navigate to it

Top comments (2)

Collapse
 
thomazella profile image
Thom • Edited

on macos:
use pbcopy and pbpaste!

on node:
I use clipboardy package, it works well. :D

linux tip:
use a clipboard history widget on desktop or something similar! it really helps to re-copy something you copied a while ago.

Collapse
 
arnabsen1729 profile image
Arnab Sen

On the same topic I would like to add, sometimes we run a command and we want to copy the command we just ran onto clipboard. For that I have a solution. In the same way create another bash file and put this piece of code.

#!/usr/bin/bash

history | tail -2 | head -1 | awk '{print $2}' | xclip -selection clipboard

Make it executable in the same way as shown above. If you want to access the file anywhere from the terminal just put it in /bin/ directory.