Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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.

share|improve this question

5 Answers 5

up vote 19 down vote accepted

If I understood your question properly, try this:

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.

share|improve this answer
    
+1 You did indeed understand perfectly. Thank you. –  Sardathrion Feb 17 '12 at 9:19
    
Fine. :) You're welcome. –  sr_ Feb 17 '12 at 9:20

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

share|improve this answer

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.

share|improve this answer

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 .

share|improve this answer
    
-1 for not entering insert mode since that is specifically what I asked for. +1 for completeness sake. –  Sardathrion Feb 17 '12 at 11:10

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

share|improve this answer
    
+1 nice answer indeed. But not really what I want since sometimes my code is not nicely formatted like that ^_~ –  Sardathrion Feb 17 '12 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 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.