问题
This feels like a dumb question, but I can't find an answer on the Internet (or in VIM help). I'm using VIM 7.2 on Mac OS X. All I want to do is wrap my lines at 72 characters, but doing
:set textwidth=72
has no effect. The textwidth is being set correctly (I can see that when I just query ":set textwidth"), but neither existing lines nor new lines that I type after setting textwidth get wrapped. If I start a new line, still doesn't wrap. Open and close the file, no change. I've also tried :set wrapmargin=72 (with textwidth=0), with no effect.
What am I missing here?
回答1:
Try gggqG
to apply the new text width to the whole buffer.
gg
means : go to the beginning of buffergq
means : reformat the text included in the motionG
means : go to the end of the buffer
(It will works if the format options are correctly set, as detailed in Zyx post)
On the other hand, you could also display your existing text with a width of 72 characters by adding a modeline
at the beginning or end of your file. See :help modeline
Something like vim:tw=72
should work.
回答2:
What does
:set formatexpr?
:set indentexpr?
:set cindent?
:set filetype?
:set paste?
:filetype
print.
At least one of those (and I think all of them) will override the setting for textwidth
.
For example, if you're editing a C source file, the C indent rules override textwidth
.
回答3:
I was looking for an answer to the same question and had to scramble around a bit before I found the solution in the VIM docs. So, i thought i will update the thread and save others the time.
The problem in my case was that the default ftplugin was disabling textwidth.
Just updating your .vimrc with (:set tw=79 && :set formatoptions+=t
) won't work since the fplugins are sourced after vimrc.
Here are the steps that i followed:
1) find out what your current formatoptions (inside vim)
:set formatoptions?
formatoptions=croql (note no 't')
2) create a filetype.vim file as suggested by vimdocs (http://vimdoc.sourceforge.net/htmldoc/filetype.html#ftplugin-overrule)
Overrule the settings after loading the global plugin.
You must create a new filetype plugin in a directory from the end of
'runtimepath'. For Unix, for example, you could use this file:
vim ~/.vim/after/ftplugin/fortran.vim
In this file you can change just those settings that you want to change.
3) add the line :set formatoptions+=t
&& :set textwidth=79
in that file.
Voila! next time when you open the file it will set the textwidth to your desired characters.
As a debugging aid you can always check which file is overriding your vimrc setting by prepending your command with verbose. So for e.g. if i want to check who updated the formatoptions last, i would type
:verbose set formatoptions?
formatoptions=croqlt
Last set from ~/.vim/after/ftplugin/fortan.vim
回答4:
Vim won't do anything unless requested. textwidth
will have an effect for currently edited lines if you either have t
(for non-comments only), c
(for comments only) or both in formatoptions
(if a
is not present there, then it will autowrap only when you reach the margin set by textwidth
), or if you use gq
to reformat your text. If I am not mistaking, you can set such formatexpr
or formatprg
so that textwidth
will be ignored.
回答5:
set formatoptions+=t
This may help you
回答6:
I had this exact same issue and found the following solved the issue well enough for me:
autocmd FileType python setlocal textwidth=79 formatoptions+=t
Feel free to change python
to be your file type of choice (e.g. *
or comma separated list python,ruby,go,sh,javascript
)
See
:h fo-table
for details offormatoptions
回答7:
I am using Neovim and have met this issue too. After setting textwidth
to 80 in my init.vim
, the content of a text file does not change when I open the file. For the existing text, we have to manually format it with gggqG
.
When you type new characters in a line, the textwidth
option will work (you will get a linebreak automatically if the character number reaches 80).
来源:https://stackoverflow.com/questions/5136902/vim-textwidth-has-no-effect