Rename file in PHPStorm without refactoring
Asked Answered
J

5

28

How do I quickly rename a file name without refactoring in PHPStorm? Even after I've just created the file, PHPStorm takes a long time to search through my files for its usage.

Jarv answered 19/10, 2011 at 6:19 Comment(0)
T
2

There is also an option available Right-click on file select "Show In Explorer" and then in windows explorer rename file by F2. It will not refactor or effect any other file in your project.

Toxicology answered 13/3, 2020 at 8:47 Comment(1)
This is eventually the fastest way when you are confident that you just want to rename and your renaming will not break the code.Jarv
C
30

My technique for renaming files quickly is to single-click the file name and press Shift-F6. A dialog opens and you can immediately type the new name of the file and hit return/enter to complete the rename.

I find this technique intuitive because you don't have to move your mouse to the dialog window, type the new name, or click anything to confirm. As long as all you're doing is a rename that you know is safe, you just type it and go.

I also like this feature because you still have the option of quickly previewing the changes before they're made. And on those occasions when you are refactoring something with possible implications to the rest of your app, you can still enable "Search for references" and/or "Search in comments and strings" to get the whole enchilada. (I have those options disabled by default.)

I agree with you that right-clicking the file name, scrolling down to Refactor, selecting Rename, and then entering the new name is cumbersome, especially if the safe checks are enabled. I find that PhpStorm developers seem to understand these types of issues and usually have thought of a "faster way" to do things as well.

Carbrey answered 24/10, 2011 at 22:40 Comment(1)
You can also use mnemonics(Alt + <underlined_letter>) to quickly enable/disable checkboxes.Medicare
D
11

You can't do it without refactoring, though Refactor | Rename is usually very quick and you can disable options to search for references.

Dillingham answered 19/10, 2011 at 7:26 Comment(2)
sometimes I create a new file and then i want change it's name. Although the file just is created the refactorer report a lot of refrences to it?!Jarv
False references can be found if you search for them in comments and the name of file is some common word, so just disable both checkboxes in the Rename dialog.Dillingham
H
6

Default is Shift-F6. For not refactoring, disable the checkboxes in the popup.

I wanted to use F2 for file renaming like Windows default.

You can make a shortcut for F2. Go to:

  1. File
  2. Settings
  3. Keymap
  4. Search for "rename"
  5. Doubleclick "Refactor -> Rename"
  6. Click "Keyboard Shortcut"
  7. Press F2
  8. Ok

EDIT

If it does not work immediately, restart PHPStorm.

Herbart answered 27/4, 2016 at 13:34 Comment(3)
+1 This is why I came here. Google brought me, and disappointed I am not. F2 is for me the expected shortcut. Who ever heard of Shift+f6 for rename operations?Viridian
Weird, not sure why this shortcut needs a restart, while others don't.Frediafredie
I did all of this but forgot to restart phpstorm .. :(While
T
2

There is also an option available Right-click on file select "Show In Explorer" and then in windows explorer rename file by F2. It will not refactor or effect any other file in your project.

Toxicology answered 13/3, 2020 at 8:47 Comment(1)
This is eventually the fastest way when you are confident that you just want to rename and your renaming will not break the code.Jarv
C
1

This will work for linux users.

It depends on zenity, so first check if it installed on your system,
run in console: zenity --version
If it output something like 3.18.1.1 - then it's ok.

Create file idea-rename.sh with content below:

#!/usr/bin/env bash

FileDir="$1"
FileName="$2"

NewFileName="$(zenity --entry --title "Rename ${FileName}" --text "Please enter new name" --entry-text "${FileName}" --ok-label="Rename" 2>/dev/null)"

if [[ "${NewFileName}" != "" ]]; then
    if [[ -f "${FileDir}/${FileName}" ]]; then
        path="${FileDir}"
        name="${NewFileName}"

        if [[ "${NewFileName}" == *"/"* ]]; then
            IFS='/' read -ra newDirs <<< "$NewFileName"
            IFS='/' read -ra dirs <<< "$FileDir"

            last=$((${#newDirs[@]} - 1))
            path=""
            name="${newDirs[$last]}"
            unset 'newDirs[last]'

            if [[ "${NewFileName}" == *"./"* ]]; then
                for i in "${!newDirs[@]}"; do
                    dir="${newDirs[i]}"

                    if [[ "$dir" == ".." ]]; then
                        unset 'newDirs[i]'
                        j=$i

                        while [ $j -ge 0 ]; do
                            j=$(($j-1))

                            if [ $j -lt 0 ]; then
                                for (( k=${#dirs[@]}-1 ; k>=0 ; k-- )); do
                                    if [[ "${dirs[k]}" ]]; then
                                        echo 'dirs[k]'
                                        unset 'dirs[k]'
                                        break
                                    fi
                                done
                            elif [[ "${newDirs[j]}" ]]; then
                                echo 'newDirs[j]'
                                unset 'newDirs[j]'
                                break
                            fi
                        done

                    elif [[ "$dir" == "." ]]; then
                        unset 'newDirs[i]'
                    fi
                done
            fi

            for dir in "${dirs[@]}"; do
                if [[ "$dir" ]]; then
                    path="${path}/${dir}"

                    if [[ ! -d "$path" ]]; then
                        mkdir -m 0777 "$path"
                    fi
                fi
            done

            for dir in "${newDirs[@]}"; do
                if [[ "$dir" ]]; then
                    path="${path}/${dir}"

                    if [[ ! -d "$path" ]]; then
                        mkdir -m 0777 "$path"
                    fi
                fi
            done
        fi

        if [[ ! -f "${FileDir}/${NewFileName}" ]]; then
            mv "${FileDir}/${FileName}" "${path}/${name}"
        fi
    fi
fi

Open your Settings > Tools > External Tools, click on add button
and fill form as below

Adding tool

Name: Rename
Group: File
Program: /bin/bash
Parameters: /path/to/idea-rename.sh $FileDir$ $FileName$
Working Directory: $ProjectFileDir$

Click Ok, Apply
After that you can see new item in your file menu

Menu

When you click on rename will opens dialog

Rename dialog

After rename in some cases phpstorm not sync files, so in path where located your file need to click in file menu "Synchronize 'path'"

Synchronize path menu

Complot answered 26/3, 2017 at 3:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.