Limit the number of characters per line editor vi linux - linux

I am trying that in the editor vi, limit the number of characters per line you can do. Once you reach those x characters, break the line with a carriage return. For example: limit 50 characters.
I have not seen that there is any command (like :set nu to write the numbers of the lines in editor vi) or something similar to activate it.
I know that in order for it to take effect I have to create the file ~/.vimrc but there I don't know how to edit it so that when I later create a file, I restrict it.

Are you really using vi? You are probably using vim. If so, :help will answer most of your _I don't know_s. From there, you can also jump to specific parts of the help following the links (you recognize them as they are likely colored, bolded, or highligthed somehow) by hitting Ctrl+] (and yes, you can also enter :help ctrl-] to see the help on the key combination I've just mentioned).
In order to do what you want, it is enough that you put set textwidth=50 or set tw=50 in your ~/.vimrc file (note that a value of zero for tw means that the option is disabled, or if you prefer, that tw is infinite). If you want to look at the description of this option, enter :help textwidth.
This setting (:set tw=50), however, won't change already existing lines; in order to change all the already existing lines according to the current setting of tw, you can do gggqG, which moves to the first line (gg) and then formats the lines (gq, for info enter :help gq, which will also reveal the reason why this command will have an effect even if tw is 0) till to the last line (G moves to last line of file).

Related

Vim: colon appends .,.+3

Sometimes when entering :, vim appends .,.+3 to it, preventing me from executing a command. I couldn't find anything about this behaviour online, is there a way to get do away with it? At the moment, I use vim 8.0 (in iTerm2) but it also used to occur in earlier versions.
This happens when you typed a number before the colon. Vim interprets it as the number of lines on which you want your ex command to operate.
If you press 4: you get :.,.+3 which is a range that covers the current line (.) and the three lines below the current line (.+3).
That behavior is old and documented in :help N: (:help :range is an interesting read, too).
If you want to get rid of that range, see :help <C-u>.

How to configure Vim word wrap options vimrc

I'm trying to configure Vim so that it always wraps at x-number of columns automatically, at all times such as in this other editor that I provided a screenshot of-
Use :set wrap and :set textwidth=N . For permanent effect add the wrap settings to the vim start-up file .vimrc
set wrap
set textwidth=X
To apply the wrapping to an existing file, move to the start of the file (you can use gg to do this). Then type gqG to apply the re-formatting to the entire file.
Also setting a margin may be useful:
set wrapmargin=0
If you want hard wrapping, i.e. real reformatting of the text by inserting newline characters, the 'textwidth' setting (as outlined in suspectus's answer) is the key. You need to reformat existing text, e.g. via the gq command.
For soft wrapping, i.e. where long lines just appear to be broken in the editor window, :set wrap will always fill the entire available window space; you cannot restrict that to take less. Either resize the Vim window, or add a padding buffer to the right that restricts the available space to the current window. For the latter, my LimitWindowSize plugin may be helpful.
Judging from the second screenshot, you seemed to want soft-wrap. This answer suggests a way to achieve a soft-wrap effect .
I'm generalizing the solution a bit compared to the one I referred to, but basically
use :set nuw to see how many columns the line numbers are occupying, let's call this nuw.
then use :set columns=x where x = (the number of columns you want + nuw).
Note that the columns is automatically reset to window width once you resize the window, so this manual setting is not persistent.

Fixing too long comment lines in Vim

I'm looking for a convenient way to fix comments where line lengths exceed a certain number of characters in Vim. I'm fine with doing this manually with code, especially since it's not that frequent, plus refactoring long lines is often language, or even code-style dependent, but with comments this is pure drudgery.
What happens is I often spot some issue in a comment, tweak one or two words and the line spills out of the, say, 80 character limit. I move the last word to the next line and then the next line spills, and so on. Does anyone know a way to do this automatically in Vim?
I would recommend putting the following into your vimrc if this is a regular issue:
nnoremap <leader>f gqip
This maps the leader f shortcut (f is for "format") to format the comment (considered a paragraph after setting some formatoption flags) with gq which formats the comment to be the width of the currently set textwidth or tw option. You should set textwidth in your .vimrc with textwidth=80.
Formatoptions is the other thing you should fiddle with, specifically in your case by adding the acq flags with formatoptions+=acq. Be careful to remove the t flag with formatoptions-=t because that will automatically wrap all of your code, not just recognized comments. After doing all this you should be able to hit f and format inside only the comment, irrespective of whether is is surrounded by blank lines.
Here is the relevant info on formatoptions so you can make your own choices.
t Auto-wrap text using textwidth
c Auto-wrap comments using textwidth, inserting the current comment
leader automatically.
r Automatically insert the current comment leader after hitting
<Enter> in Insert mode.
o Automatically insert the current comment leader after hitting 'o' or
'O' in Normal mode.
q Allow formatting of comments with "gq".
Note that formatting will not change blank lines or lines containing
only the comment leader. A new paragraph starts after such a line,
or when the comment leader changes.
w Trailing white space indicates a paragraph continues in the next line.
A line that ends in a non-white character ends a paragraph.
a Automatic formatting of paragraphs. Every time text is inserted or
deleted the paragraph will be reformatted. See |auto-format|.
When the 'c' flag is present this only happens for recognized
comments.

How to keep my own CR in Vim with automatic format?

I used set fo+=a in Vim to enable automatic format when typing.
With that set, the <CR> I pressed will be erased when I continue to type, if the length of current line is less than lw, and that is not what I want.
What I want is:
Still able to add a <CR> automatically if the line is longer than lw.
When I type a <CR> manually when the line length is less than lw, I don't want that <CR> erased when I continue to type.
Thanks.
You can't. The format option "a" is designed to reformat your paragraph every time your paragraph is changed, so there's no way for you to keep any single line breaks inside your paragraph, regarding the definition of "paragraph" in VIM.
If you only need to be able to wrap long lines while typing, you don't need option "a". Option "t" is already quite sufficient for your case.
t Auto-wrap text using textwidth
If you are fine with using trailing spaces to indicate that a paragraph continues (by default it only ends on an empty line) then you can also do
set fo+=w
. Then to keep vim from joining lines all you need is to end it with something other then a space.
Note: this is not going to change the meaning of the paragraph for motion commands (in fact, there is nothing that can do change it).

How can I add non mouse-selectable line numbers to vim? [duplicate]

This question already has answers here:
How to clear the line number in Vim when copying?
(12 answers)
Closed 6 years ago.
So I've figured out how to add line numbers to vim (:set no or :set number) but how can I make it so that when I use my mouse in a terminal emulator to select a block of lines, it does not also select the numbers?
For example, say I have three lines that look like so in vim:
1 First line
2 Second
3 Third
If I want to select the three lines with the mouse what I want is for it to ONLY select the actual text. But what ends up happening is it selects the line numbers as well as all the space to the left and right of the line numbers.
Is there any way to change this behavior? BTW, I'm using the gnome terminal editor in gnome if that makes a difference.
Use the following:
:set mouse=a
to turn on xterm style mousing in all modes. This will allow you to do what you want. Keep in mind if the vim is remote via ssh, you'll need X11 forwarding turned on for the selection to make it to your local clipboard.
AFAIK, that is not possible.
The only thing I can add at the moment is that you'd be better off with
:set invnumber
It will inverse the current condition, so you can map it to a key, and toggle it. That way you don't have to remember two commands.
I agree with the first answer. If you use gvim, you can experiment with using set mouse=n and set mouse=a, which should change the line number selecting behavior.
When I want to select text into a terminal, I remove line numbers.
:set nonu
When I finished
:set nu
You're probably not on a Macintosh, and I can't tell if you mean you want to use system copy rather than vim's yank.
But if you are both those things, you can use option-drag to select text. This creates a 2d box over the text, selecting things that are under it. It should be functionally the same as 'take columns x1 to x2 of rows y1 to y2'.
I'm pretty sure this is not possible in terminal-vim. There is no accepted standard (e.g. a TTY escape) for indicating blocks of characters that aren't highlightable by mouse, as far as I know.
I would use gvim if you want to be able to do this at any cost. Its behavior is as you describe.
My recommendation is to get used to the little box on the right hand corner of the screen which has the current line's number and character position information. Before I used vim I could never imagine living without line numbers, but since then I've moved beyond this. Not having line numbers clutter up the screen allows for distraction free code viewing, and do you really need to know that the line number above your current active line is one less than your current line's number, and the line below is one more?
A copy without mouse:
Enter Visual mode: Position your cursor, press v (For complete line V) move up or down until desired position
Press control + c to copy into clipboard (if +clipboard exists in :ve command output) press ESC and paste it wherever you want with control + v.
Or press y to yank into register and press p to put the text after the cursor

Resources