[英]How can I set different textwidths for specific file extensions in .vimrc?
I would like to have a default textwidth of 80 characters except for a few file extensions like txt
. 我希望默认文本宽度为80个字符,除了像txt
这样的文件扩展名。 The following lines appear to work, except for the first time when I edit (and create) a txt
file. 除了第一次编辑(和创建) txt
文件时,以下行似乎有效。
setlocal textwidth=80
autocmd bufreadpre *.txt set textwidth=0
What is the correct way to do this? 这样做的正确方法是什么?
use setlocal
使用setlocal
autocmd bufreadpre *.txt setlocal textwidth=0
instead of set
. 而不是set
。
With setlocal
you make sure that the value you're setting is set in the current buffer, not for all buffers. 使用setlocal
您可以确保在当前缓冲区中设置要设置的值,而不是所有缓冲区。
First, you've got the scopes the wrong way; 首先,你的范围是错误的; use :set
for the global default and :setlocal
for the buffer-local override in the :autocmd
. use :set
为全局默认值, :setlocal
用于:autocmd
的缓冲区本地覆盖。
Second, BufReadPre
is only for reading existing files, not new ones; 其次, BufReadPre
仅用于读取现有文件,而不是新文件; that's why it doesn't work the first time. 这就是它第一次不起作用的原因。 Instead, you should use BufNew,BufRead
; 相反,你应该使用BufNew,BufRead
; this captures both cases, and only applies after the file was read, so it will still work when you use modelines or have a setting in an filetype plugin . 这会捕获这两种情况,并且仅在读取文件后应用,因此在使用modelines或在filetype插件中设置时它仍然有效。
Third, the :autocmd
solution tends to become unwieldy once you have many customizations. 第三,一旦你有很多自定义, :autocmd
解决方案往往变得难以处理。 If you only want to enable a setting for certain filetypes, put the corresponding :setlocal
commands into ~/.vim/after/ftplugin/<filetype>.vim
, where <filetype>
is the actual filetype (eg java
). 如果您只想为某些文件类型启用设置,请将相应的:setlocal
命令放入~/.vim/after/ftplugin/<filetype>.vim
,其中<filetype>
是实际的文件类型(例如java
)。 (This requires that you have :filetype plugin on
; use of the after directory allows you to override any default filetype settings done by $VIMRUNTIME/ftplugin/<filetype>.vim
.) (这要求您具有:filetype plugin on
;使用after目录可以覆盖$VIMRUNTIME/ftplugin/<filetype>.vim
完成的任何默认文件类型设置。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.