1

We have rather complex file structure and I would like simplify opening file independently where am I in the filesystem at the moment.

Let's say we have a project_folder and several files in it:

  /project_folder/common-prefix/foo.txt
  /project_folder/common-prefix/bar.txt
  /project_folder/common-prefix/nested/zoo.txt

Usually I change to project_folder/common-prefix and work from it. Some command and documentation usually expose full relative project path, e.g. common-prefix/bar.txt

In this case I have to manually remove common-prefix to modify interested file. How can I configure vim so that opening a file it's file name would be processed and known prefixes would be removed/replaced?

$ cd /project_folder/common-prefix/
$ vim foo.txt # this works naturally
$ vim common-prefix/foo.txt # here I want to cut `common-prefix`
$ vim ../common-prefix/foo.txt # probably I want to convert to absolute path, but this is a corner case

I read about includeexpr, but it seems it olny works for gf commands, whereas I want something like BufRead/BufNewFile

I found similar questions:

And also two plugins, which probably close to the implementation I want:

3
  • It's unclear to me what you're trying to accomplish... What's wrong with opening /root_folder/foo.txt, that's a valid path after all... Is the issue the path attached to the buffer (when you use :ls to list buffers, or the path shown in the status line)? Does it help if you :cd to the directory where the file is inside Vim? Do you want to change the behavior for paths listed in the command-line arguments, or for using the :e and similar commands inside Vim, or both? Please be more specific about the actual issue you're having, with realistic examples if possible. Thanks!
    – filbranden
    May 22, 2021 at 2:25
  • Btw, please edit the question to add more details. (Don't just answer in the comments.) Thanks!
    – filbranden
    May 22, 2021 at 2:26
  • 1
    Yeah, sorry for messy explanation. I tried my best to make it clear. Most likely this is something trivial, but I work with vimscript very rarely, so it's hard to know where to start. Thank you.
    – grundic
    May 22, 2021 at 7:33

2 Answers 2

1

Try to play with BufNewFile:

augroup test_prefix | au!
    au BufNewFile common_prefix/* :e <afile>:t
augroup END

In the example above, if you are in ..../common_prefix/ directory and do vim text.txt or vim common_prefix/test.txt you will end up editing test.txt in the current directory.

3
  • Also worth checking if filereadable(<afile>) (possibly need expand()?), so you can edit an existing file normally.
    – D. Ben Knoble
    May 24, 2021 at 17:54
  • This doesn't work for netsted files :| common-prefix/nested/zoo.txt
    – grundic
    May 24, 2021 at 19:35
  • 'Nested' is not in the question. Try adding **/* instead of *.
    – Maxim Kim
    May 25, 2021 at 5:12
0

This is not an actual answer, more like an attempt. I saw something similar was done in vim-vp4 plugin:

func! s:openPrefixedFile(fname)
    echom "function is called with " . a:fname
    let l:oldBufname = bufname('%')
    let l:newFile = substitute(a:fname, "common-prefix/", "", "")
    echom "new filename is " . l:newFile
    echom "buf number is " . bufnr('%')

    execute 'bdelete! ' . l:oldBufname
    execute 'edit ' . l:newFile
    execute 'buffer ' . bufnr('%')
    execute 'doauto BufRead'
endfunc

augroup test_prefix | au!
    au BufNewFile common-prefix/* call s:openPrefixedFile(expand('%:p'))
augroup END

This work with single file, but doesn't support -O/-o options for splitting multiple files :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.