Question

I know about displaying line numbers in Vim using :set nu but what I really want is to make those line numbers the actual contents of the file , preferably only some part of the file. fancy and works outside vim cat -n fileName >> fileName.numbers . Any suggestions to make it work inside Vim?

Was it helpful?

Solution

You can pass the file through a program that numbers the lines, for example cat:

:%!cat -n

To number only some lines, first select the lines in visual mode (command V) and then type :. The prompt changes into :'<,'> and you can type the rest of command !cat -n. The full command is:

:'<,'>!cat -n

OTHER TIPS

You might also want to check out the increment package here. If you have a block of text,

test
test
test
test
test
test
test
test
test
test
test

You can select the 't' on the first line, and enter visual block mode using Ctrl-V. Select until the last line, then enter insert mode with I. Enter the number '1' plus a space, and hit escape to leave visual block mode. You'll then have a 1 before each line:

1 test
1 test
1 test
1 test
1 test
1 test
1 test
1 test
1 test
1 test
1 test

Next, highlight all of the 1's in visual block mode again, and type :Inc<CR>. This will increment the numbers, essentially numbering your lines in text:

 1 test
 2 test
 3 test
 4 test
 5 test
 6 test
 7 test
 8 test
 9 test
10 test
11 test
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top