如何在保存前告诉Vim自动缩进

如何在保存前告诉Vim自动缩进,vim,auto-indent,Vim,Auto Indent,我最近开始在研究生级别的项目中使用Vim。我面临的主要问题是,有时我签入未插入的代码。我觉得如果我能以某种方式创建一个自动缩进+保存+关闭的快捷方式,那么应该可以解决我的问题 My.vimrc文件: set expandtab set tabstop=2 set shiftwidth=2 set softtabstop=2 set pastetoggle=<F2> syntax on filetype indent plugin on 设置扩展选项卡 设置tabstop=2 设置s

我最近开始在研究生级别的项目中使用Vim。我面临的主要问题是,有时我签入未插入的代码。我觉得如果我能以某种方式创建一个自动缩进+保存+关闭的快捷方式,那么应该可以解决我的问题

My.vimrc文件:

set expandtab
set tabstop=2
set shiftwidth=2
set softtabstop=2
set pastetoggle=<F2>
syntax on
filetype indent plugin on
设置扩展选项卡
设置tabstop=2
设置shiftwidth=2
设置softtabstop=2
设置粘贴切换=
语法高亮
上的文件类型缩进插件
有没有办法创建这样的命令快捷方式&使用
:x
(保存+退出)进行覆盖


请让我知道。

我建议首先打开
自动缩进以避免此问题。在开发的每个阶段使用适当缩进的代码要容易得多

set autoindent
通过
:help autoindent
阅读文档

但是,=命令将根据文件类型的规则缩进行。您可以创建一个
BufWritePre
autocmd来对整个文件执行缩进

我还没有测试过这个,也不知道它的实际效果如何:

autocmd BufWritePre * :normal gg=G
阅读
:help autocmd
,了解有关该主题的更多信息<代码>gg=g
细分为:

  • :normal
    作为普通模式编辑命令而不是
    :ex
    命令执行
  • gg移到文件的顶部
  • =缩进到
  • G。。。文件的结尾
但我真的不推荐这种策略。习惯使用
设置自动缩进
。在所有文件上定义此
autocmd
(与
*
一样)可能是不明智的。只能在某些文件类型上执行此操作:

" Only for c++ files, for example
autocmd BufWritePre *.cpp :normal gg=G

将以下内容添加到
.vimrc

" Restore cursor position, window position, and last search after running a
" command.
function! Preserve(command)
  " Save the last search.
  let search = @/

  " Save the current cursor position.
  let cursor_position = getpos('.')

  " Save the current window position.
  normal! H
  let window_position = getpos('.')
  call setpos('.', cursor_position)

  " Execute the command.
  execute a:command

  " Restore the last search.
  let @/ = search

  " Restore the previous window position.
  call setpos('.', window_position)
  normal! zt

  " Restore the previous cursor position.
  call setpos('.', cursor_position)
endfunction

" Re-indent the whole buffer.
function! Indent()
  call Preserve('normal gg=G')
endfunction
" Indent on save hook
autocmd BufWritePre <buffer> call Indent()
如果您希望所有文件类型在保存时自动缩进,我强烈建议您不要使用,请将此挂钩添加到
。vimrc

" Restore cursor position, window position, and last search after running a
" command.
function! Preserve(command)
  " Save the last search.
  let search = @/

  " Save the current cursor position.
  let cursor_position = getpos('.')

  " Save the current window position.
  normal! H
  let window_position = getpos('.')
  call setpos('.', cursor_position)

  " Execute the command.
  execute a:command

  " Restore the last search.
  let @/ = search

  " Restore the previous window position.
  call setpos('.', window_position)
  normal! zt

  " Restore the previous cursor position.
  call setpos('.', cursor_position)
endfunction

" Re-indent the whole buffer.
function! Indent()
  call Preserve('normal gg=G')
endfunction
" Indent on save hook
autocmd BufWritePre <buffer> call Indent()

任何其他文件类型也是如此,例如java的
~/.vim/after/ftplugin/java.vim
,等等。

要缩进已经存在的文件,可以使用快捷键
gg=G
(不是命令;只需按两次
G
,然后按
=
,然后按
Shift+G
),特别是因为您使用的是
文件类型缩进
。。。线路


在保存之前,请使用
设置自动缩进
并首先正确缩进代码,而不是使用触发器!如果脚本中没有bang,请不要使用
normal
gg=G
在本例中编写时不需要进行分解。除非您在每次插入后都倾向于保存文件,否则此命令很有用,因为它会将光标移回gg,使其不可用。@SimonBudin您可以使用
magg=G`a
,它将在“a”寄存器上创建一个标记,重新插入该文件,然后将您带回标记“a”@TylerFowle,这正是我想要的行为。谢谢你的建议。为什么你强烈建议不要在保存时自动缩进?@BallpointBen,我强烈建议不要在保存时对所有文件类型自动缩进,因为这很可能不是你所期望的。相反,在保存挂钩上只对专用文件类型进行自动缩进,如答案所示(例如,对于C++)。