1

it is common knowledge the :set number will display the line numbers in vim. However, I have come an issue with this. When I use :set linebreak and reach a new line a number is not displayed with that newline. As I understand it these are referred to as display lines.

tl;dr How do I add line number to display lines?

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
user419627
  • 13
  • 3
  • Is this because you have wordwrap turned on? Try using `:set nowrap` to see if that helps. – Lee Netherton Nov 14 '12 at 10:31
  • see [this](http://stackoverflow.com/questions/319925/difference-between-hard-wrap-and-soft-wrap) to see if that helps. – none Nov 14 '12 at 10:54
  • 1
    A concrete use case for this request: I've become a big fan of using a ruler with relative line numbers to aid in navigation, so when I see that the line I want to edit has a '5' beside it I can type `5j` to get there quickly. With word wrapping though, this doesn't work because I jump down 5 **display** **lines** rather than to the actual line that has the '5' beside it. So I type `5j`, see that I'm actually only half-way there and now my target line has a '3' beside it... `3j`, nope, not there yet, try again. Turning off line wrapping isn't an option because I need to see all the text. – Michael Iles Oct 30 '13 at 14:49

3 Answers3

8

This is not a feature of vim. If you want wrapped/broken lines to actually be new lines, why don't you actually make them new lines?

  • gqq reformats the current paragraph using the textwidth settting

To get automatic formatting going:

  • set fo=tcrwa textwidth=80

Now, whenever you're type your text will wrap around. On auto-wrap, a trailing space is left on the previous line indicating it isn't the end of a paragraph yet.

See vimdoc for 'fo-table'

sehe
  • 374,641
  • 47
  • 450
  • 633
  • 2
    Suppose you want to edit in vim, then paste into Google Docs or MS Word... You don't want actual new lines, but having the line numbers for displayed lines would still be nice, that way you can wrap the lines by word, then when you copy and paste into Docs or Word it just works. – trusktr Oct 12 '13 at 19:48
  • I'm completely at a loss why "having the line numbers for displayed lines would still be nice". You're pasting into Word anyways. Also, see [ViEmu for Word and Outlook](http://www.viemu.com/viemu-vi-vim-word-outlook.html) – sehe Oct 12 '13 at 20:23
2

There's no command :linebreak. Do you mean :set linebreak?

The line numbers displayed by Vim correspond to real lines in the file, not "display lines". If you hit <Enter> a real new line is created and Vim correctly shows its number.

Or you have :set wrap? In this case, wrapped lines are just a presentation trick: because it's still one real line it doesn't make any sense to display line numbers for non-existing lines. Anyway, even with :set wrap, hitting <Enter> still creates a real new line. So I'm not sure what exactly is your problem, here.

romainl
  • 186,200
  • 21
  • 280
  • 313
2

If you want Vim to show display lines, not the actual, physical lines, that's not possible. It also doesn't make sense, since no movement command except gj / gk works on display lines, and you cannot use them in :[range].

Ingo Karkat
  • 167,457
  • 16
  • 250
  • 324
  • I'm nit-picking -- and I completely agree with your answer -- but the movement commands `g$`, `g^`, `g0` and `g_` also work on display lines. I'm not sure if there's a complete list anywhere. – Prince Goulash Nov 15 '12 at 10:43
  • @PrinceGoulash: Your additional commands move horizontally (unless a count is given -- I added this to prevent another nitpick :-), but for this question, only vertical movement to other lines is important; that's why I did not mention them. Thanks for noticing and commenting, though. – Ingo Karkat Nov 15 '12 at 11:22
  • 1
    It would be nice though. It would have to be a new feature or a plugin. It'd make things like doing 5gj to move the cursor 5 visual lines down easier if we had relative display-line numbering. This could probably be done by scripting your own gutter plugin for display lines, but it might be tricky. – trusktr Oct 12 '13 at 19:50