42

Maybe I am being daft but can you replace all the characters from where the cursor is to the end of line by one command? Then use . to do the same replace on the next line and so on.

7 Answers 7

59

If I understood your question properly, try this:

C (that's a capital C) will delete everything from the cursor to the end of the line and put you in INSERT mode, then you write your replacement, leave INSERT mode, use . to repeat the process somewhere else.

3
  • Fine. :) You're welcome.
    – sr_
    Feb 17, 2012 at 9:20
  • 16
    if anyone was wondering, it is a capital C :) Oct 2, 2015 at 15:09
  • 1
    Would be good to update the answer to specify that this is a capital C.
    – fraxture
    Aug 24, 2017 at 17:02
14

Adding to sr_'s answer:

If your cursor starts at the same position on each line, you might be interested in the Visual Block feature.

Type the following in order, with your cursor in the initial position:

  • Ctrl+v: Enter visual block mode.
  • $: go to the end of the line.
  • [X]j: replace X with the number of lines you want to go down.

This should create a rectangular selection going from your cursor on the first line to the end of the line [X] lines below.

You can then replace your whole selection:

  • c: delete selction and go in insert mode.
  • insert new text
  • Esc: Exit insert mode; Vim will automatically repeat the step on each line selected.

I use visual block whenever I can, I thought I'd share

2
  • +1 nice answer indeed. But not really what I want since sometimes my code is not nicely formatted like that ^_~ Feb 17, 2012 at 10:22
  • @sardathrion; RE comment, 'nicely formatted'. Creating tables in Markdown I was able to auto fill spaces in n+1 lines, after creating line n by Visual Block selecting backwards! In other words the shape was similar to backwards 7 or vertical flipped L, or 'not nicely formatted'. lol.
    – xtian
    Oct 1, 2014 at 15:18
4

C or c$ is for "change" and R is for "replace".

Two almost similar behavior. The former would get the line deleted before you start typing; while the latter would let you type over the characters and possibly go beyond the limit of the old line's characters displayed.

2

To change every character on a line with a single character from the current column until the end of the line while keeping the line length unchanged:

:.s/\%>.c./-/g where - is the new character


.s/./-/g would replace every character on the current line with -

\%>.c matches from the current column

1
  • This is the only true answer. Even the visual method does not work from the middle of the line. Mar 12 at 6:30
1

Just press 'C' i.e. Capital C in the command mode, and type the replacement text.

0

The other answers missed these, they don't enter insert mode.

  • D Delete to the end of the line
  • d$ Deletes from the cursor to the end of the line

Then of course use .

2
  • -1 for not entering insert mode since that is specifically what I asked for. +1 for completeness sake. Feb 17, 2012 at 11:10
  • 1
    @Sardathrion-againstSEabuse, you specifically did not ask for insert mode. You simply said "replace all characters." That is ambiguous at best. You could want to replace with p for example, or replace with <C-v>$rX, or anything else under the sun. Feb 17, 2020 at 11:33
0

As it can be useful, once we are trying to change till the end of line. I have created an insert mapping to perform this task, it not uses D because in insert mode this key is used to decreasing indentation. So it is Ctrl-k

:inoremap <C-k> <C-o>C

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .