lf icon indicating copy to clipboard operation
lf copied to clipboard

lf -remote and newline (new line, LF, \n, ...) character in filenames

Open dumblob opened this issue 3 years ago • 3 comments

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"

dumblob avatar Nov 14 '22 21:11 dumblob

Maybe escape the $ so the variable gets expanded on the other end?

p-ouellette avatar Nov 14 '22 21:11 p-ouellette

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?

dumblob avatar Nov 14 '22 22:11 dumblob

Nevermind, I didn't realize it's not a shell command.

p-ouellette avatar Nov 14 '22 22:11 p-ouellette

Hello @dumblob, the tips section mentions this specifically.

Because of the nature of lf -remote, in many of the examples below sed is 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 of sed in 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-escape and 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 sed commands 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.

CatsDeservePets avatar Nov 08 '25 00:11 CatsDeservePets