Tip 41: Open a File by Its Filepath Using :edit
Return to the top: <<Practical Vim>>
Open a File Relative to the Current Working Directory
Open a File Relative to the Active File Directory
-
:edit %<Tab>
: reveal the absolute path of the active buffer-
%
: shorthand for the filepath of the active buffer
-
References
# If you open the file in vim, # then you can \"ayy@a the next line to execute the vim command: :h cmdline-special
-
:edit %:h<Tab>
: remove the filename while preserving the rest of the path
References
# If you open the file in vim, # then you can \"ayy@a the next line to execute the vim command: :h ::h
Easy Expansion of the Active File Directory
map %%
to %h:h<Tab>
cnoremap <expr> %% getcmdtype() == ':' ? expand('%:h').'/' : '%%'