Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

saving on TextChanged event breaks vim-repeat #59

Closed
samuelallan72 opened this issue Mar 13, 2017 · 12 comments
Closed

saving on TextChanged event breaks vim-repeat #59

samuelallan72 opened this issue Mar 13, 2017 · 12 comments

Comments

@samuelallan72
Copy link

Minimal vimrc displaying this behaviour:

set nocompatible
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-surround'
call plug#end()
augroup save
  au!
  au TextChanged ?* silent! wa
augroup END

An example of what happens:

  1. perform ysiw) to surround a word with parentheses.
  2. press . to repeat the surround on another word
  3. the cursor appears at the bottom of the screen, waiting for the character to surround the word with (as if you did ysiw
  4. pressing ) completes the surround as expected.

I haven't read the source of vim-repeat, so not sure exactly why this might be happening. The help for TextChanged says that it's fired when b:changedtick is changed, so I'm guessing it saves the file halfway through the initial command, which makes only part of the commend registered with vim-repeat somehow...

@bluz71
Copy link

bluz71 commented Jun 23, 2017

Today I decided to figure out why does vim-repeat not work for me. Yes, basically due to this.

My autocmd is similar (a Vim hack auto-save):

augroup autoSave
    autocmd!
    autocmd TextChanged,InsertLeave,FocusLost * silent! wall
augroup END

In the meantime recording a macro is the way to go to do vim-surround repeats for those who do the above style TextChange tricks: qq then cs"' then q then lastly Q (a mapping of mine) to run the macro.

Many thanks.

@samuelallan72
Copy link
Author

samuelallan72 commented Jun 23, 2017

This reminds me - I should add that this only breaks with tpope/vim-surround and tpope/vim-repeat together. - ie. saving on textchanged breaks repeating vim-surround actions.

Interestingly, vim-sandwich works fine with vim-repeat and saving on textchanged. Minimal vimrc that works fine:

set nocompatible
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-repeat'
Plug 'machakann/vim-sandwich'
call plug#end()

" for vim-surround like mappings
runtime macros/sandwich/keymap/surround.vim

augroup vimrc
  autocmd!
  autocmd TextChanged ?* silent! wa
augroup END

Maybe this issue should be raised on vim-surround's issues instead?

@ironhouzi
Copy link

ironhouzi commented Jul 2, 2018

I should add that this only breaks with tpope/vim-surround and tpope/vim-repeat together.

@swalladge this is not entirely correct. vim-repeat is also needed for other plugins developed by @tpope. vim-commentary comes to mind, but probably others as well.

@samuelallan72
Copy link
Author

vim-repeat is also needed for other plugins developed by @tpope. vim-commentary comes to mind, but probably others as well.

@ironhouzi yes it's needed for many plugins, but it doesn't break with all plugins that use it. That's the reason why I'm not sure if the issue is in vim-repeat, or the issue is with how other plugins use it.

@tpope
Copy link
Owner

tpope commented Jul 2, 2018

FYI The HEAD version of commentary.vim no longer requires repeat.vim, which might be the source of some confusion. If you're looking for another plugin to test against, try [e and [<Space> in unimpaired.vim.

@bluz71
Copy link

bluz71 commented Jul 3, 2018

Worth noting, the vim-auto-save plugin does work with vim-repeat.

So if your only need to use the TextChanged event was to initiate auto-saving (as I was) then I suggest just using the vim-auto-save plugin instead. Somehow it avoids the clash with vim-repeat that this snippet had.

These are the settings I use:

Plug '907th/vim-auto-save'
let g:auto_save        = 1
let g:auto_save_silent = 1
let g:auto_save_events = ["InsertLeave", "TextChanged", "FocusLost"]

Thanks to @tpope for all your contributions.

@samuelallan72
Copy link
Author

samuelallan72 commented Jul 3, 2018

@bluz71 great find! I believe I have found a lead based on this. vim-auto-save uses nested (reference) in the autocmd call to recurse autocmds when calling :w or :e from inside the autocmd. :h autocmd-nested for more info. Modifying my original minimal vimrc to include nested in the autocmd fixes the original issue:

set nocompatible
call plug#begin('~/.vim/plugged')
Plug 'tpope/vim-repeat'
Plug 'tpope/vim-surround'
call plug#end()
augroup save
  au!
  au TextChanged ?* nested silent! wa
augroup END

Now it remains to be seen why this works...

@samuelallan72
Copy link
Author

Update: vim-repeat relies on the BufWrite* events in

autocmd BufLeave,BufWritePre,BufReadPre * let g:repeat_tick = (g:repeat_tick == b:changedtick || g:repeat_tick == 0) ? 0 : -1
. If the file is saved from inside an autocmd that isn't nested, those events will not be fired and g:repeat_tick will not be updated. It sounds like something similar to #63 where b:changedtick is suddenly a value that vim-repeat isn't expecting.

@MahbubAlam231
Copy link

Is there a way to make vim-repeat work along with TextChanged and TextChangedI?

@samuelallan72
Copy link
Author

@MahbubAlam231 see my previous comment for a work around (add nested to the autocmd).

@MahbubAlam231
Copy link

MahbubAlam231 commented Jul 8, 2018

Thanks @swalladge , it works. I do not quite understand vim script yet, I read :h autocmd-nested, didn't get what's going on, can you please explain what is this nested thing and why it works?

nested doesn't work with TextChangedI though, the cursor shifts abruptly to left while inserting text.

@tpope
Copy link
Owner

tpope commented Jul 8, 2018

Okay, makes sense, the old autocommand was broken and adding nested fixes it. I don't think this is a repeat.vim bug.

@tpope tpope closed this as completed Jul 8, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants