How to remove text from all rows between two columns?

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

For example, I want:

abcdefg hijklmn opqrstu 

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

 abfg himn optu 
+6
source share
5 answers

Place the cursor in d, then press Ctrl-V , l , G and d .

  • Ctrl-V enters visual block mode;
  • l extends the visual selection by one character to the right;
  • G extends the selection to the last line;
  • d deletes the selection.
+15
source

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

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

 %s/\%3v.*\%<7v// 
+4
source

Your question is very similar to this one .

To remove columns 3 through 5 for all lines in a file :

 :%normal 3|d6| 

To remove a specific line interval (from 80 to 90), use the following:

 :80,90normal 3|d6| 

If you are not familiar with a normal team and with | "movement" comes with a quick explanation:

  • The normal command executes the following commands in normal mode;
  • The | "motion" moves the cursor to the specified column, so 3| moves the cursor to the third column;
  • Then I delete all ( d ) to the 5th column ( 6| ).
+4
source

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 column index (based on zero), and the second number (3) is the number of characters the column has.

Explanation:

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

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

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

Omit the replacement string as you want to remove the match.

NTN

+3
source

I would do the following:

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

which will delete everything else except the first two columns at the beginning, and two at the end.

0
source

Source: https://habr.com/ru/post/916019/


All Articles