read Read
read Read

Reputation: 6063

How to remove text from all lines between two columns?

I would like to remove the content for all lines between two columns. How do I do this?

For example, I want this:

abcdefg
hijklmn
opqrstu

To become this if I remove text between columns 3 through 5:

abfg
himn
optu

Upvotes: 8

Views: 12710

Answers (5)

kenorb
kenorb

Reputation: 166419

I would do in the following way:

:%s/^..\zs.*\ze..$//g

which will remove everything else apart first two columns at the start, and two at the end.

Upvotes: 0

Magnun Leno
Magnun Leno

Reputation: 2738

Your question is very similar to this one.

To delete the columns 3 through 5 for all lines in the file:

:%normal 3|d6|

In order to delete an specific line interval (80 to 90), use this:

:80,90normal 3|d6|

If you're not familiar with the normal command nor with the | "motion" there goes a quick explanation:

  1. The normal command execute the following commands in the normal mode;
  2. The | "motion" moves the cursor to a specified column, so 3| moves the cursor to the 3rd column;
  3. Then i deletes everything (d) until the 5th column (6|).

Upvotes: 8

Tassos
Tassos

Reputation: 3288

You can use search and replace:

:%s/..\zs...\ze

or in a more general form:

:%s/.\{2}\zs.\{3}\ze

where the first number (2) is the index of the column (zero based) and the second number (3) is the count of characters the column has.

Explanation:

:%s/ search and replace in the whole buffer.

.\{2}\zs find two characters and set the beginning of match here.

.\{3}\ze find three characters and set the end of match here.

Omit the replacement string since you want to delete the match.

HTH

Upvotes: 3

Ingo Karkat
Ingo Karkat

Reputation: 172570

For spontaneous editing, I would use blockwise visual mode via CTRL-V (often mapped to CTRL-Q on Windows), then d to delete it.

If you do this often, for a large range / the entire buffer, or repeatedly, I would use a substitution that starts matching in a virtual column, and extends (up) to the end column, like for your example:

%s/\%3v.*\%<7v//

Upvotes: 4

gonsalu
gonsalu

Reputation: 3184

Position your cursor in d, then press Ctrl-V, l, G and d.

  • Ctrl-v enters visual block mode;
  • l expands the visual selection one character to the right;
  • G expands the selection to the last line;
  • d deletes the selection.

Upvotes: 18

Related Questions