Aug
11
2008

ReSharper Tip of the Day: Introduce Variable

I recently wrote a post about how I wish I could have a context action to replace a string literal with a new constant. To my surprise, someone from JetBrains posted a comment saying it would already do it.

It isn’t as discoverable as other context actions, but it does work. With your cursor in the string you want to replace, you can press Ctrl+W to extend the selection to the entire string. This is important. Without selecting the string, it will try to introduce a variable for the entire statement. With the string selected, press Ctrl+R,V to introduce a variable. It will notice it can replace all the occurrences of the selected string.

Select that you want to replace all the occurrences, and press Enter. There is the constant that I wanted to introduce.

It gives you some suggested variable names too.

Additionally, you can use Ctrl+R,F to introduce a field if you want the replacement at a higher level.

Thanks JetBrains! That would be a cooler context action though.

Written by mark in: ReSharper Tip of the Day | Tags:

3 Comments »

  • There’s also a cool Control+Shift+R shortcut which displays all refactorings available in the current context.

    Comment | August 11, 2008
  • Thanks! I’ll have to make that an upcoming tip of the day. I use the Alt+` to bring up a similar navigation window.

    Comment | August 12, 2008
  • [...] Comments Pages tagged “literal” on ReSharper Wish List: Replace Literal With…mark on ReSharper Tip of the Day: Introduce VariableOleg Stepanov on ReSharper Tip of the Day: Introduce VariableGymnastics Is Like NASCAR | Premier [...]

    Pingback | September 3, 2008

RSS feed for comments on this post. TrackBack URL

Leave a comment

Powered by WordPress | Kredit | TheBuckmaker