Tuesday, February 24, 2015

A More Betterer Autosave In Vim

Run :version in your local Vim. What do you see?

Here are the first two lines I get:

:version
VIM - Vi IMproved 7.4 (2013 Aug 10, compiled Feb 24 2015 14:04:47)

That 7.4 is the key thing.

I actually wrote in old time (December 2013, as I recall; this was on an older site) about a cool new(-ish) autocommand event, or rather a pair of events, which someone sent a patch to the vim_dev list about—they were not mentioned in the help file. I don’t know when the events were added, but in Vim 7.4, the help file situation was remedied, and now we have this as the help for TextChanged and TextChangedI:

                                                        TextChanged
TextChanged            After a change was made to the text in the
                       current buffer in Normal mode.  That is when
                       b:changedtick has changed.
                       Not triggered when there is typeahead or when
                       an operator is pending.
                       Careful: This is triggered very often, don't
                       do anything that the user does not expect or
                       that is slow.
                                                        TextChangedI
TextChangedI           After a change was made to the text in the
                       current buffer in Insert mode.
                       Not triggered when the popup menu is visible.
                       Otherwise the same as TextChanged.



I’ve (since writing that) upgraded to OS X Yosemite, and was back on its system Vim until today, in fact. (Its system Vim is a 7.3.) So I’d forgotten about these events, and was not even using an auto-saving trick for some time (until my last post, as it turns out). Then “Unknown” reminded me of the events (albeit indirectly) with a comment on that last post, “Save time in Vim NOW with these five weird tricks”, which mentioned an auto-saving autocommand as a bonus (sixth) trick:

Sadly auto-save a file when you leave insert mode doesn't do anything for edits made in normal mode (dd to delete a line etc)

(And yes, as you can see, I just upgraded to 7.4 again today. Yay Homebrew!)

I hereby, therefore, give you a new and improved (or at least improved) autocommand, Unknown! Here it is:

autocmd InsertLeave,TextChanged * if expand('%') != '' | update | endif

As the help text says, TextChanged watches for changes to the text (hmm—what a surprise!) when we’re in normal mode, which is where we do things such as dd, p, c, etc.; InsertLeave only notices that we’ve left insert mode (and thus lets us save all of the changes we made there, which is very nice of it). So the combined effect of the two events in this improved autocommand should be to take care of All Your Autosaving Needs (TM).

The one thing to keep in mind with this version is that, well, it saves after basically every change to the buffer. This means that after every accidental command or mis-pasted text, Vim will write the file. You can always undo it right afterwards, of course, if you notice it, but still. (Plus, as the help implies, this will cause our file to be saved quite often—normally we would consider this a Win, but if you’re the kind of Vimmist that’s doing 382,000,000,000 editing operations a second, who knows.)

So, I’m going to go with something like at the end of a recipe when they say “Salt to taste.” Try it out, and maybe it’ll prove massively helpful. (I’m trying it out again now myself.) Or, your taste might run more toward the old InsertLeave one (or even to not using an autosave at all—I would not understand that point of view myself, but to own his each).

