lf -remote and newline (new line, LF, \n, ...) character in filenames
Using lf -remote I need to send file names containing newline (\n aka linefeed/LF) characters:
lf -remote "send $id select $containsnewline"
It seems though that the socket protocol uses newline as message separator and thus there would need to be a way to signal there is a continuation of the previous message.
Any hints how to achieve that? I have tried escaping the newline using \ character but to no avail.
Same/similar issue occurs when sending it as if typed by user (i.e. with : in front):
lf -remote "send $id :select $containsnewline"
Maybe escape the $ so the variable gets expanded on the other end?
I have now exported it nad ran it like:
export containsnewline
lf -remote "send $id select \$containsnewline"
But lf understood $ literally and did no expansion. Actually this is the first time I hear lf can expand variables - is it some undocumented functionality?
Nevermind, I didn't realize it's not a shell command.
Hello @dumblob, the tips section mentions this specifically.
Because of the nature of
lf -remote, in many of the examples belowsedis used as a means of escaping special characters. This is necessary for arguments which contain single or double quotes, a backslash, or even a newline. If you find yourself with repeated uses ofsedin your configuration, consider using the following script instead:#!/bin/sh exec sed -z 's/\\/\\\\/g;s/"/\\"/g;s/\n/\\n/g;s/^/"/;s/$/"/'Name the script
lf-escapeand use it like so:lf -remote "send $id select $(printf '%s' "$file" | lf-escape)"This has the advantage (over the command used in many of the examples) of correctly escaping newlines and automatically adding quotes around the given argument. This was omitted from the
sedcommands below for the sake of brevity. If, for whatever reason, you have filenames containing newlines on your system (unlikely but possible), consider using the above script.
There is also a long-standing discussion about adding support for other delimiters:
- https://github.com/gokcehan/lf/issues/1114 Feel free to comment in there.
I will close this as a duplicate.