To replace the last eight characters of a line with spaces and without highlighting the change in Vi editor, you can use the following steps.

Vi/Vim substitute command
Move the cursor to the beginning of the line where you want to make the replacement. Enter command-line mode by pressing : in normal mode.
Type the following command and press Enter:
%s/.\(.\{8\}\)$//
This command uses a regular expression to match and replace the last eight characters of the line with nothing (i.e., effectively replacing them with spaces). Here’s what the command does:
This command breaks down as follows:
%s: Substitute command to perform the search and replace operation on all lines.
.: Matches any character.
\(.\{8\}\): Captures the last 8 characters of each line in a group.
$: Anchors the search to the end of the line.
//: Replaces the matched pattern with an empty string, effectively removing the captured characters.
You can use this command on single and multiple lines. Make sure to execute this command in vi’s command mode by pressing Esc to exit insert mode and then typing : followed by the command.
After making the replacement, you can save your changes by typing :w and pressing Enter or save and quit by typing :wq and pressing Enter.
References
Related







You must be logged in to post a comment.