I normally put my code from VIM with syntax highlighting and line numbers turned on on my blog. This is good for readability, but bad if you want to copy paste it from the web site. With vim it’s easy to remove these line numbers from pasted code. Here is how to do that.
- Select the code on the website (no CTRL-C/CTRL-V is needed on Linux, selecting does already the copy)
- Start up vim and enter :set paste. This turns off the automatic indentation, to allow keeping the indentation of the pasted code.
- Press “i” to change to insert mode.
- Now click with the middle mouse button into the vim window. That inserts the code.
- Press “Esc” to change back to command mode.
- Now use vim’s regexp based substitute and replace command to remove the line numbers: :%s/^[0-9 ]\d \=//
- Explanation: [0-9 ] (Note the space in brackets). This stands for a digit or a beginning space, because the numbers are right aligned. \d stands for a second digit. More than 100 lines I never have in my scripts 😉
nice tip! I wished I knew about “:set paste” earlier 😉