Vim 自动缩进换行
我如何让vim将光标放在大括号内以新行开始,即用|表示光标位置:
class {
|
}
现在使用我的设置,它只会执行此操作,
class {
|}
我在 .vimrc 文件中得到了这个 set autoindent shiftwidth=2 tabstop=2 noexpandtab
基本上我只想知道普通的 IDE 如何缩进它。
更新:
我找到了如何使用 inoremap { {
执行此操作
How do I get vim to place the cursor within the braces starting on a new line, ie with | denoting the cursor position :
class {
|
}
right now with my settings it only does this
class {
|}
I got this in my .vimrc file
set autoindent shiftwidth=2 tabstop=2 noexpandtab
Basically I just want how a normal IDE would indent it.
update:
I found how to do this with inoremap { {<CR>}<Esc>O
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我有 Ubuntu 12.04,我在主目录中没有发现 vimrc 文件。全局
vimrc
文件位于/etc/vim/vimrc
中。这个文件里几乎什么都没有。所以对我来说,将这 3 行添加到
/etc/vim/vimrc
的末尾,当你下次输入
{
时,它将被组合更改{
,输入,}
,向上,结束,输入。cindent
和autoindent
将添加所需数量的制表符。PS 我不擅长调整 vim,所以有些解释可能不太准确。我认为这就是它的运作方式。
I have Ubuntu 12.04 and I found no
vimrc
file in home directory. Globalvimrc
file was in/etc/vim/vimrc
.There was almost nothing in this file. So for me it worked to add this 3 lines to the end of
/etc/vim/vimrc
When you will type
{
next time it will be changed by combination{
, Enter,}
, up, end, Enter.cindent
andautoindent
will add required amount of Tab's.P.S. I'm not good in tuning up vim so some explanations may be not so accurate. It's how I think it works.
我发现 delimitMate 完全按照您所描述的方式进行操作(即自动插入结尾的
}
)。请注意,您必须通过在配置中添加let delimitMate_expand_cr=1
来告诉 delimitMate 展开回车符。根据我的观察,这正是 TextMate 和 SublimeText 中发现的行为。
I found that delimitMate does exactly what you describe and more (that is, automatically inserting the ending
}
). Note you have to tell delimitMate to expand carriage returns by addinglet delimitMate_expand_cr=1
to your config.From my observation, this is exactly the behaviour found in TextMate and SublimeText.
将其放入您的 .vimrc 中:
假设
autoindent
和smartindent
设置正确,在大括号之间键入Ctrl + Return
会将光标放在您想要的位置成为。Put this in your .vimrc :
Assuming
autoindent
andsmartindent
are set correctly, typingCtrl + Return
between braces will put your cursor where you want it to be.autoindent
指的是它将当前的缩进级别转移到后续行。要使其根据语法缩进,您还需要指定一个标志,例如smartindent
或cindent
。autoindent
refers to it carrying over the current indentation level onto subsequent lines. To get it to indent according to syntax, you need to specify a flag likesmartindent
orcindent
as well.我在
.vimrc
中写了这个。上面的代码首先检查您是否使用
Enter
来确认代码完成,如果没有,它将缩进{|}
当您输入Enter
时。此外,它还提供 html 标签自动缩进。对于您的问题:
按
Enter
,您将得到按
Enter
,您将得到I wrote this in my
.vimrc
The code above first check if you are using
Enter
to do confirm a code completion, if not it will indent the{|}
when you typeEnter
. Also, it provides html tags auto indent.For your problem:
press
Enter
and you will getpress
Enter
and you will get在文件底部,我使用:
例如
Dockerfile
:如果您只想保留缩进,请使用
# vim: st ai si
At bottom of the file, I'm using:
For example
Dockerfile
:If you want keep the indentation only, use
# vim: st ai si