VIM paste text with line numbers

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.

  1. Select the code on the website (no CTRL-C/CTRL-V is needed on Linux, selecting does already the copy)
  2. Start up vim and enter :set paste. This turns off the automatic indentation, to allow keeping the indentation of the pasted code.
  3. Press “i” to change to insert mode.
  4. Now click with the middle mouse button into the vim window. That inserts the code.
  5. Press “Esc” to change back to command mode.
  6. Now use vim’s regexp based substitute and replace command to remove the line numbers: :%s/^[0-9 ]\d \=//
  7. 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 😉

1 Response to “VIM paste text with line numbers”

  1. 1 tompson June 1, 2009 at 12:24 pm

    nice tip! I wished I knew about “:set paste” earlier 😉

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: