Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a keyboard shortcut in Pycharm for renaming a specific variable?

I'm using Pycharm for Python coding, and I want to change the name of a specific variable all over the code. is there any keyboard shortcut for this operation?

In Matlab I can use ctrl + shift.

For example:

old_name=5
x=old_name*123

will become:

new_name=5
x=new_name*123

without the need to change both of the old_name references.

Thanks!

like image 601
roishik Avatar asked Sep 28 '16 11:09

roishik


People also ask

How do you change a variable name in python?

Simply put your cursor on a symbol and use Refactor | Rename ( Shift-Ctrl-Alt-T, 1 Win/Linux, Ctrl-T, 1 macOS.) Provide the new name, preview the places it found, and select Do Refactor . Change your mind? Refactoring is done as a single editor transaction, meaning Undo reverts all the places that were renamed.

How do you change the name of a variable?

Right-click the variable or function name and then click Rename.

How do you change the name of a variable in all places?

Visual Studio Code is Ctrl + Shift + L and begin typing. Sublime/Atom are alt + F3. Show activity on this post. If you're using the PyCharm IDE use Mayus + F6 on the variable that you want change and write the new name variable.

How do you Refactor words in PyCharm?

Do one of the following: On the main Refactor menu or from the context menu of the selection, choose the desired refactoring or press the corresponding keyboard shortcut (if any). From the main menu, choose Refactor | Refactor This, or press Ctrl+Alt+Shift+T , and then select the desired refactoring from the popup.


2 Answers

Highlight your old_name and hit Shift+F6

like image 113
Moses Koledoye Avatar answered Sep 19 '22 15:09

Moses Koledoye


I don't know about a shortcut for this special purpose, but I simply use Ctrl+R to replace the old variable names with new ones. You can also set settings such as Match case, Regex or In selection.

Note that this won't work, if you have a variable name including another variable name:

var1 = 0
var1_s = "0"

Replacing var1 to xy would result in :

xy = 0
xy_s = "0"

But that also forces you to do consistent and clear variable naming.

like image 32
linusg Avatar answered Sep 21 '22 15:09

linusg