开发者

how to add an extra character after exactly two character on each line in vim

开发者 https://www.devze.com 2023-04-11 00:44 出处:网络
I have the text in the file like this: in IDMAN ip f开发者_如何转开发rghj I want the text to be like this:

I have the text in the file like this:

in IDMAN
ip f开发者_如何转开发rghj

I want the text to be like this:

in *IDMAN
ip *frghj


You can do:

:%s/\%3c/*

Explanation:

  • :s is the substitute command, % is the range for all lines in the file
  • / is the pattern delimiter
  • \%3c is a pattern that matches nothing at third character in the line
  • * is the substitution expression


Hover your cursor over the capital I in IDMAN in normal mode.

how to add an extra character after exactly two character on each line in vim

Enter visual block selection with CTRL-V and go down a line with j.

how to add an extra character after exactly two character on each line in vim

Enter insert mode with SHIFT-I and then enter your desired character.

how to add an extra character after exactly two character on each line in vim

Go back to normal mode with ESC and the character will appear in the same column for the rest of the lines.

how to add an extra character after exactly two character on each line in vim


For the example file:

in IDMAN
ip frghj

and the following output (a star on each line):

in *IDMAN
ip *frghj

The sequence of commands is the following (cursor has to be on the character where the addition should happen):

CTRL-vjI*ESC

That means:

  • CTRL-v: Start visual block mode
  • j: Mark the second line too
  • I: Go into input mode for the block
  • *: Insert the character(s)
  • ESC: Close the visual input mode, so to all marked lines, the characters will be added.


How to add an * character after exactly three character on each line in vim:

:%s/\(...\)/\1*/

see :help :s, :help range, :help s/\\1

or, more shortly (great thanks to @Benoit for :-) )

:%s/.../&*/

see help s/\& (thank @Benoit for being pointed to this)

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号