Open TXT Files by Default with Termux (nvim).
Is there a way I can expose some a script as an app outside of termux, so that, for example, my termux script (nvim) would show as valid ways to open a .txt file?
53 Replies
wdym?
just open the file with
nvim
yeah it can be done within termux
but i'd like my text files (when tapped) to open by default in termux
not sure if there's a way to do this though
seems like impossible without Termux adding functionality when opening files
there's already that but it "downloads" the file into the Termux home dir
and you can't make it run a command on it at the same time?
yeah afaik
if you want to open a file using neovim, best guess is to navigate to the directory where the file reside and run nvim or just don't use it at all
using linux apps as defaults in android would be great
(better way to word it I guess)
If it's a browser or a file manager there's always open with option
just use Acode i would say if it's a quick edit
but then what happens?
cause there's no command attached to it
might give that a try
This will show up

ah ok, so save as rather than 'open'
in my case, i use zarchiver to open txt file, it will give a list of app to open it with, then choose termux, choose edit, first time you do this a instruction will show up on how to set it up.
in my case, i just
hmm, is it by default always save to ~/download ?
kinda useless isn't it?
ohh ok, from the screenshot i thought edit just changed the path
will give that a try
i dont think it gonna be useful as an text editor cuz this
it will not save the file to original location
ahh.. yeah that's problematic
what your saying checks out though, seems like i can handle file opening in termux with edit in the dialog

oh yeah i remember
yes you can create a file in
~/bin/termux-file-editor
to edit file with preferred editor
-# why did i forgot about thislet us know if it works
hmm, i'm probably missing something

data/data is sus will keep trying a few things
does not seem like i have a /home folder
or /bin rather
did you create the
~/bin
directory.nope, not used to having to do that in linux, but that makes sense, will do
symlink got created, now termux just closes if i press 'edit' in the dialog instead of showing the red error screenshot i had before
hmm, seems like it opened in nvim as a i was typing this
yeah behavior is pretty weird, it closes the window but seems to open in termux too
i just gotta go open the termux manually
on my phone it show that it need some kind of permission so that termux can show on top of other app.
hmm i'll check if I have that enabled but i think so
not sure but i recall a command for termux to request permissions
maybe i can modify this handle to open termux instead?
ˋˋˋ
#!/data/data/com.termux/files/usr/bin/env sh
LD_PRELOAD="$LD_PRELOAD:/data/data/com.termux/files/usr/lib/libluajit.so" exec "/data/data/com.termux/files/usr/libexec/nvim" "$@"
ˋˋˋ
i dont think its a good idea
you were right, just had to enable termux 'Appear on top of apps'
cool
in android app settings
but yeah the issue with all of this remains that if you save, it wont modify the initial file, it saves to /downloads in termux
yup correct
workaround for this issue
not perfect
like when files having same name and same content but on different location, it may edit the wrong file location.
finally got to testing it, i left the LD_PRELOAD line in the code, not sure if you keep it too?
LD_PRELOAD from where?
from the default termux-file-editor
without I get this error, but i think i'm supposed to remove it

weird, i got no error with no LD_PRELOAD
but if it works for you its alright ig
the error is probably due to the code, not missing LD_PRELOAD
i think with LD_PRELOAD it just skips the code
since the file opens
before the code runs
might be cuz i am using this on android 10?
i never tested it on newer android
oh yeah i'm prolly 11-12+
rip
also something seems off when i launch nvim, will see if i can figure it out
yeah just running the nvim command gives me that error now
well i clueless too, i don't have newer android phone so can't even troubleshoot it myself.
may i look at your current termux-file-editor file?
sure, tbh i've been struggling to paste stuff outside of termux
but right now my nvim is broken for some reason so that might just be it
your code might work, just not sure how nvim stopped working out of nowhere

the error gives the impression running nvim also runs your code cause the error is on a find?

yeah removing your code and uncomenting the line with LD_PRELOAD fixes nvim for me, but i'm not sure why termux-file-editor affects my nvim installation
probably something about how termux works i would guess
i think you use symbolic link? so you actually editing $PREFIX/usr/bin/nvim instead of ~/bin/termux-file-editor?
you should delete ~/bin/termux-file-editor, remove my code on $PREFIX/usr/bin/nvim, then make a new termux-file-editor file with my code in it.
and chmod +x it
right...
I guess I was editing nvim itself
since it was a symlink
will give that a try
okay so i still get the find permission denied error you can see in the previous screenshot
BUT it does fully work, and saves the edit to the proper file
(outside of termux, in my case it's in my android download folder)

ok, maybe can be fixed by piping the error to /dev/null.
but im not sure
yeah not sure why there's an error on find if it works though, does that mean the find logic is not needed?
all that said though it doesn't matter too much, cause it works
seem to be fixed by piping stderr to null
check it on gist
ohh i see what you meant now
yup does seem to work, i didn't see any errors this time
thanks for that