I'm creating a document in latex and I would like all lines to be broken at roughly 80 characters. This is easy to do manually when writing new text, but the editing of old text often leads to lines that are longer or shorter. Fixing it leads to other lines that need to be changed and so on.
Is there an automatic way in vim to break lines after 80 characters and merge them if possible?
To make lines be only 80 characters long you can use textwidth
Assuming your latex file has the extension .tex you can add the following line to your vimrc
autocmd BufReadPost *.tex setlocal textwidth=80
To format the current paragraph you can use gqap
I use the following setup.
set textwidth=80
(either manually or in your ~/.vimrc). Then if I have a block of text in a file that I want to format I first mark it in visual mode (i.e., shift-V to switch to visual line mode and than mark all the lines you want to format) and then use gq. See also :help gq.
Related
I have several C files, in which the lines exceed more than 80 columns. I need to wrap them to a 80 column boundary and at the same time maintain proper C syntax. Is this possible in Vim?
I have seen several solutions on the web for doing it on a normal text file. Also, have found tricks to do wrapping in a C file, which is about to be edited. But not for existing C files.
Any tools for achieving this apart from Vim are also welcome.
If you edit c-sources it is likely that you set the cindent option. Unfortunately the textwidth option doesn't work with cindent.
For your purpose artistic style, although it's a very nice program isn't recommended either since it doesn't support the breaking of long lines.
To get what you want i use gnu indent and set equalprg to
let &equalprg= "indent -l80 -i" . &shiftwidth . " <optional args>"
and add
map <leader>i mzgg=G`z
to my ~/.vimrc. With that pressing <leader>i reformats the whole file without changing the cursor's position.
Another possibility would be to add an autocommand that indents the source on every write, like
autocmd BufWrite *.c execute "%!".&equalprg
With vim you could do that with :set tw=79 and gggqG. However, it is not context-aware so you will need to restore certain lines (for example, lines with long strings in them).
For an external tool you might want to look at Artistic Style, which reformat lines where reformatting is not hard (for example, lines containing a {).
When I edit a haskell file in Vim, my editor automatically wraps comments that go past 80 characters to the next line. I'd like Vim to use the same behavior for python files (and text files), but I can't find the setting that does this anywhere, in my ~/.vim/syntax folder or vimrc.
Here are relevant lines of my .vimrc:
set wrap
set textwidth=80
Setting textwidth will put your maximum line length. This would put a new line at that character point (white space could play with an exact 80 a bit though). (This effects the actual formatting of your file).
wrap is indeed what you want for your splitting/wrapping though.
Make sure your .vimrc is in your home directory.
I uses the following script in my vimrc to wrap .txt file automatically. This may give you some hints.
if has('autocmd')
au BufRead,BufNewFile *.txt set wm=2 tw=80
endif
If a line is already longer than textwidth when insert mode is started, the line may not be wrapped even if text is added to the line. A long line is not wrapped when text is added if formatoptions contains "l". If needed, "l" can be removed like so:
:set fo-=l
You can check the value of your formatoptions via:
:set fo?
You can learn the meaning of those letters here or via:
:he fo-table
I am trying to setup vim to skip adding eol on last line or eof, i have tried this
:set binary
:set noeol
:w
which is not perfect cause binary override filetype for later use.
Any other option to set this, i don't need newline on last line.
I wanted to add an answer that I think can be as useful. The selected answer always remove the EOL on files even if they had one to begin with. This may be the behavior that you want, it also may not be. In my opinion I want to preserve the EOL as I originally opened the file.
What I suggest is a slight modification. Put set binary at the top of your .vimrc file. This way any files will open in binary mode. If they have no EOL then vim will detect this and leave it NOEOL. If they have an EOL then it'll recognize it as having an EOL and leave it be.
If you would like new files to also not have EOL then you should set,
au BufNewFile * set noeol
The commands for the chap would be if you always want NOEOL ever. The one thing to be aware of is that if you have a file with spaces at the bottom, then you will lose spaces at the end. This is because the following happens,
VIM reads the file and see a newline at the end and thinks, OK, this is a file with a newline (regardless of whether in binary mode or not).
When you then write a file it executes the autocmd,
set binary mode
set noeol (this deletes the EOL that VIM thought was ending the file and deletes it.
the file gets saved
Now you have a file that has one less EOL in it.
This process will repeat until the EOL's are gone and the file ends in a single character. With my setup, what happens is the file is opened and if VIM sees at least one EOL then it keeps it internally. If you save that file, no matter what it is there (you can check by typing set eol?. In the case that you want to get rid of that internally stored EOL you just say set noeol and then save and BOOM, the last EOL is removed.
WHOOO, I am winded typing this.
This is even better i found somewhere:
au BufWritePre * :set binary | set noeol
au BufWritePost * :set nobinary | set eol
From vim documentation:
'binary' 'bin' boolean (default off)
local to buffer
{not in Vi}
This option should be set **before** editing a binary file. You can also
You should therefore use vim -b or :e ++bin file, or reload using :e! ++bin.
I have came up with this:
" php remove eol from end of file
autocmd FileType php setlocal noeol binary fileformats="mac,unix,dos"
Thank you all.
Cheers.
I like that the long lines are displayed over more than one terminal line; I don’t like that vim inserts newlines into my actual text. Which part of .vimrc I should change?
Use
:set wrap
To wrap lines visually, i.e. the line is still one line of text, but Vim displays it on multiple lines.
Use
:set nowrap
To display long lines as just one line (i.e. you have to scroll horizontally to see the entire line).
I like that the long lines are displayed over more than one terminal line
This sort of visual/virtual line wrapping is enabled with the wrap window option:
:set wrap
By default this will wrap at the first character that won't fit in the window. This means it will wrap in the middle of a word if that's where the window boundary lies. To change it to wrap on word boundaries, you can also:
:set linebreak
This will cause wrap to only wrap at the characters in the breakat setting, which defaults to space, tab, and small set of punctuation characters.
:set breatat
breakat= ^I!#*-+;:,./?
I don’t like that vim inserts newlines into my actual text.
To turn off physical line wrapping, clear both the textwidth and wrapmargin buffer options:
:set textwidth=0 wrapmargin=0
I'm not sure I understand completely, but you might be looking for the 'formatoptions' configuration setting. Try something like :set formatoptions-=t. The t option will insert line breaks to make text wrap at the width set by textwidth. You can also put this command in your .vimrc, just remove the colon (:).
:set tw=0
VIM won't auto-insert line breaks, but will keep line wrapping.
Use :set nowrap .. works like a charm!
You may find set linebreak useful; with set wrap on this will wrap but only cutting the line on whitespace and not in the middle of a word.
e.g.
without linebreak the li
ne can be split on
a word
and
with linebreak on the
line will be
split on
whitespace only
set formatoptions-=t Keeps the visual textwidth but doesn't add new line in insert mode.
Its strange that such a simple setting would require this amount of 'hocus-pocus' to work.
To answer your question now, for me it seemed to work with the combination of the following:
:set wrap linebreak nolist
(this seems to prevent existing lines from breaking, just wrap.)
AND
set formatoptions=l
(this prevents new/edited lines from breaking, while += does not do it for me as other settings/plugins seem to find space and add their own options which override mine.)
If, like me, you're running gVim on Windows then your .vimrc file may be sourcing another 'example' Vimscript file that automatically sets textwidth (in my case to 78) for text files.
My answer to a similar question as this one – How to stop gVim wrapping text at column 80 – on the Vi and Vim Stack Exchange site:
In my case, Vitor's comment suggested I run the following:
:verbose set tw?
Doing so gave me the following output:
textwidth=78
Last set from C:\Program Files (x86)\Vim\vim74\vimrc_example.vim
In vimrc_example.vim, I found the relevant lines:
" Only do this part when compiled with support for autocommands.
if has("autocmd")
...
" For all text files set 'textwidth' to 78 characters.
autocmd FileType text setlocal textwidth=78
...
And I found that my .vimrc is sourcing that file:
source $VIMRUNTIME/vimrc_example.vim
In my case, I don't want textwidth to be set for any files, so I just commented out the relevant line in vimrc_example.vim.
It is correct that set nowrap will allow you to paste in a long line without vi/vim adding newlines, but then the line is not visually wrapped for easy reading. It is instead just one long line that you have to scroll through.
To have the line visually wrap but not have newline characters inserted into it, have set wrap (which is probably default so not needed to set) and set textwidth=0.
On some systems the setting of textwidth=0 is default. If you don't find that to be the case, add set textwidth=0 to your .exrc file so that it becomes your user's default for all vi/vim sessions.
I personnally went for:
set wrap,
set linebreak
set breakindent
set showbreak=ͱ.
Some explanation:
wrap option visually wraps line instead of having to scroll horizontally
linebreak is for wrapping long lines at a specific character instead of just anywhere when the line happens to be too long, like in the middle of a word. By default, it breaks on whitespace (word separator), but you can configure it with breakat. It also does NOT insert EOL in the file as the OP wanted.
breakat is the character where it will visually break the line. No need to modify it if you want to break at whitespace between two words.
breakindent enables to visually indent the line when it breaks.
showbreak enables to set the character which indicates this break.
See :h <keyword> within vim for more info.
Note that you don't need to modify textwidth nor wrapmargin if you go this route.
This has bugged me for a long time, and try as I might I can't find a way round it.
When I'm editing text (specifically latex, but that doesn't matter) files, I want it to auto-wrap at 80 columns. It does this, except if I happen to be in the middle of a parenthetical clause, it indents the text which is very annoying. For example, this works fine
Here is some text... over
two lines.
but this doesn't
Here is some text... (over
two
lines
If anyone can tell me how to turn this off (just for text/latex files) I'd be really grateful. Presumably it has something to do with the fact that this is desired behaviour in C, but I still can't figure out what's wrong.
:set nocindent
The other options do nothing, and the filetype detection doesn't change it.
There are three options you may need to turn off: set noai, set nosi, and setnocin (autoindent, smartindent, and cindent).
This may be related, when pasting from gui into terminal window, vim cannot distinguish paste modes, so to stop any odd things from occuring:
set paste
then paste text
set nopaste
I had similar issues trying to paste xml text, it would just keep indenting. :)
gvim, the gui version of vim, can detect paste modes.
You can have a look at the autoindent option :
autoindent - ai
Copy indent from current line when starting a new line (typing
in Insert mode or when using the "o" or "O" command). If you do not
type anything on the new line except and then type or
, the indent is deleted again. When autoindent is on,
formatting (with the "gq" command or when you reach 'textwidth' in
Insert mode) uses the indentation of the first line. When
'smartindent' or 'cindent' is on the indent is changed in specific
cases. The 'autoindent' option is reset when the 'paste' option is
set. {small difference from Vi: After the indent is deleted when
typing or , the cursor position when moving up or down is
after the deleted indent; Vi puts the cursor somewhere in the deleted
indent}.
From the official Vim documentation
filetype plugin indent on
This switches on three very clever
mechanisms:
Filetype detection. Whenever you start editing a file, Vim will try to
figure out what kind of file this
is. When you edit "main.c", Vim will
see the ".c" extension and
recognize this as a "c" filetype.
When you edit a file that starts with
"#!/bin/sh", Vim will recognize it as
a "sh" filetype. The filetype
detection is used for syntax
highlighting and the other two
items below. See |filetypes|.
Using filetype plugin files Many different filetypes are edited with
different options. For example,
when you edit a "c" file, it's very
useful to set the 'cindent' option to
automatically indent the lines. These
commonly useful option settings are
included with Vim in filetype plugins.
You can also add your own, see
|write-filetype-plugin|.
Using indent files When editing programs, the indent of a line can
often be computed automatically.
Vim comes with these indent rules for
a number of filetypes. See
|:filetype-indent-on| and
'indentexpr'.
:set noai
sets no auto indent tt may be smartindent though. Check out the doc and see if you can find something more
http://www.vim.org/htmldoc/indent.html