2

I am trying to write a function that runs before a new file is created, or an existing file is opened. What I would like to do is edit the path to the file which is being opened depending on the path.

At the very beginning of my journey to solve above is find the autocmds that will fire.

From :h autocmd-events it says that one of the following 4 autocmds are run whenever a file is created/opened: BufNewFile,BufReadPre,FilterReadPre,FileReadPre.

For READING FILES there are four kinds of events possible:
  BufNewFile                      starting to edit a non-existent file
  BufReadPre      BufReadPost     starting to edit an existing file
  FilterReadPre   FilterReadPost  read the temp file with filter output
  FileReadPre     FileReadPost    any other file read Vim uses only one of
these four kinds when reading a file.  The "Pre" and "Post" events are
both triggered, before and after reading the file.

However I never see any of the following messages echo'ed:

augroup my_au_test_group
    " Clear existing autocmds for this group
    autocmd!
    autocmd BufNewFile    * :echom "......BufNewFile   "
    autocmd BufReadPre    * :echom "......BufReadPre   "
    autocmd FilterReadPre * :echom "......FilterReadPre"
    autocmd FileReadPre   * :echom "......FileReadPre  "
    echom "Auto commands set"
augroup END

I can see the message "Auto commands set" to verify that the file was sourced correctly.

Then run :new and or open a file, and I don't see any of the messages printed. I have checked the history with ":message". I also tried as the help suggests using set verbose=9, but it never printed out anything related to these autocommands.

Having looked at other SO posts, they mostly are to do with certain file types, there are normally making mistakes with certain file types, however I would like to run a function on all filetypes.

Bonus Help

And then to change the path of the file being loaded, I thought I could pass <afile> into the function, check the path, if its okay, do nothing, if the path needs to be changed then I am unsure how to do either of the follwoing:

  1. Cancel the current path from opening (so I can just exe ":e ".new_path) Maybe feedkeys a <C-C> (CTRL+C)?...or...
  2. Tell Vim the path of the file its loading has changed.

If anyone has a hint what to look for the above 2 approaches that will be greately appreciated!

4
  • Just to be sure are you using Vim or Neovim? I ask because Neovim seems to be bugged when it comes to using echom in some auto-commands. (And some people say vim when it's really nvim). Don't want to make things more complicated than they have to be.
    – B Layer
    Commented Jul 15, 2020 at 13:51
  • 1
    @BLayer Yes I am using Neovim, I just tested in actual Vim, and the BufPreWrite worked, however the BufNewFile still was not fired with :new. Will raise an issue on Github Neovim, and think about how to reword the question. Thanks! Commented Jul 15, 2020 at 15:04
  • @Blayer Turns out its not buggy, it's in the Neovim FAQ Here By default Neovim has shortmess+=F, which turns off messages during file operations. So I added shortmess-=F and now its working same as original Vim. Commented Jul 16, 2020 at 7:55
  • Interesting. Thanks for the update. Even though it's not technically a bug I'd argue it should be treated as one (whether its roots lie in nvim or vim). A typical user can't reasonably be expected to make sense of current situation on their own, IMO. The number of issues opened suggests same. Cheers.
    – B Layer
    Commented Jul 16, 2020 at 10:44

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.