showkey
showkey

Reputation: 338

set line number to be part of line content in vim

To use command :set number in vim can add numbers at the beginning of the every line. But the line number is not part of the line contents ,when you copy the contents ,the line number will not be in it ,how can i create the line number ,and make it be the part of the line content ,you can copy and paste the number with every line?

Upvotes: 0

Views: 137

Answers (1)

Kent
Kent

Reputation: 195269

I believe that you are looking for this line:

:%s/^/\=line('.').' '/

this will add line number to each line of text. note:

  • the original text would be changed.
  • the line number won't be automatically fixed if you add new line or remove lines, they are parts of your text (line content), as you wished.

FYI

if you are on linux box, you may consider to use other tool(s) to get line-numbered text output, without changing your original text, like:

nl file
cat -n file
awk '$0=NR" "$0' file
.....

Upvotes: 6

Related Questions