" line_numbers.vim " Utility to (un)renumber the lines of a program (or any text). " Copyright (C) 2010,2011,2015 Marcos Cruz (programandala.net) " Change log: " 2010-08-05: First version. " 2010-08-08: Unrenum() removed from Renum(). " 2010-08-11: RemoveSpaces() added, to remove the spaces nl creates at blank lines. " 2010-08-12: Renum() saves the file. " 2010-08-13: Renum() and Unrenum() go back to the current line. " 2011-04-24: Renum() improved with a parameter: the first line number. Additional shortcut key created to call Renum(30000). " 2011-05-14: Additional parameter in Renum(), the line step. " 2015-02-13: Improved; partly rewritten; no warning messages. function! RemoveSpaces() " Remove all spaces from lines that only contain spaces. silent! %s/^\s\+$// endfunction function! Unrenum() " Remove line numbers; and also spaces from blank lines. " The "'" register is changed. mark ' silent! %s/^ *[0-9]\+ // call RemoveSpaces() normal '' endfunction function! Renum(first_line,step) call Unrenum() " Version with the awk program: " execute ":%!awk '{print NR,$0}'" " Reference: " http://vim.wikia.com/wiki/VimTip28 " Example of an improved version with the awk program: " awk 'BEGIN {n=100};{sub(/xxx/,n-=3);print}' " Reference: " http://vim.wikia.com/wiki/Numbering_lines_and_interpolating_sequences " Version with the nl program (coreutils 6.9.92.4-f088d-dirty): silent execute ":%!nl --body-numbering=t --number-format=rn --number-width=5 --number-separator=' ' -v".a:first_line." -i".a:step " Most of the options are not needed, because their values are the default ones, " but I prefer to write them in order to make it easy to modify them. " In that version of coreutils, the long option for -v doesn't work (though the manual mentions it). " -v sets the first line number, and -i sets the line increment. " More modern versions of nl uses the clearer options --first-line and --line-increment, see: " http://www.gnu.org/software/coreutils/manual/coreutils.html#nl-invocation call RemoveSpaces() endfunction " Mappings for normal mode nmap ,lr :call Renum(10,10) nmap ,lu :call Unrenum() " vim: ts=2:sts=2:et