zsh has a very handy "directory hash" feature that allows you to create directory shortcuts like so:
hash -d v=~/src/vim
hash -d vim=/usr/share/vim/vim92
And then you can use cd ~v or vim ~v/src/map.c etc. everywhere, similar to ~user.
This adds that feature in Vim:
# Create and list hashes
:dirhash v ~/src/vim
:dirhash vim ~/src/vim
:dirhash
vim /usr/share/vim/vim92
v ~/src/vim
# Works in expand(), expandcmd()
:echo expand('~vim')
/usr/share/vim/vim92
:echo expand('~vim/foo')
/usr/share/vim/vim92/foo
# And on CLI
:e ~v
:e ~v/src/map.c
# Remove dirhash
:undirhash vim
:dirhash
v ~/src/vim
I am not in love with the name :dirhash, but I can't really think of anything else at the moment. I considered :dirabbr and :dabbr, but it seems to me that this is significantly different enough from :abbr that it would be somewhat confusing. And keeping the same name zsh uses is probably a good idea?
None of the map*() functions support dirhashes. I would prefer to add new functions – that all of this uses the map hash table seems mostly an implementation detail rather than something that should be exposed.
https://github.com/vim/vim/pull/19554
(12 files)
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
@arp242 commented on this pull request.
> @@ -1423,7 +1424,31 @@ Expands to: ^[
[example given by Steve Kirkendall]
==============================================================================
-3. Local mappings and functions *script-local*
+3. Directory hashes *dirhash*
+
+A directory hash allows using any value as ~name at the start of the path. For
+example:
+>
+ :dirhash ~vim /usr/share/vim/vim92
+ :edit ~vim/syntax/vim.vim
+<
+These work everywhere |expandcmd()| is called.
+
+ *:dirhash*
+:dirh[ash] list all directory hashes.
+
+:dirh[ash] [<buffer>] {lhs} {rhs}
:dirh[ash] is a lie, and it's actually :dir[hash]. I'm not sure we want to abbreviate this to just :dir, since I can see :dir might be useful for something else in the future? I don't know if there's a way to prevent that?
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
@arp242 pushed 1 commit.
—
View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
How is this different from using arbitrary environment variables?
To me, this looks like a directory-specific environment variable, and the difference between the $ and ~ symbols seems trivial.
I'd like to know the clear advantage of using this dirhash instead of using environment variables.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
There's a bunch of things you can do better, such as tab completion (~vi<Tab> should complete to ~vim/, with trailing slash, like any other directory), and the statusline/tabline/etc. can show ~vim/defaults.viminstead of/usr/share/vim/vim92/defaults.vim`. That is not yet implemented though, but shouldn't be hard.
And it will never conflict with environment variables, which can have all sorts of special meanings to various programs. :dirhash cc ~/code/c will work as expected. let $CC = ~/code/c ... not so much. There's a few others with a huge potential for confusion and problems.
It's a lot more convenient if you're used to zsh. I don't know how many people actually use this feature though, but I keep using ~dir in Vim because of muscle memory from zsh.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Wouldn't it be more useful to add a way to expand environment variables inline? Could this be done with a plugin?
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Here's a prototype plugin by @mattn:
https://gist.github.com/mattn/f6b47de9c1f1b7074b036b8fce07167b
Neither he nor I have tested it yet, but if this approach works, I don't think there's any need to build the functionality into Vim itself.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
@zeertzjq commented on this pull request.
In src/ex_cmds.h:
> +EXCMD(CMD_undirhash, "undirhash", ex_dirhash, + EX_EXTRA|EX_TRLBAR|EX_NOTRLCOM|EX_CTRLV|EX_CMDWIN|EX_LOCK_OK, + ADDR_NONE),
This should come after :undo, otherwise it will break :u.
—
Reply to this email directly, view it on GitHub, or unsubscribe.
You are receiving this because you are subscribed to this thread.![]()
—
Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.![]()
Apart from the naming (I would think about :dirmark command), I think, that seems like a nice Vim plugin that does not necessarily belong to Vim core.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you commented.![]()
It's really hard to do this well in a plugin, because you need to not just implement something for the commandline, but also statusline, tabline, tabpanel, quickfix, etc. to get ~shortcut to show, and that tends conflict with other plugins. I tried this a few years ago and it's very "invasive" as you need to touch so many parts. Plus things like expand() and expandcmd() will work with ~foo/dir, so it will work with other plugins and such.
I probably didn't explain this well enough because it's so obvious for myself, but half the usefulness of this is to display paths much shorter everywhere, and not just make typing :e ~foo/file easier.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you commented.![]()
I leave this open for a bit, but I am not (yet) really convinced of the general usefulness of this.
—
Reply to this email directly, view it on GitHub.
You are receiving this because you commented.![]()