Daniel Kaplan
Daniel Kaplan

Reputation: 67370

IdeaVIM: How do I conveniently rename a variable I extract?

In IdeaVIM, if I extract a variable and want to name it, it seems like I'm in visual mode (the name of the variable is highlighted), yet I can't figure out a way to change what's highlighted. Here's an image:

enter image description here

If I type c, it doesn't do anything. If I type cc, it changes the whole line. Basically, it acts like I'm in command mode yet the name is highlighted as if it's in visual mode. What I want to do 99% of the time is change this word. Yet, the cursor is annoyingly one character after the word so ciw doesn't change it.

What's a convenient way to rename this variable? I've normally done hciw. I'd prefer that "extract variable" put me directly into insert mode, though.

Upvotes: 8

Views: 5807

Answers (2)

Luke
Luke

Reputation: 1329

For "Extract Variable" once you've got to the point where it's suggesting names: name suggestions

What always works is pressing Escape, b, cw and then typing the new name. Sometimes you can press b right away, but often that puts me into visual mode for some reason, so pressing Escape is guaranteed.

Upvotes: 3

jbyler
jbyler

Reputation: 7847

It's a little counterintuitive, but the easiest thing to do is just immediately hit i to enter insert mode, then start typing the new name, and it will replace the highlighted text.

The problem, as I see it, is that vim is modal and vanilla IntelliJ is also modal, and what you have here is the interaction of two sets of modes. To understand the IntelliJ modes, try turning off IdeaVIM with ^Z and use the extract variable refactoring. Notice how if you start typing right away, what you type replaces the selected text, but if you move the cursor first and then start typing, what you type is inserted at the cursor. Now add IdeaVIM to the mix: once you are in insert mode, IntelliJ's behavior kicks in.

See VIM-274 for a request to change this behavior and some discussion of alternatives.

Upvotes: 6

Related Questions