[vim/vim] Add :dirhash to create directory shortcuts (PR #19554)

15 views
Skip to first unread message

Martin Tournoij

unread,
Mar 2, 2026, 7:24:18 PM (yesterday) Mar 2
to vim/vim, Subscribed

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.


You can view, comment on, or merge this pull request online at:

  https://github.com/vim/vim/pull/19554

Commit Summary

  • c9d707b Add :dirhash to create directory shortcuts

File Changes

(12 files)

Patch Links:


Reply to this email directly, view it on GitHub.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19554@github.com>

Martin Tournoij

unread,
Mar 2, 2026, 7:25:54 PM (yesterday) Mar 2
to vim/vim, Subscribed

@arp242 commented on this pull request.


In runtime/doc/map.txt:

> @@ -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.Message ID: <vim/vim/pull/19554/review/3879516922@github.com>

Martin Tournoij

unread,
Mar 2, 2026, 7:43:27 PM (24 hours ago) Mar 2
to vim/vim, Push

@arp242 pushed 1 commit.

  • 06f67e6 Add :dirhash to create directory shortcuts


View it on GitHub or unsubscribe.
You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19554/before/c9d707bf860bc57a232d456d0746a2f2f7e2ed4a/after/06f67e6e507e8c6251399a861b039cfac56a7bab@github.com>

Martin Tournoij

unread,
Mar 2, 2026, 8:14:48 PM (23 hours ago) Mar 2
to vim/vim, Push

@arp242 pushed 1 commit.

  • a5c596f Add :dirhash to create directory shortcuts

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19554/before/06f67e6e507e8c6251399a861b039cfac56a7bab/after/a5c596f86098764c99f7eaa79e807427683f3f2e@github.com>

MURAOKA Taro

unread,
Mar 2, 2026, 8:43:23 PM (23 hours ago) Mar 2
to vim/vim, Subscribed
koron left a comment (vim/vim#19554)

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.Message ID: <vim/vim/pull/19554/c3988070791@github.com>

Martin Tournoij

unread,
Mar 2, 2026, 9:01:24 PM (23 hours ago) Mar 2
to vim/vim, Subscribed
arp242 left a comment (vim/vim#19554)

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.Message ID: <vim/vim/pull/19554/c3988128981@github.com>

Martin Tournoij

unread,
Mar 2, 2026, 9:15:48 PM (22 hours ago) Mar 2
to vim/vim, Push

@arp242 pushed 1 commit.

  • f90aa55 Turns out macOS comes with an "unknown" user by default

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19554/before/a5c596f86098764c99f7eaa79e807427683f3f2e/after/f90aa5551586ecb814a763b9ad397e8f987393d9@github.com>

MURAOKA Taro

unread,
Mar 2, 2026, 9:23:02 PM (22 hours ago) Mar 2
to vim/vim, Subscribed
koron left a comment (vim/vim#19554)

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.Message ID: <vim/vim/pull/19554/c3988209482@github.com>

MURAOKA Taro

unread,
Mar 2, 2026, 9:59:43 PM (22 hours ago) Mar 2
to vim/vim, Subscribed
koron left a comment (vim/vim#19554)

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.Message ID: <vim/vim/pull/19554/c3988304460@github.com>

zeertzjq

unread,
Mar 2, 2026, 10:32:08 PM (21 hours ago) Mar 2
to vim/vim, Subscribed

@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.Message ID: <vim/vim/pull/19554/review/3880003434@github.com>

vim-dev ML

unread,
4:12 AM (15 hours ago) 4:12 AM
to vim/vim, vim-dev ML, Your activity
vim-ml left a comment (vim/vim#19554)
On Mon, Mar 02, 2026 at 04:24:07PM -0800, Martin Tournoij (Vim Github Repository) wrote:
> 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

you can let $vim=/usr/share/vim/vim92 so not only you can cd but the
variable will be exported to any subshell.

I really think vim is bloated enough.

regards.


Reply to this email directly, view it on GitHub.

You are receiving this because you are subscribed to this thread.Message ID: <vim/vim/pull/19554/c3989668001@github.com>

Christian Brabandt

unread,
7:48 AM (12 hours ago) 7:48 AM
to vim/vim, vim-dev ML, Comment
chrisbra left a comment (vim/vim#19554)

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.Message ID: <vim/vim/pull/19554/c3990791645@github.com>

Martin Tournoij

unread,
7:52 AM (12 hours ago) 7:52 AM
to vim/vim, vim-dev ML, Comment
arp242 left a comment (vim/vim#19554)

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.Message ID: <vim/vim/pull/19554/c3990813674@github.com>

Christian Brabandt

unread,
4:32 PM (3 hours ago) 4:32 PM
to vim/vim, vim-dev ML, Comment
chrisbra left a comment (vim/vim#19554)

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.Message ID: <vim/vim/pull/19554/c3993658031@github.com>

Reply all
Reply to author
Forward
0 new messages