.vimrc文件中的textwidth未生效

6
以下是我完整的 .vimrc 文件:
出现问题的地方在文件底部的 set wrapset textwidth=73(我希望文件在 73 列处自动换行,但似乎并没有生效)。我仍然可以在文件中输入超过 73 列的内容。
是否存在需要修复的冲突命令?还是我做错了什么?
set nocompatible
set smartindent
set cursorline
filetype plugin indent on
set background=dark
syntax enable
set grepprg=grep\ -nH\ $*
syntax on
set mouse=a
set history=1000
set showmode

if has('cmdline_info')
    set ruler                   " show the ruler
    set rulerformat=%30(%=\:b%n%y%m%r%w\ %l,%c%V\ %P%) " a ruler on steroids
    set showcmd                 " show partial commands in status line and
endif

set backspace=indent,eol,start      " backspace for dummys
set linespace=0                 " No extra spaces between rows
set nu                          " Line numbers on
set showmatch                   " show matching brackets/parenthesis
set incsearch                   " find as you type search
set hlsearch                    " highlight search terms
set winminheight=0              " windows can be 0 line high 
set ignorecase                  " case insensitive search
set smartcase                   " case sensitive when uc present
set wildmenu                    " show list instead of just completing
set wildmode=list:longest,full  
set scrolljump=5                " lines to scroll when cursor leaves screen
set scrolloff=3                 " minimum lines to keep above and below cursor
set gdefault                    " the /g flag on :s substitutions by default
set autoindent                  " indent at the same level of the previous line
set shiftwidth=4                " use indents of 4 spaces
set expandtab                   " tabs are spaces, not tabs
set tabstop=4                   " an indentation every four columns
set softtabstop=4               " let backspace delete indent
set matchpairs+=<:>
set comments=sl:/*,mb:*,elx:*/
autocmd FileType c,cpp,java,php,js,python,twig,xml,yml autocmd BufWritePre <buffer> :call setline(1,map(getline(1,"$"),'substitute(v:val,"\\s\\+$","","")'))

set foldmethod=syntax  "fold based on indent
set foldnestmax=10      "deepest fold is 10 levels
set foldlevel=0         "this is just what i use
set wrap
set textwidth=73
set formatoptions+=t

编辑:我希望VIM可以在73行时自动换行(实时)。这是可能的吗?我已经尝试添加set formatoptions+=t来包装文本,但它仍然没有效果。


7
运行:verbose set tw命令,查看覆盖设置的原因。很可能是某个(文件类型)插件所导致的。 - sehe
可能是重复的问题,与 http://superuser.com/questions/250112/textwidth-0-and-wrapwidth-0-in-vimrc-local-not-being-respected 相关。 - Felipe G. Nievinski
3个回答

4

选项wraptextwidth涉及两种完全不同的换行方式。

textwidth设置了一条行宽限制,超过这个限制的每一个新单词(也就是用空格分隔的)都会被放在新的一行上。 附带的.vimrc可能已经正确地工作了,textwidth正是这样做的。

wrap根本不影响编辑文件内容,只是在文件中的行超过显示宽度时,在多行中显示。我并没有找到令人满意的方法来配置它以在固定列宽处换行,而且我个人认为没有这种需求。

然而,如果您发现屏幕末尾的软换行很烦人,有两个方面可以改变:

  • 更改Vim窗口的宽度,使用set columns=73(这相当麻烦,因为它会改变整个窗口的宽度)
  • 将软换行放在最后一个单词边界上,而不是屏幕边界上,通过设置linebreak选项,参见help linebreak了解详情。

2
把这段代码放入.vimrc文件中,对我有效:
autocmd FileType * set textwidth=58

2

也许不是最好的解决方案,但尝试在自动命令中分配textwidthformatoptions,然后格式化文件的所有行。

autocmd BufRead * set fo+=t tw=73|normal gggqG

这在我的测试中有效。你可以更具体地使用*.txt或类似的替换*


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接