Create a Zsh Function to Rename Your Current Directory

John Lindquist
InstructorJohn Lindquist
Share this video with your friends

Social Share Links

Send Tweet
Published 4 years ago
Updated 3 years ago

A common problem you'll run into when working inside of your terminal is the desire to rename the directory you're currently in. This is achieved in 3 steps: back out of the directory, mv to a new name, then back inside of it. Rather than 3 separate commands, let's take this down to a single command in a Zsh function. This lesson walks you through the process of creating a Zsh function to rename your current directory by using ${PWD##*/} to find the name of the directory your in then walking through the steps.

John Lindquist: [0:00] You may find that when you create a new directory, you accidentally name it incorrectly. Most people in this situation would back up one directory, then type mv and rename that to the name that you wanted to name it to. Then, you go back into the directory. Now you're back in the directory where you can start writing code and doing whatever you need to do.

[0:22] We can automate these steps by knowing which directory we're in. If we create this bad-project-name, we can find the name of the directory with $PWD and you can see the entire path of the directory that we're in.

[0:36] To get just the name of the current directory, we're going to essentially chomp away until we find a forward slash. That looks like this, where you wrap {PWD} inside of curly braces, type PWD, and then the chompers, and then the regex is any character until we find a forward slash. If I echo that out, that will give me the name of the current directory by itself, without the rest of the path.

[1:04] Knowing the name of the current directory, we can do the remaining steps. Let's do all of that inside of a function. Inside of my Zsh functions folder, I'll create a rename-current-dir function. We'll start with this command, paste that in there. We'll assign that to a variable of currentDir.

[1:26] After we grab the current directory, we can go up one directory. Then, issue that mv command. We want to rename the currentDir to the argument that we pass in and $1 will represent that argument. We're saying change current to this new value that we'll pass in. Then, we can go back into the value that we pass in.

[1:49] Let's save there, clear up my console, and the autoload rename-current-dir loads up the function. Now, inside of bad-project-name, I can type rename-current-dir to good-project-name. Hit Enter. Now, I'm inside of the folder called good-project-name. The folder bad-project-name no longer exists. If I try to even search for it, you won't see anything with bad. You'll only see one with good.

OmarAguinaga
OmarAguinaga
~ 4 years ago

I was getting this error:

rename-current-dir:1: command not found: bad-project-name
usage: mv [-f | -i | -n] [-v] source target
       mv [-f | -i | -n] [-v] source ... directory

I had to change it to:

currentDir=$(echo ${PWD##*/})
cd ..
mv $currentDir $1
cd $1
Markdown supported.
Become a member to join the discussionEnroll Today