Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197843
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:09:36+00:00 2026-06-17T22:09:36+00:00

I have some commands that I’m using to compress a file on saving, and

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:09:37+00:00Added an answer on June 17, 2026 at 10:09 pm

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a post-build event that runs some commands for a c# project. The
I have a batch file with some commands that I need to run with
Possible Duplicate: In psql, why do some commands have no effect? I've just installed
I have some commands that use Ctrl-Win- as hotkeys. However, MenuItem displays these as
I have some commands which I am running using the subprocess module. I then
I have a script that executes some commands remotely, every time it runs it
I am using Wordpress and I want to have some PHP commands executed on
I have some embedded code that writes commands to a memory address to control
Just trying out ubuntu server on my pc and have been testing some commands
I have some a document that have commands of ESPON printers. now i have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.