简体   繁体   中英

(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. 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="" . 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:

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. Of the four, the one that you need is:

histdel()

which you can read about in :h histdel() .

Manipulating 'viminfo' is too big a club to wield here. Commands that are executed by :autocmd aren't added to the command history.

The only thing I see is that :substitute pollutes the search history and the current search pattern. You can avoid a lot by moving the command into a :function , and invoke that from the autocmd. See :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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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