2

It needs to support completion.

An example use case would be: I'm on a given project (so a certain cwd) but need to update one of my Vim files. If they are already opened so navigate buffers or navigate them using MRU.

But often if I need another Vim file I used type mc to switch the cwd then :e filename. Then need to switch back.

It is also confusing because git folder changes when this happens.

Vivian De Smedt
  • 16,336
  • 3
  • 18
  • 37
eyal karni
  • 1,106
  • 9
  • 33

3 Answers3

4

This all is implemented long time ago by builtin command :h :find used along with option :h 'path'.

Namely:

set path=.
find FILENAME

which could be made a command.

eyal karni
  • 1,106
  • 9
  • 33
Matt
  • 20,685
  • 1
  • 11
  • 24
1

The advantage here is that it filters only on files (not directories). Also, it is a prepared command.

:com! -nargs=1 -bang -complete=customlist,EditFileComplete
        \ EditFile exec "edit<bang> ". expand("%:p:h")."/<args>"
:fun! EditFileComplete(A,L,P)
:    return map(filter(split(glob(expand("%:p:h").'/*'.(len(a:A)>1 ? a:A . "*" : '')), "\n"),'filewritable(v:val) != 2' ),'fnamemodify(v:val, ":t")' )
:endfun

eyal karni
  • 1,106
  • 9
  • 33
1

My solution doesn't involve tweaking path or a custom command (which isn't as scalable to other forms of :edit, like :split): I use a command-line mapping on %% to insert the directory. It goes like this:

cnoremap <expr> %% bk#filename#command_dir('%%')

where bk#filename#command_dir is an autoloaded function:


" Get the directory of a file
" - On Ex command lines, returns the directory of the file ('./' for new files)
" - On other command lines (/,?) returns the keymap used to trigger it
function! bk#filename#command_dir(keymap) abort
  let l:command_type = getcmdtype()
  if l:command_type isnot# ':'
    return a:keymap
  endif
  let l:dir = expand('%:h')
  if empty(l:dir)
    let l:dir = '.'
  endif
  return l:dir . (has('win64') || has('win32') || has('win32unix') ? '\' : '/')
endfunction

Now I type :edit %%file, :vsplit %%, or other variants when I need to (actually I have mappings that insert that automatically).

D. Ben Knoble
  • 26,070
  • 3
  • 29
  • 65
  • 1
    I like it. I have made it os independent because the / didnt bode well in windows. – eyal karni Dec 30 '23 at 19:19
  • 1
    @eyalkarni I've edited your edit to align better with my preferred style. Note that has('win16') is documented as always false, so I've also changed the condition a bit. – D. Ben Knoble Jan 01 '24 at 15:49