将eborisch 的答案与我在这里找到的其他一些答案以及我必须解决的问题相结合,我想出了以下两部分的解决方案:
第一部分使编辑长行文本变得更容易:
" Allow enabling by running the command ":Freeform", or <leader>sw
command! Softwrap :call SetupSoftwrap()
map <Leader>sw :call SetupSoftwrap() <CR>
func! SetupFreeform()
" Use setlocal for all of these so they don't affect other buffers
" Enable line wrapping.
setlocal wrap
" Only break at words.
setlocal linebreak
" Turn on spellchecking
setlocal spell
" Make jk and 0$ work on visual lines.
nnoremap <buffer> j gj
nnoremap <buffer> k gk
nnoremap <buffer> 0 g0
nnoremap <buffer> $ g$
" Disable colorcolumn, in case you use it as a column-width indicator
" I use: let &colorcolumn = join(range(101, 300), ",")
" so this overrides that.
setlocal colorcolumn=
" cursorline and cursorcolumn don't work as well in wrap mode, so
" you may want to disable them. cursorline highlights the whole line,
" so if you write a whole paragraph on a single line, the whole
" paragraph will be highlighted. cursorcolumn only highlights the actual
" column number, not the visual line, so the highlighting will be broken
" up on wrapped lines.
setlocal nocursorline
setlocal nocursorcolumn
endfunc
仅凭这一点,您就可以获得不错的文本包装,以编写诸如降价或自述文件之类的内容。
如其他答案所述,以确切的列宽进行换行需要准确地告诉 vim 有多少列,并在每次调整 vim 大小时覆盖它:
command! -nargs=? Draft :call SetupDraftMode(<args>)
func! SetupDraftMode()
" I like 80 columns + 4 for line numbers
set columns=84
autocmd VimResized * if (&columns > 84) | set columns=84 | endif
:Softwrap
endfunc
这仍然存在几个问题:
- vim 不会在调用 set columns 后清除你指定的列之外的屏幕,我不知道如何告诉它,所以理想情况下你应该在打开 vim 后立即调用它
- vim 会在您打开它时显示带有版本号和一些有用命令的提示,因此这些不会被清除。您可以添加
set shm+=I
以禁用该提示
- 您不能打开任何垂直拆分,因为这样两个拆分都将是 ~40 列。您需要将列设置为所需宽度的 2 倍,然后始终打开拆分。
- 我的 vimscript 很糟糕,但理想情况下,有人可以修改
Draft
上面的函数以将列宽作为参数,或者使用g:explicit_vim_width
可以在窗口大小发生变化时手动设置的全局变量 (?)。