Adrian Gunawan
Adrian Gunawan

Reputation: 14479

vim shortcut to open a file with current directory populated

I'm trying to add a key mapping so that it opens up command and populate it with :e /path/to/current/file

I can get the current directory using :pwd but I'm having a trouble to use it in the mapping

I think it will be along the lines of setting pwd to a variable and use that variable as such:

noremap <C-q> <C-o>:e *pwdvariable*<Space>

Should I create a function to perform this?

Upvotes: 0

Views: 733

Answers (3)

ZyX
ZyX

Reputation: 53664

I guess you need

nnoremap <C-q> <C-\><C-n>:e <C-r>=fnameescape(expand('%:p:h'))<CR>

Upvotes: 1

jofel
jofel

Reputation: 3415

Not exactly, what you have asked for, but maybe more helpful: Vim tip 64: Set working directory to the current file: In short, add the following line to .vimrc:

 autocmd BufEnter * silent! lcd %:p:h

Interesting for you would be also Easy edit of files in the same directory.

Upvotes: 0

Andrew Marshall
Andrew Marshall

Reputation: 97004

%:p:h will get the full path of the current file (without trailing slash). Read more in :help filename-modifiers.

Upvotes: 1

Related Questions