2

我有一些命令用于在保存时压缩文件,并在保存后解压缩它(只是开头的缩进)。我遇到困难的一件事是我不希望将命令添加到历史记录中。命令和光标位置等都不是。

我想我要做的就是关闭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
4

2 回答 2

2

Vim 有四个操作历史的函数,:h history-functions给你一个列表和简短的描述。在这四个中,您需要的是:

histdel()

您可以在:h histdel().

于 2013-01-27T08:38:28.367 回答
2

操纵'viminfo'是一个太大的俱乐部,无法在这里挥舞。执行:autocmd的命令不会添加到命令历史记录中。

我唯一看到的是:substitute污染了搜索历史和当前的搜索模式。您可以通过将命令移动到 a 中来避免很多事情:function,并从 autocmd 调用它。见:help function-search-undo

在函数结束时唯一需要做的就是从历史记录中删除搜索模式:

:call histdel('search', -1)

编辑:要保持当前光标位置,请:substitute使用以下内容进行包装:

let l:save_view = winsaveview()
    %substitute/...
call winrestview(l:save_view)
于 2013-01-27T09:53:50.093 回答