ChatGPT解决这个技术问题 Extra ChatGPT

How can I add a string to the end of each line in Vim?

I want to add * to the end of each line in Vim.

I tried the code unsuccessfully

:%s/\n/*\n/g
Yours doesn't work because on the right hand side of :s///, \n corresponds to a null character. The section :help sub-replace-special is relevant.

N
Nathan Fellman

Even shorter than the :search command:

:%norm A*

This is what it means:

 %       = for every line
 norm    = type the following commands
 A*      = append '*' to the end of current line

Man, there exists no vi or vim thread from which I do not learn something. Today I learn about norm. And as they say at Cheers, "Norm!"
For prepending, you'd just use 0i*, 0 takes you to the beginning of the line, i for insert mode, and * for the *. D:
As a VimGolfer I can't help but add that using I* rather than 0i* will save you one keystroke.
One other tip: This works for ranges as well. Just highlight the lines in visual mode and press :, this will pre-fill the command with :'<,'> which you can then make into :'<,'>norm A*
For prepending, use I instead - :%norm I*
S
Sabito 錆兎 stands with Ukraine
:%s/$/\*/g

should work and so should :%s/$/*/g.


Is there a way to insert '' at the same column, since all lines are not of same length, so line 1 might have '' at 15th column, but line 2 has '*' at 25th column.
there is only 1 EOL per line, so the g flag is unnecessary.
I wanted to add / to the start of each line :1,$s/^/\/g worked for me.
G
Glory to Russia

I think using visual block mode is a better and more versatile method for dealing with this type of thing. Here's an example:

This is the First line.  
This is the second.  
The third.

To insert " Hello world." (space + clipboard) at the end of each of these lines:

On a character in the first line, press Ctrl-V (or Ctrl-Q if Ctrl-V is paste).

Press jj to extend the visual block over three lines.

Press $ to extend the visual block to the end of each line. Press A then space then type Hello world. + then Esc.

The result is:

This is the First line. Hello world.  
This is the second. Hello world.  
The third. Hello world.  

(example from Vim.Wikia.com)


Instead of pressing jj one can hit G to move to the last line. This is useful in large files, where pressing j until you hit the last line isn't practical.
Agree that visual block is cleanest here, more extensible too
Note that this doesn't seem like it's working on all lines at first, but after you press Esc it goes through and adds it to all.
Visual Block causes issues and errors when using vim as an extension for other things, like VS Code. In those cases, the search and replace the original user is asking for is the reliable option.
make sure you use Capital A, lowercase "a" doesn't work.
B
Brian Carper

Also:

:g/$/norm A*

Also:

gg<Ctrl-v>G$A*<Esc>

Ahhh... The second one can be used with a selection of lines too. Nice!
Could you please explain the first example?
@CodyPoll g is global command that does something with every matching line. $ matches every line. It could be also ^ or .*. norm executes commands that you normally use in command mode. So A* means append * to the end of line.
u
utkarsh

If u want to add Hello world at the end of each line:

:%s/$/HelloWorld/

If you want to do this for specific number of line say, from 20 to 30 use:

:20,30s/$/HelloWorld/

If u want to do this at start of each line then use:

:20,30s/^/HelloWorld/

p
paxdiablo

One option is:

:g/$/s//*

This will find every line end anchor and substitute it with *. I say "substitute" but, in actual fact, it's more of an append since the anchor is a special thing rather than a regular character. For more information, see Power of g - Examples.


What does s// mean in the regex? I know a similar command, :g/ / /p, (grep). I am pretty sure that your command is close to mine, at least in the structure.
The s is substitute - it replace the end of line anchor with the asterisk (well, not actually replaces it since it's an anchor point).
g
glts

You don't really need the g at the end. So it becomes:

:%s/$/*

Or if you just want the * at the end of, say lines 14-18:

:14,18s/$/*

or

:14,18norm A*

U
User 1058612

...and to prepend (add the beginning of) each line with *,

%s/^/*/g

This doesn't answer the question.
佚名
:%s/\n/*\r/g

Your first one is correct anywhere else, but Vim has to have different newline handling for some reason.


In my version of Vim \n works, but this just replaces the newline with a *, effectively joining the lines.
This command is verified to work fine on every version of Vim I've tried. Are you using the windows version or something?
C
Conner
%s/\s*$/\*/g

this will do the trick, and ensure leading spaces are ignored.