Vim 自动换行

发布于 2025-01-02 09:49:17 字数 217 浏览 4 评论 0原文

当我在 vim 中编写一长行文本(例如在 Latex 中编写一个段落)时,它会将我的文本包装成多行,这很好。但是,如果我随后尝试使用“j”和“k”(或向上/向下箭头)导航这些行,它将跳过整个段落。我通过突出显示该段落并按 gq 解决了这个问题。这会在每行末尾插入换行符。

我的问题是,有没有办法自动执行此操作,这样我就不必继续突出显示文本并按 gq

When I'm writing a long line of text in vim (such as a paragraph in latex), it wraps my text into multiple lines which is good. However, if I then try to navigate these lines with 'j' and 'k' (or the up/down arrows) it will skip the entire paragraph. I fixed this problem by highlighting the paragraph and pressing gq. This inserts line breaks at the end of each line.

My question is, is there a way to automate this, so I don't have to keep highlighting text and pressing gq?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

套路撩心 2025-01-09 09:49:17

您可以使用 textwidth 选项限制行的宽度
(参见:help tw)。

例如,如果你想将宽度限制为 80 列,你可以使用:

:set tw=80

使用此选项,当你输入的内容超过 80 列时,Vim
会自动插入换行符。

You can limit the width of a line with the textwidth option
(see :help tw).

For example, if you want to limit the width to 80 columns, you can use:

:set tw=80

With this option, when you will type something longer than 80 columns, Vim
will automatically insert a newline character.

┊风居住的梦幻卍 2025-01-09 09:49:17

您需要退后一点并使用 gjgk 在换行内向下和向上。

由于 gjgk 的工作方式与非换行中的 jk 完全相同,因此您可以安全地映射 < code>j 或 gjkgk 使一切变得无缝。

- 编辑 -

是的,它并没有解决艾迪的直接问题,但它解决了他最初的问题(缠绕线中的垂直运动),这导致他找到了一个糟糕的解决方法,反过来又使他陷入了这种境地。

You need to step back a little and use gj and gk which go down and up inside wrapped lines.

Since gjand gk work exactly the same as j and k in non-wrapped lines you can safely map j or <down> to gj and k or <up> to gk making it all seamless.

-- EDIT --

Yes it doesn't adress Eddy's immediate problem but it solves his original problem (vertical movement in wrapped lines) which led him to a poor workaround that, in turn, put him in this situation.

时间你老了 2025-01-09 09:49:17

这些是我使用的映射。他们处理了我迄今为止遇到的所有案件。

" Modify the up/down keys so that they move per virtual (rather than
" physical) line if a line is displayed wrapped and no count for the
" command has been specified. Always use logical line steps for quickfix
nnoremap <expr> k ((v:count) ? 'k' : ((&buftype == 'quickfix') ? 'k' : 'gk'))
nnoremap <expr> j ((v:count) ? 'j' : ((&buftype == 'quickfix') ? 'j' : 'gj'))
nnoremap <expr> <Up> ((v:count) ? 'k' : ((&buftype == 'quickfix') ? 'k' : 'gk'))
nnoremap <expr> <Down> ((v:count) ? 'j' : ((&buftype == 'quickfix') ? 'j' : 'gj'))
xnoremap k gk
xnoremap j gj
vnoremap <Up> gk
vnoremap <Down> gj

" Move to start and end of virtual line (this will default to normal behaviour
" if the line isn't wrapped)
nnoremap 0 g0
nnoremap <Home> g0
nnoremap ^ g^
nnoremap $ g$
nnoremap <End> g$

" These mappings need to deal with virtual line numbers in 'insert' mode,
" but they need to do it without messing up normal 'completion menu' operation
inoremap <expr> <Up> pumvisible() ? "\<Up>" : "\<C-o>gk"
inoremap <expr> <Down> pumvisible() ? "\<Down>" : "\<C-o>gj"

These are the mappings I use. They handle all the cases I have encountered up to now.

" Modify the up/down keys so that they move per virtual (rather than
" physical) line if a line is displayed wrapped and no count for the
" command has been specified. Always use logical line steps for quickfix
nnoremap <expr> k ((v:count) ? 'k' : ((&buftype == 'quickfix') ? 'k' : 'gk'))
nnoremap <expr> j ((v:count) ? 'j' : ((&buftype == 'quickfix') ? 'j' : 'gj'))
nnoremap <expr> <Up> ((v:count) ? 'k' : ((&buftype == 'quickfix') ? 'k' : 'gk'))
nnoremap <expr> <Down> ((v:count) ? 'j' : ((&buftype == 'quickfix') ? 'j' : 'gj'))
xnoremap k gk
xnoremap j gj
vnoremap <Up> gk
vnoremap <Down> gj

" Move to start and end of virtual line (this will default to normal behaviour
" if the line isn't wrapped)
nnoremap 0 g0
nnoremap <Home> g0
nnoremap ^ g^
nnoremap $ g$
nnoremap <End> g$

" These mappings need to deal with virtual line numbers in 'insert' mode,
" but they need to do it without messing up normal 'completion menu' operation
inoremap <expr> <Up> pumvisible() ? "\<Up>" : "\<C-o>gk"
inoremap <expr> <Down> pumvisible() ? "\<Down>" : "\<C-o>gj"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文