TransWikia.com

Modify the path of a file be opened

Vi and Vim Asked by run_the_race on August 31, 2021

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!

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP