There are times when you need to manipulate BIG text files. Today going through syslog, i had to deleted first few characters ( date and time) to get a better understanding of my log files. Vim is so cool this can be done in fraction of second with just these few characters.
:%s/^.\{n}/
Substitute n with number of characters you want to delete.
:s/<search for>/<replace with>
will replace first occurrence of “search for” with “replace with”, so if you leave “replace with” blank it will basically delete first occurrence of “search for”
Dissecting Command
:s is a vim command for substitution.
% stands for each line.
^ – From first
. – regular expression, matches first character
Now what will this command do?
:%s/^./
For each line starting from first, this command will substitute first character with nothing(hence deletes first character)
Now you want this to be repeated for n characters
\{#\}
is used for repetition. So,
:%s/^.\{n\}/
will do the magic.
You need not escape closing bracket ‘}’ so this also works
:%s/^.\{n}/
removing % will delete first n characters in current line
Source : Vim Search and Replace
One thought on “Delete first n characters in a file : Vim Trick”