Welcome to Vifm Q&A, where you can ask questions about using Vifm. Registration is optional, anonymous posts are moderated. E-mail and GitHub logins are enabled.
0 votes
in vifm by
edited by

May I suggest adding a sanitation command that would replace all spaces and various OS's reserved characters from file names and directory names?

Relevant: https://kb.acronis.com/content/39790

2 Answers

0 votes
by

Not sure if there is a need for that, :tr should be able to do the job. Something like:

tr/ ?<>\\:*|"/_

but I hit unnoticed bug while testing it (thanks for that!), so the command should be like this in current version:

tr/ ?<>\\:*|"/__________

You can define a command:

command! sanitize :tr/ ?<>\\:*|"/__________
by

xaizek, what happens when renaming in a text editor a file name that contains a line break?

by

It won't work. Displaying such names is not really nice too. Luckily, it's very rare case, because it's also the one which is hard to handle in various situations.

0 votes
by

That kind of a tool can be useful,
especially when copying between file systems (like copying to FAT or NTFS on GNU/Linux), and the name would have to be corrupted anyway.
but as can be seen from your acronis.com link, what to replace is OS-, user- and file-dependent. Maybe if it shows a menu where you can customize your defaults?

Some GNU/Linux examples:

  • “-” in the beginning of a name is a must to get rid of, because they are dangerous with sloppy command lines.
  • I guess / < > and line breaks must not be in file names.
  • “:” is not a problem, except with multimedia software like FFmpeg, unless you simply prepend “file:” in its command.
  • Spaces are not always a problem, but are inconvenient and can break poorly-written scripts or command lines. Same with quotation marks (and asterisks) and parentheses (likely to be in names of pages and their directories saved by a Web browser). They must not be removed by default.
  • “|” also comes from Web browsers. Not sure there is any other use for it in file names.
  • Double spaces can be removed for polish.
by
edited by

@xaizek


version of VIFM

:version

Version: 0.12


command! sanitize :tr/ ?<>\\:*|"/__________ works well and I use it often

I have added two symbols to your function

Hex ff1a (not a colon) - looks likes a colon : Hex 3a

and

Hex 29f8 (not a slash) - looks likes a slash / Hex 2f

so the final command is in vifmrc:

command! sanitize :tr/ ?<>'\\:*|"⧸:/__________


I noticed that after using the function I had double hyphens in the folder or file name

eg:

I tried this with no result

Do you have an idea how to resolve it. Thx for your time. Great things from Munich

by
edited by

Mind that you've commented on the wrong answer. :tr can't be used to collapse underscores, need :substitute:

command! sanitize :tr/ ?<>\\:*|"⧸:/_/|s/_{2,}/_/g

This won't work with selection though. This should work with selection starting with v0.12.1:

ommand! sanitize :execute 'keepsel tr/ ?<>\\:*|"/_/' | s/_{2,}/_/g

Speaking of a dedicated tool for this (this answer), there is at least https://github.com/emrakyz/sanitize_fs.

by

amazing thx

command! sanitize :tr/ ?<>\:*|"⧸://|s/{2,}/_/g

working very well !

by
edited by

this is an update after testing

1) the sanitize command below, allow to change the name of file or directorie - one by one.

I've tried to select many files and run it - It's only change the first item.

2) all the letters below are substitued except ü - I don't know why...

command! sanitize :tr/ ?<>\\,:*|"'⧸:/_/|s/À/A/g|s/à/a/g|s/Â/A/g|s/â/a/g|s/Û/U/g|s/û/u/g|s/Ê/E/g|s/ê/e/g|s/È/E/g|s/è/e/g|s/É/E/g|s/é/e/g|s/Ä/AE/g|s/Ö/OE/g|s/Ü/UE/g|s/ä/ae/g|s/ö/oe/g|s/ü/ue/g|s/&/and/g|s/_-_/_/g|s/_-/_/g|s/-_/_/g|s/_{2,}/_/g

All is working except ü

for example I have the file name

äöüabcüöä

it becomes

aeoe abc oeae

the ü is ignored, I think it's a VIFM bug. I've tested two hours. With uppercase it's running

by

Mind that

s/À/A/g|s/à/a/g

is equivalent to

tr/Àà/Aa/

So you can simplify your command a great deal.

The problem with ü is that it and other umlauts can be encoded in at least two different ways, making the outcome depend on the initial file name. To address this Vifm would have to normalize the name and arguments of :tr and :s before processing. This will make things work but also result in file names changing in ways which aren't visible to the user. Not sure if it's makes a practical difference though, most don't even realize that Unicode allows such non-sense to occur.

by

yes @xaizek it can simplify, I understand how "Tr" works now. it's nice

I realized during a test that if I have two sanitizing functions like

command! sanitize0 :tr/ ?<>\\,:*|"'⧸:/_____________/|s/_{2,}/_/g

command! sanitize1 :s/É/E/g

and I use the second one - after restarting VIFM - to rename the file

Émile.txt

I have the error

Ambiguous use of user-defined command

with other name

command! sanitizeA :tr/ ?<>\\,:*|"'⧸:/_____________/|s/_{2,}/_/g

command! sanitizeB :s/É/E/g

I was able to rename Émile.txt to Emile.txt

by
edited by

Ambiguous use of user-defined command

This does look like a bug, thanks.

by
edited by

Here is a shell command that undoes some of those changes recursively (it's for yt-dlp downloads).

find -D exec . \( -name "*"*" -or -name "*:*" -or -name "***" -or -name "*?*" \) -execdir rename -E 's/:/:/g' -E 's/"/"/g' -E 's/*/*/g' -E 's/?/?/g' '{}' \+

by

Ambiguous use of user-defined command

Fixed in v0.14.1.

If you would like to make a bug report or feature request consider using GitHub, SourceForge or e-mail. Posting such things here is acceptable, but this is not a perfect place for them.
...