Extract Variables

Extract refactorings are performed for the various expressions and blocks of code, including strings. Currently Extract refactoring on substrings is not supported.
Refactoring is limited to complete expressions and blocks of code.

The current release will extract all occurenes (within the current scope) of the selected expression and replace with a variable.

How to invoke the Extract Variable command?

There are a two options to invoke the extract variable command, they are as follows:

Option 1 (Command Palette)

  • Select the text (expression) to be extracted into a variable
  • Launch the Command Palette via the shortcut (⇧⌘P or Ctrl+Shift+P)
  • Select the ‘Extract Variable’ command
  • The selected expression(s) will be extracted into a variable with a random name prefixed ‘newvariable’
  • Next provide a new name for this variable

Sample

Option 2 (Quick Fix)

  • Select the text (expression) to be extracted into a variable
  • Launch the Quick Fix command from the Command Palette (or use the short cut ⌘. or cmd+.)
  • Select the ‘Extract Variable’ command from the menu
  • The selected expression(s) will be extracted into a variable with a random name prefixed ‘newvariable’
  • Next provide a new name for this variable

Launching the Quick Fix command from the Command Palette
Sample using Quick Fix Command

Launching the Quick Fix command using the keyboard shortcut
Sample using Quick Fix Short Cut

Option 3 (Keyboard Shortcut)

  • Setup a custom keyboard short cut for the command ‘python.refactorExtractVariable’
  • Select the text (expression) to be extracted into a variable in the editor
  • Invoke the above command using the designated keyboard shortcut
Topics:

Extract Variables

Extract refactorings are performed for the various expressions and blocks of code, including strings. Currently Extract refactoring on substrings is not supported.
Refactoring is limited to complete expressions and blocks of code.

The current release will extract all occurenes (within the current scope) of the selected expression and replace with a variable.

How to invoke the Extract Variable command?

There are a two options to invoke the extract variable command, they are as follows:

Option 1 (Command Palette)

  • Select the text (expression) to be extracted into a variable
  • Launch the Command Palette via the shortcut (⇧⌘P or Ctrl+Shift+P)
  • Select the ‘Extract Variable’ command
  • The selected expression(s) will be extracted into a variable with a random name prefixed ‘newvariable’
  • Next provide a new name for this variable

Sample

Option 2 (Quick Fix)

  • Select the text (expression) to be extracted into a variable
  • Launch the Quick Fix command from the Command Palette (or use the short cut ⌘. or cmd+.)
  • Select the ‘Extract Variable’ command from the menu
  • The selected expression(s) will be extracted into a variable with a random name prefixed ‘newvariable’
  • Next provide a new name for this variable

Launching the Quick Fix command from the Command Palette
Sample using Quick Fix Command

Launching the Quick Fix command using the keyboard shortcut
Sample using Quick Fix Short Cut

Option 3 (Keyboard Shortcut)

  • Setup a custom keyboard short cut for the command ‘python.refactorExtractVariable’
  • Select the text (expression) to be extracted into a variable in the editor
  • Invoke the above command using the designated keyboard shortcut