38

I have a program with many formulas and variables. I need to change the name of some of these variables. Is there any command or function in Spyder that can help me change all the names in one step?

I am looking for something like the Matlab's MAIUSC+ENTER.

0 _
  • 10,524
  • 11
  • 77
  • 109
VitOne
  • 439
  • 1
  • 4
  • 8
  • 10
    CTRL+H was the solution. – VitOne Apr 25 '14 at 14:15
  • Maker sure to include spaces in your search and replacement, like replace ` a ` with ` b ` to avoid all letters "a" being replaced. – cwahls Dec 24 '15 at 04:51
  • Another possibility is using [`atom`](http://atom.io/). – 0 _ Nov 07 '17 at 21:38
  • 3
    Find and replace is Ctrl-R in my version of Spyder (3.2.8) – Josiah Yoder Jun 05 '18 at 17:53
  • Recently (May 2020), refactoring seems to be implementable via [python-language-server](https://github.com/palantir/python-language-server) according to [Issue 415](https://github.com/spyder-ide/spyder/issues/415), but might have some issues still – Sterling Jan 08 '21 at 21:59

3 Answers3

39
1) Select the variable you want to change, then Press CTRL + R
2) Enter the new variable name you want. (in replace by)
3) Replace all

enter image description here

Ludo Schmidt
  • 1,283
  • 11
  • 16
  • 12
    Still not a full Refactoring option but I love your Paint Skills! – MichaelA Jun 23 '20 at 07:51
  • 1
    problematic if you want to change a short name variable like 'i'. The 'i' letter is everywhere in the code, although when the variable i is selected innspyder, the correct i variables are highlighted throughout the code – newman_ash Mar 29 '22 at 15:54
16

No way to do it currently. It's an open issue. https://github.com/spyder-ide/spyder/issues/415

Mudit Jain
  • 4,163
  • 1
  • 22
  • 18
1

Alternatively, PyCharm can be used instead of Spyder, which is now supported in Anaconda.

In PyCharm: Select the variable > Right Click > Refactor > Rename

Stef Geysels
  • 1,023
  • 11
  • 27
MI Alam
  • 417
  • 3
  • 9
  • When you say supported in Anaconda, does this mean it can have it's own "block" module (similar to Jupyter, Spyder, etc.) in the Windows Desktop version? Or does it need to be installed separately? – Sterling Jan 08 '21 at 21:57
  • Anaconda is a giant package available for Windows, Mac, & Linux that needs to be installed. Spyder and Jupyter are already included in Anaconda. – MI Alam Jan 11 '21 at 04:39
  • Doesn't answer my question. I was asking about PyCharm – Sterling Jan 11 '21 at 08:33
  • 1
    "*Alternatively, PyCharm can be used instead of Spyder*" that should be a comment, as it doesn't answer the question. If the OP had asked for alternatives to Spyder, then it would be appropriate (but the question would have been closed). While being a bettter product, PyCharm is not exactly the same approach, and not exactly "free", as contrary to Spyder to use PyCharm, the user must be registered. – mins May 01 '21 at 12:57