Vim – how to add text at the end of each line
Vim – how to add text at the end of each line #
Given the following code:
'item1
'item2 with another name
'item3 something
The goal is to append ', at the end of each line so it looks like:
'item1',
'item2 with another name',
'item3 something',
There are several ways to append text at the end of each line:
using visual block mode #
gg0<C-V>2j$A',<Esc>
- go to the beginning of the first line:
gg0 - enter visual block mode:
<C-V>(ctrl + V) - check for -- VISUAL BLOCK --
- move downwards 2 lines to select all lines:
2j - go to the end of the last line:
$ - now enter
Ato append text - the cursor appears at the end of the first line
- type in the desired characters ',
- press the
<Esc>key to confirm
You can use any kind of selection to select the lines (for example: vip - visual select a paragraph).
using the substitute command #
Option A) Apply it to every line in the whole file:
:%
Option B) Apply it only to a subset of lines, select the desired lines by pressing v and moving up/down k/j:
:'<,'>
Append the regex
s/$/',/
- search
s/ - for the line ending
$ - and replace with
/ - some characters
', - finish regex
/
using a macro #
gg
qq
$a',<Esc>jq
2@q
- go to the first line:
gg - type
qqto start recording atq(check for recording @q) - go to the end of the current line:
$ - press
ato enter text afterwards - type in the desired characters ',, leave insert mode by hitting
<Esc> - press
jto enter the next line - now press
qto stop recording - go to the second line
:2and execute the recorded macro twice by typing2@q