<p>How can I add line numbers to a range of lines in a file opened in Vim? Not as in <code>:set nu</code>—this just <em>displays</em> line numbers—but actually have them be prepended to each line in the file?</p>

```
cat -n
```
adds line numbers to its input. You can pipe the current file to
```
cat -n
```
and replace the current buffer with what it prints to stdout. Fortunately this convoluted solution is less than 10 characters in vim:
```
```
:%!cat -n
```
```
Or, if you want just a subselection, visually select the area, and type this:
```
```
:!cat -n
```
```
That will automatically put the visual selection markers in, and will look like this after you've typed it:
```
```
:'<,'>!cat -n
```
```
In order to erase the line numbers, I recommend using
```
control-v
```
, which will allow you to visually select a rectangle, you can then delete that rectangle with
```
x
```
.