0

Normally, when you type text (this is just annoyance when you are taking notes) in vim, and you exceed the line length of the program, it keeps the line going but the line number isn't present:

100 Some text here, blah blah blah blah blah blah blah blah blah blah blah blah
    blah blah blah blah
101

Is there a way to make it so vim just pushes your text to the next line or creates a new one? Why do terminal text editors like vim and nano make typing over the line length kinda annoying to deal with?

1
  • Did we addressed your question or do you still something open? Feel free to precise your question or comment such that we can help you better. If the solution fit you need maybe you can accept the answer to let the question rest :-) Apr 24, 2023 at 14:25

1 Answer 1

1

To have the desired behavior for the text file I would add the following line to ~/.vimfiles/ftplugin/text.vim or ~/.vimfiles/after/ftplugin/text.vim:

:setlocal textwidth=80
:setlocal formatoptions+=t

This called hard break. With the this setting Vim will insert a carriage return when the cursor pass the maximum value (80 in the example).

Remark: If you want the behavior to be the default behavior for all type of buffers you can add the following lines to the ~/.vimrc file:

:set textwidth=80
:set formatoptions+=t

If you would like that Vim reformat the paragraph when inserting text you can do:

:setlocal formatoptions+=a

A paragraph is by default a list of lines separate by a blank line. But it can also be a line that end with a non white-space.

:setlocal formatoptions+=w

You can also reformat a list of lines using the gq command.

More information with:

  • :help textwidth
  • :help breakat
  • :help formatoptions
  • :help gq
10
  • unfortunately those lines of code have no effect on the line wrap for me when put in the .vimrc file...to be clear, i want a new line to materialize when i go over the line wrap instead of putting everything on the line, but it's not big deal, overrall vim is better for programming than any other text editor use.
    – user8919
    Apr 23, 2023 at 20:15
  • If you want hat to be global to all file type you can replace :setlocal by :set and put the lines in .vimrc. Apr 23, 2023 at 20:18
  • But if you want the settings to apply only to some file type you have to put the line in the corresponding vimfiles/ftplugin file e.g.: vimfiles/ftplugin/text.vim Apr 23, 2023 at 20:20
  • But to verify that the behavior is the one you want you could just type the commands and verify that for the current buffer the behavior is as you expect. Apr 23, 2023 at 20:23
  • 1
    2 points: just changing the settings won't automatically reformat a buffer. But the gq operator will, and by default respects 'textwidth'.
    – D. Ben Knoble
    Apr 24, 2023 at 13:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.