Skip to content

Fix [f and ]f behavior in directory containing [, $ #232

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 21, 2022

Conversation

ryym
Copy link
Contributor

@ryym ryym commented Nov 20, 2022

Currently s:entries() lists file paths using glob() without escaping filename, so it works incorrectly when a directory name contains a special character such as [ . This PR fixes that and make [f and ]f work for such directories.

Without this, glob() fails to list files and [f/]f works incorrectly when a path contains a special character such as "[".
@tpope
Copy link
Owner

tpope commented Nov 20, 2022

fnameescape() isn't the right tool here: It escapes a bunch of unnecessary characters, and every backslash it adds will be interpreted as a directory separator on Windows. Better is to replace [ with [[].

@ryym ryym changed the title Fix [f and ]f behavior in directory with special character Fix [f and ]f behavior in directory containing [ Nov 21, 2022
@ryym ryym force-pushed the filx-file-move-in-special-char-dir branch from 688c135 to 359ac0e Compare November 21, 2022 00:37
@ryym ryym changed the title Fix [f and ]f behavior in directory containing [ Fix [f and ]f behavior in directory containing [, $ Nov 21, 2022
@@ -77,6 +77,7 @@ call s:MapNextFamily('t', 't', 'trewind')

function! s:entries(path) abort
let path = substitute(a:path,'[\\/]$','','')
let path = substitute(path, '\([\|\$\)', '[\1]', 'g')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try substitute(path, '[[$*]', '[&]', 'g') to make it a bit cleaner. I threw * in there also for good measure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for quick advice. I updated the code and confirmed it works!
dd00e8a

see :help wildcards.
Escaping by adding a backslash does not work on Windows.
@ryym ryym force-pushed the filx-file-move-in-special-char-dir branch from 359ac0e to dd00e8a Compare November 21, 2022 00:56
@ryym
Copy link
Contributor Author

ryym commented Nov 21, 2022

fnameescape() isn't the right tool here: It escapes a bunch of unnecessary characters, and every backslash it adds will be interpreted as a directory separator on Windows.

Thank you for pointing it out. I didn't know that.
memo for myself: https://vimhelp.org/editing.txt.html#wildcards

I updated the code to escape [ and $ .
(This is because some web frameworks force us to name a directory with these characters like [id] or $id)

@tpope tpope merged commit 6d44a6d into tpope:master Nov 21, 2022
@ryym ryym deleted the filx-file-move-in-special-char-dir branch November 21, 2022 01:16
@ryym ryym restored the filx-file-move-in-special-char-dir branch November 21, 2022 01:16
@ryym ryym deleted the filx-file-move-in-special-char-dir branch November 21, 2022 01:16
@ryym ryym restored the filx-file-move-in-special-char-dir branch November 21, 2022 01:16
ryym added a commit to ryym/vim-unimpaired that referenced this pull request Nov 21, 2022
Without this, glob() fails to list files and [f/]f works incorrectly
when a path contains a special character such as "[".

Resolves: tpope#232
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants