Setup Vim to cut fold line after 80 characters - vim

In my .vimrc file I have textwidth=80 and a white line indicating 80 characters per line limit. Now, as I am also using folds, is there any way to cut fold lines to 80 characters.
Right now a fragment of C file in my setup looks like this:

The display of folded lines can be customized by the foldtext setting. In your case you likely want to restrict the width of the text so you would need to write a custom function that does this. As an example consider my CustomFoldText() function (simply specify a maximum width via the g:custom_foldtext_max_width variable.)

Related

How can I tell Vim to fold GEDCOM files?

GEDCOM files start with a level number 0,1,2, etc., e.g. 2 DATE 10 SEP 1843. The GEDCOM syntax file does not support folding. It should take only a couple of colon commands to tell Vim that it should automatically assign the level number to the line's foldlevel so that zM displays only the level 0 lines, etc. Unfortunately my Vimscript skills are not up to it.
Put these lines inside gedcom.vim:
set foldmethod=expr
foldexpr=getline(v:lnum)

Word wrapping a txt file in vim not working as expected

I am trying to edit a file on vim. You can download the txt file here.
I am following the instructions on this blog. I am also using what I read on StackOverflow.
First, I did this inside vim (command mode):
:set textwidth=80
Second, I used visual mode v to select all lines in the file. I started at the first line and the pressed G (goes to the last line). This made all the file selected.
Third, to reformat it, I did:
gq
Ok, the text is close to what I want. However, when I do this:
:echo max(map(range(1, line('$')), "col([v:val, '$'])")) - 1
The output is:
90
The command above shows me the length in characters of the lengthiest line. The output should be 80, not 90!
What I set as the limit of the text wrap was: 80
What mistake am I making? How can I wrap the text to 80 columns?
I started to use Vim this week. I am sorry if this question is too naive.
Your text width and reformatting is working fine, but the expression col is actually counting the "byte index" of the column position at the end of each line (not the character position). See :help col for all the info you need on how col works.
Instead, try using a character counting function like strchars:
echo max(map(range(1, line('$')), "strchars(getline(v:val))"))
On your example text I get an output of 83 because of the way the wrapping works on whitespace which count as characters. To take care of that, you could trim trailing whitespace with something like :%s/\s*$//, and now my example expression above using strchars returns 80 as expected.
:help col() gives you the byte index of the column. This would work as expected if you only had single-byte characters like c or a but your text contains a lot of characters like ç (U+00e7) or á (U+00e3) which are encoded with more than one byte so the count will be off for many lines.
What you actually want is to count screen columns, with :help virtcol().

vim command to restructure/force text to 80 columns

I know there are ways to automatically set the width of text in vim using set textwidth (like Vim 80 column layout concerns). What I am looking for is something similar to = (the indent line command) but to wrap to 80. The use case is sometimes you edit text with textwidth and after joining lines or deleting/adding text it comes out poorly wrapped.
Ideally, this command would completely reorganize the lines I select and chop off long lines while adding to short ones. An example:
long line is long!
short
After running the command (assuming the wrap was 13 cols):
long line is
long! short
If this isn't possible with a true vim command, perhaps there is a command-line program which does this that I can pipe the input to?
After searching I found this reference which has some more options: http://www.cs.swarthmore.edu/help/vim/reformatting.html
Set textwidth to 80 (:set textwidth=80), move to the start of the file (can be done with Ctrl-Home or gg), and type gqG.
gqG formats the text starting from the current position and to the end of the file. It will automatically join consecutive lines when possible. You can place a blank line between two lines if you don't want those two to be joined together.
Michael's solution is the key, but I most often find I want to reformat the rest of the
current paragraph; for this behavior, use gq}.
You can use gq with any movement operators. For example, if you only want to reformat to the end of the current line (i.e. to wrap the line that your cursor is on) you can use gq$
You can also reformat by selecting text in visual mode (using `v and moving) and then typing gq.
There are other options for forcing lines to wrap too.
If you want vim to wrap your lines while you're inserting text in them instead of having to wait till the end to restructure the text, you will find these options useful:
:set textwidth=80
:set wrapmargin=2
(Don't get side-tracked by wrap and linebreak, which only reformat the text displayed on screen, and don't change the text in the buffer)
Thanks to a comment from DonaldSmith I found this, as the textwidth option didn't reformat my long line of text (I was converting playing with hex-to-byte conversions):
:%!fold -w 60
That reformated the whole file (which was one line for me) into lines of length 60.
If you're looking for a non-Vim way, there's always the UNIX commands fmt and par.
Notes:
I can't comment on Unicode, it may or may not behave differently.
#nelstrom has already mentioned using par in his webcast.
Here's how we would use both for your example.
$ echo -e 'long line is long!\nshort' > 3033423.txt
$ cat 3033423.txt
long line is long!
short
$ fmt -w 13 3033423.txt
long line is
long! short
$ par 13gr 3033423.txt
long line is
long! short
To use from inside Vim:
:%! fmt -w 13
:%! par 13gr
You can also set :formatprg to par or fmt and override gq. For more info, call :help formatprg inside Vim.
Almost always I use gq in visual mode. I tell my students it stands for "Gentlemens' Quarterly," a magazine for fastidious people.

How to add line numbers to range of lines in Vim?

How can I add line numbers to a range of lines in a file opened in Vim? Not as in :set nu—this just displays line numbers—but actually have them be prepended to each line in the file?
With
:%s/^/\=line('.')/
EDIT: to sum up the comments.
This command can be tweaked as much as you want.
Let's say you want to add numbers in front of lines from a visual selection (V + move), and you want the numbering to start at 42.
:'<,'>s/^/\=(line('.')-line("'<")+42)/
If you want to add a string between the number and the old text from the line, just concatenate (with . in VimL) it to the number-expression:
:'<,'>s/^/\=(line('.')-line("'<")+42).' --> '/
If you need this to sort as text, you may want to zero pad the results, which can be done using printf for 0001, 0002 ... instead of 1, 2... eg:
:%s/^/\=printf('%04d', line('.'))/
Anyway, if you want more information, just open vim help: :h :s and follow the links (|subreplace-special|, ..., |submatch()|)
cat -n adds line numbers to its input. You can pipe the current file to cat -n and replace the current buffer with what it prints to stdout. Fortunately this convoluted solution is less than 10 characters in vim:
:%!cat -n
Or, if you want just a subselection, visually select the area, and type this:
:!cat -n
That will automatically put the visual selection markers in, and will look like this after you've typed it:
:'<,'>!cat -n
In order to erase the line numbers, I recommend using control-v, which will allow you to visually select a rectangle, you can then delete that rectangle with x.
On a GNU system: with the external nl binary:
:%!nl
With Unix-like environment, you can use cat or awk to generate a line number easily, because vim has a friendly interface with shell, so everything work in vim as well as it does in shell.
From Vim Tip28:
:%!cat -n
or
:%!awk '{print NR,$0}'
But, if you use vim in MS-DOS, of win9x, win2000, you loss these toolkit.
here is a very simple way to archive this only by vim:
fu! LineIt()
exe ":s/^/".line(".")."/"
endf
Or, a sequence composed with alphabet is as easy as above:
exe "s/^/".nr2char(line("."))."/"
You can also use a subst:
:g/^/exe ":s/^/".line(".")."^I/"
You can also only want to print the lines without adding them to the file:
"Sometimes it could be useful especially be editing large source files to print the line numbers out on paper.
To do so you can use the option :set printoptions=number:y to activate and :set printoptions=number:n to deactivate this feature.
If the line number should be printed always, place the line set printoptions=number:y in the vimrc."
First, you can remove the existing line numbers if you need to:
:%s/^[0-9]*//
Then, you can add line numbers. NR refers to the current line number starting at one, so you can do some math on it to get the numbering you want. The following command gives you four digit line numbers:
:%!awk '{print 1000+NR*10,$0}'
The "VisIncr" plugin is good for inserting columns of incrementing numbers in general (or letters, dates, roman numerals etc.). You can control the number format, padding, and so on. So insert a "1" in front of every line (via :s or :g or visual-block insert), highlight that column in visual-block mode, and run one of the commands from the plugin.
If someone wants to put a tab (or some spaces) after inserting the line numbers using the this excellent answer, here's a way. After going into the escape mode, do:
:%s/^/\=line('.').' '/
^ means beginning of a line and %s is the directive for substitution. So, we say that put a line number at the beginning of each line and add 4 spaces to it and then put whatever was the contents of the line before the substitution, and do this for all lines in the file.
This will automatically substitute it. Alternatively, if you want the command to ask for confirmation from you, then do:
:%s/^/\=line('.').' '/igc
P.S: power of vim :)
The best reply is done in a duplicate question.
In summary:
with CTRL-V then G I 0 You can insert a column of zero.
Then select the whole column and increment:
CTRL-V g CTRL-A
See also: https://vim.fandom.com/wiki/Making_a_list_of_numbers#Incrementing_selected_numbers

How do you enable word wrap in vim when printing

I wanted to print a simple text document and make sure words wrap on word boundaries. I tried both
set linebreak
and
set wrap
but when printing, it just breaks on the right column in the middle of words. Is this possible for printing?
You are creating a text file without any built-in linebreaks so each paragraph is a single "line", even though with linebreak and wrap set, it looks like they are multiple lines). This is why printing breaks at fixed places. (According to http://www.vim.org/htmldoc/various.html#printing it does not seem like you can have vim respect linebreak/wrap during print.)
To avoid this, if you want text to wrap while you are editing, do
set textwidth=70
to wrap at the 70th column. If you want your file to have long lines (e.g., so it formats fine when loaded into MS Word or something), then you will have to pre-process the text version before printing it. So, for example, you can try:
fmt file.txt | lpr
or if you have enscript installed, you should be able to try:
enscript --word-wrap file.txt
to print. An existing file can be wrapped by running in vim:
gggqG
that is, 'gg' to go to start of file and 'gqG' to reformat 'gq' from the current position (i.e. the first line) to the last line (by going to 'G'). 'gq' will respect your current textwidth setting.

Resources