简体   繁体   English

(g)Vim-在运行时禁用历史记录/ viminfo

[英](g)Vim - disable history/viminfo at runtime

I have some commands that I'm using to compress a file on saving, and post save uncompress it (just the indentation at the beginning). 我有一些命令在保存时用于压缩文件,然后在保存后解压缩(只是开头的缩进)。 The one thing I'm having a hard time with is that I don't want the commands to be added to the history. 我很难过的一件事是,我不想将命令添加到历史记录中。 Neither the commands nor the cursor position, etc. 既不是命令也不是光标位置等。

I thought what I would have to do is to turn off the viminfo on pre-write and then turn it back on on the post-write. 我以为我需要做的是在预写时关闭viminfo ,然后在写后将其重新打开。 But I can't seem to figure it out. 但我似乎无法弄清楚。 Here is the function I'm working with: 这是我正在使用的功能:

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

I've tried set viminfo="NONE" and set viminfo="" . 我试过set viminfo="NONE"set viminfo="" Neither seemed to have an effect. 似乎都没有影响。

Any help would be appreciated! 任何帮助,将不胜感激!

Thanks! 谢谢!

EDIT 编辑

Here's where I am now with this, but I'm still not quite getting it to work (now the indentation is broken, but the histdel() doesn't quite work either. After saving the file, the cursor moves to a completely new position and the undo history has been branched or something weird: 这是我现在使用的位置,但是我仍然无法完全使用它(现在缩进已损坏,但是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

Vim has four functions for manipulating the history, :h history-functions gives you a list and short description. Vim有四个操作历史:h history-functions:h history-functions为您提供列表和简短描述。 Of the four, the one that you need is: 在这四个中,您需要的是:

histdel()

which you can read about in :h histdel() . 您可以在:h histdel()阅读有关内容。

Manipulating 'viminfo' is too big a club to wield here. 操作'viminfo'太大了,无法在这里使用。 Commands that are executed by :autocmd aren't added to the command history. :autocmd执行的命令不会添加到命令历史记录中。

The only thing I see is that :substitute pollutes the search history and the current search pattern. 我唯一看到的是:substitute污染了搜索历史和当前搜索模式。 You can avoid a lot by moving the command into a :function , and invoke that from the autocmd. 您可以通过将命令移至:function并从autocmd调用该命令来避免很多事情。 See :help function-search-undo . 参见:help function-search-undo

The only thing you then need to do at the end of the function is remove the search pattern from the history: 然后,该函数结尾处唯一需要做的就是从历史记录中删除搜索模式:

:call histdel('search', -1)

Edit: To keep the current cursor position, wrap your :substitute with the following: 编辑:要保留当前光标位置,请用以下代码包装:substitute

let l:save_view = winsaveview()
    %substitute/...
call winrestview(l:save_view)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM