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
komo
komo3w ago
wdym? just open the file with nvim
mushrooms
mushroomsOP3w ago
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
komo
komo3w ago
seems like impossible without Termux adding functionality when opening files there's already that but it "downloads" the file into the Termux home dir
mushrooms
mushroomsOP3w ago
and you can't make it run a command on it at the same time?
komo
komo3w ago
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
mushrooms
mushroomsOP3w ago
using linux apps as defaults in android would be great (better way to word it I guess)
FZXO
FZXO3w ago
If it's a browser or a file manager there's always open with option
komo
komo3w ago
just use Acode i would say if it's a quick edit
mushrooms
mushroomsOP3w ago
but then what happens? cause there's no command attached to it might give that a try
FZXO
FZXO3w ago
This will show up
No description
mushrooms
mushroomsOP3w ago
ah ok, so save as rather than 'open'
Mlwls
Mlwls3w ago
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
ln -s ~/../usr/bin/vim ~/bin/termux-file-editor
ln -s ~/../usr/bin/vim ~/bin/termux-file-editor
hmm, is it by default always save to ~/download ? kinda useless isn't it?
mushrooms
mushroomsOP3w ago
ohh ok, from the screenshot i thought edit just changed the path will give that a try
Mlwls
Mlwls3w ago
i dont think it gonna be useful as an text editor cuz this it will not save the file to original location
mushrooms
mushroomsOP3w ago
ahh.. yeah that's problematic
mushrooms
mushroomsOP3w ago
what your saying checks out though, seems like i can handle file opening in termux with edit in the dialog
No description
komo
komo3w ago
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 this
komo
komo3w ago
let us know if it works
mushrooms
mushroomsOP3w ago
hmm, i'm probably missing something
No description
mushrooms
mushroomsOP3w ago
data/data is sus will keep trying a few things does not seem like i have a /home folder or /bin rather
Biswa96
Biswa963w ago
did you create the ~/bin directory.
mushrooms
mushroomsOP3w ago
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
Mlwls
Mlwls3w ago
on my phone it show that it need some kind of permission so that termux can show on top of other app.
mushrooms
mushroomsOP3w ago
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" "$@" ˋˋˋ
Mlwls
Mlwls3w ago
i dont think its a good idea
mushrooms
mushroomsOP3w ago
you were right, just had to enable termux 'Appear on top of apps'
Mlwls
Mlwls3w ago
cool
mushrooms
mushroomsOP3w ago
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
Mlwls
Mlwls3w ago
yup correct
Mlwls
Mlwls3w ago
Gist
termux-file-editor
termux-file-editor. GitHub Gist: instantly share code, notes, and snippets.
Mlwls
Mlwls3w ago
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.
mushrooms
mushroomsOP2w ago
finally got to testing it, i left the LD_PRELOAD line in the code, not sure if you keep it too?
Mlwls
Mlwls2w ago
LD_PRELOAD from where?
mushrooms
mushroomsOP2w ago
from the default termux-file-editor without I get this error, but i think i'm supposed to remove it
mushrooms
mushroomsOP2w ago
No description
Mlwls
Mlwls2w ago
weird, i got no error with no LD_PRELOAD but if it works for you its alright ig
mushrooms
mushroomsOP2w ago
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
Mlwls
Mlwls2w ago
might be cuz i am using this on android 10? i never tested it on newer android
mushrooms
mushroomsOP2w ago
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
Mlwls
Mlwls2w ago
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?
mushrooms
mushroomsOP2w ago
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
mushrooms
mushroomsOP2w ago
No description
mushrooms
mushroomsOP2w ago
the error gives the impression running nvim also runs your code cause the error is on a find?
mushrooms
mushroomsOP2w ago
No description
mushrooms
mushroomsOP2w ago
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
Mlwls
Mlwls2w ago
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
mushrooms
mushroomsOP2w ago
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)
mushrooms
mushroomsOP2w ago
No description
Mlwls
Mlwls2w ago
ok, maybe can be fixed by piping the error to /dev/null. but im not sure
mushrooms
mushroomsOP2w ago
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
Mlwls
Mlwls2w ago
seem to be fixed by piping stderr to null check it on gist
mushrooms
mushroomsOP2w ago
ohh i see what you meant now yup does seem to work, i didn't see any errors this time thanks for that

Did you find this page helpful?