50 comments:

  1. There are lots of information about latest technology and how to get trained in them, like UNIX Training in Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies(UNIX Certification). By the way you are running a great blog. Thanks for sharing this.


    UNIX Training in Chennai | UNIX Certification

    ReplyDelete
    Replies
    1. I am technology Enthusiast. Your blog is really awesome, attractive and impressive. I like the way you think. it is very useful for Java SE & Java EE Learners. Your article adds best knowledge to our Java Online Training from India. or learn thru Java Online Training from India Students. or learn thru JavaScript Online Training from India. Appreciating the persistence you put into your blog and detailed information you provide. Kindly keep blogging.

      Delete
  2. There are lots of information about latest technology and how to get trained in them, like Best Hadoop Training In Chennai have spread around the web, but this is a unique one according to me. The strategy you have updated here will make me to get trained in future technologies Hadoop Training in Chennai By the way you are running a great blog. Thanks for sharing this blogs..

    ReplyDelete
  3. I found some useful information in your blog, it was awesome to read, thanks for sharing this great content to my vision, keep sharing..
    SalesForce Training in Chennai

    ReplyDelete
  4. Pretty article! I found some useful information in your blog, it was awesome to read,thanks for sharing this great content to my vision, keep sharing..
    Unix Training In Chennai

    ReplyDelete
  5. This information is impressive..I am inspired with your post writing style & how continuously you describe this topic. After reading your post,thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic
    Android Training In Chennai In Chennai

    ReplyDelete
  6. I have read your blog and i got a very useful and knowledgeable information from your blog.You have done a great job.
    SAP Training in Chennai

    ReplyDelete
  7. Oracle Training in chennai
    Thanks for sharing such a great information..Its really nice and informative..

    ReplyDelete
  8. Selenium Training in Chennai
    Wonderful blog.. Thanks for sharing informative blog.. its very useful to me..

    ReplyDelete
  9. Data warehousing Training in Chennai
    I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly..

    ReplyDelete
  10. Whatever we gathered information from the blogs, we should implement that in practically then only we can understand that exact thing clearly, but it’s no need to do it, because you have explained the concepts very well. It was crystal clear, keep sharing..
    Websphere Training in Chennai

    ReplyDelete
  11. Oracle DBA Training in Chennai
    Thanks for sharing this informative blog. I did Oracle DBA Certification in Greens Technology at Adyar. This is really useful for me to make a bright career..

    ReplyDelete
  12. Nice blog.This information useful for learners.UNIX ONLINE TRAINING

    ReplyDelete
  13. Thank you for sharing such a usefull information on your blog, I am inspired with your post writing style & how continuously you describe this topic.

    Hadoop Online Training | Qlikview Online Training | Tableau Online Training | SAS Online Training | Android Online Training | Business Analyst Online Training

    ReplyDelete
  14. It tries to saves an edited command in the command line window (invoked with `q:`)

    I wrote the following:

    function! autosave()
    let name = expand('%')
    if name != '' && name != '[Command Line]'
    update
    endif
    endfunction
    autocmd InsertLeave,TextChanged * :call autosave()

    ReplyDelete
  15. This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..
    Selenium Training in Chennai | QTP Training in Chennai

    ReplyDelete
  16. Thanks for Information Oracle Apps Technical is a collection of a bunch of collected applications like accounts payables, purchasing, inventory, accounts receivables, human resources, order management, general ledger and fixed assets, etc which have its own functionality for serving the business
    Oracle Apps Training In Chennai

    ReplyDelete
  17. Oracle Training in chennai | Oracle D2K Training In chennai
    This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post, thanks for taking the time to discuss this, I feel happy about it and I love learning more about this topic..

    ReplyDelete
  18. Garcinia Cambogia, a tropical fruit also known as the Malabar tamarind, is a popular weight-loss supplement. Find Garcinia Cambogia Supplement review at http://www.dietreviewmonster.com/

    ReplyDelete
  19. Thanks for sharing such useful information. I think this is really a very nice post.
    crazy bulk is one of the best selling legal steroids which help in gaining strength, muscle,visit here : http://www.buycrazybulks.com/

    ReplyDelete
  20. Now you can get more muscle and mass in no time try crazy bulk. You can also check crazy bulk reviews here.

    ReplyDelete
  21. crazy bulk is one of the best selling legal steroids which help in gaining strength, muscle,visit here : http://www.buycrazybulks.com/

    ReplyDelete
  22. PhenQ Reviews - Most Powerful Weight Loss Pills. PhenQ is the newest weight loss pill that has been proven by legitimate and smart science. http://phenq-reviews.com/

    ReplyDelete
  23. Crazy Bulk Australia 100% Safe and Legal Steroids

    Crazy Bulk is 100% safe and legal alternate steroids available in market right now. With 100% success rate and excellent muscle building effects from day 1

    ReplyDelete
  24. Whatever bodybuilding requirements you have, you will be able to work on it and build it up with the various gym routines you will find waiting for you. And, even if you are a girl then there will be specialized bodybuilding programs for you too. JayJenkins

    ReplyDelete
  25. sizegenetics We are the only penis enlargement device to show you REAL customer testimonials. Why? Because SizeGenetics™ really works! Learn more.

    ReplyDelete
  26. male extra pills are famous on the market. But you have to know some side effects and results. My prof review will help you.

    ReplyDelete
  27. CIIT Noida provides Best MCA Courses in Noida based on the current

    IT industry standards that help students to get high paying jobs in Top MNCs. CIIT provides Best MCA Training in Noida, Greater Noida, and Ghaziabad.

    CIIT is one of the trusted MCA training institutes in Noida providing practical knowledge and 100% job assistance with basic as well as advanced

    level MCA subjects. CIITN is the best MCA college in Noida, greater noida, ghaziabad, delhi, gurgaon regoin.

    At CIIT MCA classes in Noida is conducted by subject experts corporate professionals with 9+ years of experience in managing real-time and live

    projects. Sofracle Nano Specialized MCA classes Noida is the perfect blend of academic learning and practical sessions to provide maximum exposure to

    students that transform an average student into a corporate professional whom companies prefer to hire.

    Best MCA College in Noida

    ReplyDelete
  28. This is a good post. This post give truly quality information. I’m definitely going to look into it. Really very useful tips are provided here. thank you so much. Keep up the good works.
    java training in chennai | java training in bangalore

    java online training | java training in pune

    java training in chennai | java training in bangalore

    java training in tambaram | java training in velachery

    ReplyDelete


  29. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.


    rpa training in Chennai | rpa training in velachery

    rpa training in tambaram | rpa training in sholinganallur

    rpa training in Chennai | rpa training in pune

    rpa online training | rpa training in bangalore

    ReplyDelete
  30. It is amazing and wonderful to visit your site.Thanks for sharing this information,this is useful to me...
    python online training
    python training in OMR
    python training in tambaram

    ReplyDelete
  31. Hi, Great.. Tutorial is just awesome..It is really helpful for a newbie like me.. I am a regular follower of your blog. Really very informative post you shared here. Kindly keep blogging.
    Devops training in sholinganallur
    Devops training in velachery

    ReplyDelete
  32. Hmm, it seems like your site ate my first comment (it was extremely long) so I guess I’ll just sum it up what I had written and say, I’m thoroughly enjoying your blog. I as well as an aspiring blog writer, but I’m still new to the whole thing. Do you have any recommendations for newbie blog writers? I’d appreciate it.

    Best Selenium Training in Chennai | Selenium Training Institute in Chennai | Besant Technologies

    Selenium Training in Bangalore | Best Selenium Training in Bangalore

    AWS Training in Bangalore | Amazon Web Services Training in Bangalore

    ReplyDelete
  33. This is the best explanation I have seen so far on the web. I was looking for a simple yet informative about this topic finally your site helped me allot.
    Selenium Training in Chennai
    German Classes in Chennai
    French Classes in Chennai
    Android Training in Chennai
    Qtp training in Chennai
    web designing course in chennai

    ReplyDelete
  34. Your good knowledge and kindness in playing with all the pieces were very useful. I don’t know what I would have done if I had not encountered such a step like this.
    safety course in chennai

    ReplyDelete
  35. I am obliged to you for sharing this piece of information here and updating us with your resourceful guidance. Hope this might benefit many learners. Keep sharing this gainful articles and continue updating us.
    Software Testing Training Institute Chennai
    Testing Training Chennai
    Selenium training Chennai
    Selenium Training in Chennai
    Selenium Training
    Selenium Course in Chennai




    ReplyDelete
  36. Superb. I really enjoyed very much with this article here. Really it is an amazing article I had ever read. I hope it will help a lot for all. Thank you so much for this amazing posts and please keep update like this excellent article.thank you for sharing such a great blog with us.
    best institute for big data in chennai
    big data classes in chennai
    Hadoop Admin Training in Chennai
    CCNA Training in Chennai
    CCNA course in Chennai
    CCNA Training institute in Chennai

    ReplyDelete