3

I have created aliases in my .bashrc for various frequently used long paths but I can not seem to be able to use them inside vim or with commands such as find or grep.
E.g the following:
db
in cli does cd /some/very/long/path/db
But in vim this:
:e db/file.java
or on the command line: grep -r string db do not work.
How is this fixed?

4

You can set an environmental variable for that directory.

# Making the variable name consist entirely of capital letters makes the
# variable less likely to conflict with other variables in scripts. You can
# make the variable name consist of lowercase letters but you may run
# into problems. 
export DB=/some/very/long/path/db

Then you can use the exported variable in Vim as such:

:e $DB/file.java

and in your shell as such:

grep -r string $DB

The variable-substitution facilities of Vim and bash are entirely independent from each other. Vim just happens to substitute environmental variables in a manner similar to bash (and many other shells).

1

Aliases let you give a short name to a command and some arguments. They are only expanded where a command is expected. You can't use them to abbreviate the path to a file.

Furthermore .bashrc is only read by interactive shells, so nothing that you define there will be available in a command typed in Vim.

If you want to have a shortcut to a file or directory, create a symbolic link to it.

ln -s /some/very/long/path/db ~/db
grep -r string ~/db
vim ~/db/file.java

You can define environment variables to abbreviate any string. Environment variable definitions go into ~/.profile. Not every program expands them, but Vim does when you open a file with :e (among other things).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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