Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add text at the end of each line in Vim?

Tags:

vim

People also ask

How do you add a backslash at the end of a line in Vim?

How do I insert a space + backslash at the end of the all lines in my Vim editor? Go to the end of the line, add space + backslash, go to the next line, add space + backslash, ... repeat.


This will do it to every line in the file:

:%s/$/,/

If you want to do a subset of lines instead of the whole file, you can specify them in place of the %.

One way is to do a visual select and then type the :. It will fill in :'<,'> for you, then you type the rest of it (Notice you only need to add s/$/,/)

:'<,'>s/$/,/

There is in fact a way to do this using Visual block mode. Simply pressing $A in Visual block mode appends to the end of all lines in the selection. The appended text will appear on all lines as soon as you press Esc.

So this is a possible solution:

vip<C-V>$A,<Esc>

That is, in Normal mode, Visual select a paragraph vip, switch to Visual block mode CTRLV, append to all lines $A a comma ,, then press Esc to confirm.

The documentation is at :h v_b_A. There is even an illustration of how it works in the examples section: :h v_b_A_example.


Another solution, using another great feature:

:'<,'>norm A,

See :help :normal.


ex mode is easiest:

:%s/$/,

: - enter command mode
% - for every line
s/ - substitute
$ - the end of the line
/ - and change it to
, - a comma