I'm trying to make my life easier by making a shortcut for the following command

:Ex /path/to/directory

I need to to be able to press :akeyword and execute the command above.

The directory needs to be specified with a variable. I want to access a folder under C:\Users\windowsusername\randomfolder; my problem is that the username varies between my computers. So I need $USERNAME to be used and not some fixed name.

I guess I must add something like the following, which doesn't work.

:command! :akeyword :Ex "C:\Users\" . $USERNAME "\randomfolder"

1 Answer 1


User commands must start with an uppercase character, so you could do something like this:

:command Akeyword execute 'Ex C:\Users' . $USERNAME . '\randomfolder'

The key here is to use :execute, which executes the result of an expression as a command.

However, you can shorten this to:

:command Akeyword Ex $HOME\randomfolder

Since Vim sets the $HOME variable on Windows, and the Ex command appears to expand the $HOME variable without needing to wrap it in an :execute expression.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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