yasar
yasar

Reputation: 13738

Adding current filename to a Vim shortcut

I am new to Vim and trying to add a new shortcut. I was wondering how I can put current file's path to the command dynamically. So that every time I use that shortcut, my command will executed with correct filepath in it.

Upvotes: 0

Views: 645

Answers (2)

HaskellElephant
HaskellElephant

Reputation: 9891

What you are looking for is expand("%"). It will return the file you are currently editing. If you use expand("%:p") you will get the full path of that file. So say you would want to have a shortcut to print your current file in the command bar and you wanted it mapped to F5. Then you would add the following in your .vimrc:

map <F5> :echo expand("%:p")<CR>

Upvotes: 1

evan
evan

Reputation: 12535

nnoremap <expr> <leader>cd (expand("%:p:h") !~ '^/tmp') ? ":lcd %:p:h\<CR>:echo expand(\"%:p:h\")\<CR>" : "echo \"foo\"\<CR>"

I have this line in my .vimrc to what you want. My leader is ',' so when I type ,cd it changes the local directory to that of the current file.

Upvotes: 0

Related Questions