Below is the command to delete first N
characters in vi:
:%s/^.\{N}//g
However, I don't understand the meaning of ^.\\{N}
; can anyone explain the meaning of each character by character ?
How to extend this command to delete in-between characters from lines ?
eg deleting 4th to 50th characters from given range of lines
You can match specific columns with \\%c
or \\%v
Delete 4th to 50th column:
:%s/\%4c.\{-}\(\ze\%50c\|$\)//
To only delete IFF there is a 50th column:
:%s/\%4c.\{-}\ze\%50c//
To use virtual columns (eg handy if you use tab stops):
:%s/\%4v.\{-}\ze\%50v//
Oh,
.\\{-}
is a non-greedy match of zero or more characters. \\ze
is the end-of-match directive, this stops the match at column 50 Here is the regex part explanation
^
is the start-of-the-line anchor.
.
matched any character.
N
indicates the number of characters the previous token will be matched
{}
is used to group the count. For example you can use it as {1, 9}
So it says, From the beginning of the line, match N characters
and the rest is VIM stuff.
^
is the start-of-line anchor; .
is the universal match, {N}
repeats the previous match N times.
To delete the 4th to 50th character, use :%s/^\\(.\\{4}\\).\\{46}/\\1/g
.
However, it's probably easier to use rectangular selection mode: from normal mode, move to the start location, then hit Ctrl+V
and you can create a rectangular selection by moving the cursor.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.