我有一些命令用于在保存时压缩文件,并在保存后解压缩它(只是开头的缩进)。我遇到困难的一件事是我不希望将命令添加到历史记录中。命令和光标位置等都不是。
我想我要做的就是关闭viminfo
预写,然后在写后重新打开。但我似乎无法弄清楚。这是我正在使用的功能:
function! s:CompressIndent()
augroup CompressIndent
autocmd!
" Befor writing, change 4 spaces (and single tabs) into 2 spaces
autocmd BufWritePre * set viminfo="NONE" " Turn off 'history' before making the pre-write substitutions
autocmd BufWritePre * %substitute/^\( \+\)\1/\1/e " Halve the number of spaces of indentation
autocmd BufWritePre * set tabstop=2 " Make sure that tabs = 2 spaces before re-tabbing
autocmd BufWritePre * retab " Turn tabs into two spaces
" When opening a file (and after writing the file) turn 2 spaces into (and 4 tabs) into 4 spaces
autocmd BufReadPost,BufWritePost * set tabstop=4 " Make sure to display in 4 tabs
autocmd BufReadPost,BufWritePost * %substitute/^ \+/&&/e " Double the number of spaces of indentation on Reading and writing
autocmd BufReadPost,BufWritePost * set viminfo='20,\"200 " Turn back on history
augroup END
endfunction
我试过set viminfo="NONE"
和set viminfo=""
。两者似乎都没有效果。
任何帮助,将不胜感激!
谢谢!
编辑
这是我现在的位置,但我仍然没有完全让它工作(现在缩进被破坏了,但 histdel() 也不太工作。保存文件后,光标移动到一个全新的位置和撤消历史已被分支或奇怪的东西:
function! s:CompressIndent()
augroup CompressIndent
autocmd!
" Befor writing, change 4 spaces (and single tabs) into 2 spaces
autocmd BufWritePre * call s:SpaceSubstitution("2") " Halve the number of spaces of indentation
autocmd BufWritePre * set tabstop=2 " Make sure that tabs = 2 spaces before re-tabbing
autocmd BufWritePre * retab " Turn tabs into two spaces
" When opening a file (and after writing the file) turn 2 spaces into (and 4 tabs) into 4 spaces
autocmd BufReadPost,BufWritePost * set tabstop=4 " Make sure to display in 4 tabs
autocmd BufReadPost,BufWritePost * call s:SpaceSubstitution("4") " Double the number of spaces of indentation on Reading and writing
augroup END
endfunction
command! -n=0 -bar CompressIndent :call s:CompressIndent()
function! s:SpaceSubstitution(toSpaces)
if a:toSpaces == "2"
%substitute/^\( \+\)\1/\1/e
else
%substitute/^ \+/&&/e
endif
call histdel('search', -1)
endfunction