views:

76

answers:

5

Hi,

Having the code:

[Test]
public void ShouldDoSomethingMeaningFull() {
    Assert.Fail();
}

I often need to overwrite the line (3) Assert.Fail();. What I currently do is this:

  1. Go to that line 3G.
  2. Select everything starting from first non-whitespace ^v$.
  3. Change it - c.

The whole sequence is: 3G^v$c.

While this works for me but it is not efficient way of doing it because my fingers jump to the ^ and $.

Any better way?

Thanks,
Dmitriy.

+2  A: 

For me, it's quicker to just do:

3GddO

Which goes to the line 3 3G, deletes that line dd, and inserts a line above the current line (the closing brace now) O

John Weldon
Using `O` is potentially buggy on some terminals (https://bugs.kde.org/show_bug.cgi?id=169396). I think, a better option should be considered.
Pavel Shved
@Pavel; interesting KDE bug, that seems like an issue for KDE to fix not VIM..
John Weldon
@Pavel but I liked your answer and upvoted it :)
John Weldon
@John, that's why I filed it to KDE bugzilla. ;-)
Pavel Shved
+3  A: 

The obvious improvement would be 3GC (C means change to end of line, i.e. the same as 3Gc$)

If you prefer to change the entire line as pointed out by Pavel Shved, there's a not so obvious shortcut for that as well. 3GS is the same as 3Gcc.

The "Making small changes" section of the help file has more details.

Brian Rasmussen
Nice. I'll try to use that myself.
John Weldon
Nice one, provided that `G` always jumps to the first non-whitespace position in the line (which seems like it does).
Dmytrii Nagirniak
+4  A: 

The correct key combo would be 3Gcc.

cc will replace the whole line (contrary to C, which only replaces starting from the current character), and will preserve whitespaces at the beginning of the line.

Pavel Shved
Nice one. I gues the `cc` and `C` (suggested by Brian Rasmussen) are the same.
Dmytrii Nagirniak
@Dmitriy, it is the same, unless `startofline` is set, which makes `G` keep the column. I prefer setting it, personally.
Pavel Shved
`C` will replace from current character to the end. `cc` will replace the entire line, regardless of cursor location.
Amir Rachum
Also `C` will leave cursor at current position, while `cc` puts it to the first column (as the line is no blank).
Dmytrii Nagirniak
`cc` and `S` only preserve indentation when `autoindent` is on, which if you're editing code, it probably is.
Nefrubyr
+2  A: 

I like to use S, which deletes the line and places you in insert mode, preserving indenting. While C works as well, S works from any position in the line.

box9
Unfortunately this won't work as `S` puts the cursor at the beginning of the line (first column) and will not set it to the first non-whitespace character.
Dmytrii Nagirniak
@Dmitriy Nagirnyak - it should work for the syntax you posted above. Try turning on c indenting: :set cin
box9
A: 

3GC - makes most sense to me. Easier than having to into visual mode.

Kevin McKelvin