LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How do I open a plain text file in any Linux? (https://www.linuxquestions.org/questions/programming-9/how-do-i-open-a-plain-text-file-in-any-linux-4175716841/)

lucmove 09-16-2022 09:59 PM

How do I open a plain text file in any Linux?
 
I am not sure, but I believe in Windows you can run

Code:

> start SomeTextFile.txt
on a terminal and Windows will open the text file with whatever program is configured as the default one for opening .txt files.

Whether that is true or not, is there such a thing on Linux?

I remember there used to be such a command for KDE a very long time ago (like 15 years ago), but I don't know if it is still used and, anyway, I need some command that does not rely on KDE. It just has to assume the user has a graphical environment. Is that possible?

TIA

wpeckham 09-16-2022 10:40 PM

Wow! there are SO many ways to open a text file!
Do you mean to read, or to edit?
If the file is names "file.txt" one could display it with
Code:

cat file.txt
or
Code:

less file.txt
or
Code:

more file.txt
or
Code:

view file.txt
or possibly a dozen or more additional options.

One might open it for editing with
Code:

vi file.txt
or
Code:

vim file.txt
or
Code:

nano file.txt
or
Code:

kate file.txt
or
Code:

joe file.txt
or
Code:

pico file.txt
or any of many other options depending upon what text editors you might have installed.

If you have a file manager installed, you can open that (generally it is an Xwindows program dso you open it in your gui desktop interface from the menu system) and click on the txt file and it will might in your default text editor - however you have that set at the time.


What is it you really want to do?

michaelk 09-16-2022 10:43 PM

Should be possible. I don't know if any such command that exists but fairly easy to create a script i.e. find the mime type via the find command and then find what application is associated with that type by searching the mimeapps.list file.

https://wiki.archlinux.org/title/XDG_MIME_Applications

Emerson 09-16-2022 11:16 PM

You can set the $EDITOR variable in Linux, technically you can name this variable whatever you want, just make sure you are not interfering with existing variables.

jmgibson1981 09-16-2022 11:23 PM

Can also do alias's. I don't use them but many do. Then you can type in whatever you want.

https://vitux.com/how-to-create-alia...nds-in-debian/

example

Code:

start='nano'
or any other command you can pass.

Code:

runthisnow='apt update && apt full-upgrade -y'

Michael Uplawski 09-16-2022 11:29 PM

GUI, Folks!

If there is a taskbar with a kind of “start”-button, the “utilities” should comprise some text-editor.
Alternatively there should be a “run” function, although there is no obligation, as far as I know: Usually you hit Alt+F2 and name the program or command-line that you wish to start.
Examples are listed above in this thread, but the command-line editors need to be preceded by the call to a terminal application, like:
Code:

/usr/bin/lxterminal -e "vim file.txt"
Compared to your Windows-command that you state to use “in a terminal application”, the procedure is identical on Linux. Open a terminal and use one of the calls in post #2, above.

Turbocapitalist 09-17-2022 04:57 AM

Quote:

Originally Posted by lucmove (Post 6380706)
I remember there used to be such a command for KDE a very long time ago (like 15 years ago), but I don't know if it is still used and, anyway, I need some command that does not rely on KDE. It just has to assume the user has a graphical environment. Is that possible?

Many distros will check a file and then launch the appropriate graphical program if you run xdg-open with the file name as the first option.

Code:

xdg-open somefile
Otherwise, you'll have to know or guess whether to use geany, kate, gedit, mousepad, leafpad, etc. and use one of them by name. I have a script which is on many different desktops, each with a different distro, and it does just that.

michaelk 09-17-2022 05:19 AM

Completely forgot about xdg-open. It uses the mime type to determine what application to use.

grail 09-17-2022 05:49 AM

As a senior member this seems a bit odd, however:

https://googlethatforyou.com?q=How%2...any%20Linux%3F

rtmistler 09-17-2022 08:02 AM

Quote:

Originally Posted by lucmove (Post 6380706)
I am not sure, but I believe in Windows you can run

Code:

> start SomeTextFile.txt
on a terminal and Windows will open the text file with whatever program is configured as the default one for opening .txt files.

Whether that is true or not, is there such a thing on Linux?

I remember there used to be such a command for KDE a very long time ago (like 15 years ago), but I don't know if it is still used and, anyway, I need some command that does not rely on KDE. It just has to assume the user has a graphical environment. Is that possible?

TIA

I do not believe this is specifically true. Using start in the Windows command prompt should attempt to run a program and unless the file is a script or executable, this will not work.

Linux:
Well many have answered. Do we misunderstand your question? Because you obviously know Linux. wpeckham very much covered it.

And you do not require a graphical environment.

boughtonp 09-17-2022 09:03 AM

Quote:

Originally Posted by rtmistler (Post 6380770)
I do not believe this is specifically true. Using start in the Windows command prompt should attempt to run a program and unless the file is a script or executable, this will not work.

Nope, it will work:
Quote:

Originally Posted by https://learn.microsoft.com/en-us/previous-versions/windows/it-pro/windows-server-2008-R2-and-2008/cc770297(v=ws.10)
You can run nonexecutable files through their file association by typing the name of the file as a command.


Quote:

Originally Posted by rtmistler (Post 6380770)
Well many have answered. Do we misunderstand your question? Because you obviously know Linux. wpeckham very much covered it.

Probably because of the poorly worded title, many misunderstood what was being asked.

I'm pretty sure Turbocapitalist answered the actual question - i.e: xdg-open is the equivalent to the above behaviour.

(A search for "linux open file in default application" contains many results saying the same thing...)


lucmove 09-17-2022 09:28 AM

xdg-open is just what the doctor ordered. Thank you.

The only question now is whether it is available in a large enough number of installations.

I think I will embed a basic text editor in my application. I guess it's the only foolproof way.

wpeckham 09-17-2022 10:38 AM

If those are solutions, then mimetype and mimeopen might also be useful!

As mentioned, once you find the command you want to use one could alias "start" to be an alias for that command.

EdGr 09-17-2022 01:01 PM

Quote:

Originally Posted by lucmove (Post 6380782)
I think I will embed a basic text editor in my application. I guess it's the only foolproof way.

If this is a GTK application, you can use the built-in editor, GtkTextView.
Ed

dugan 09-17-2022 02:49 PM

Just going to add that the OSX equivalent to "start" and "xdg-open" is "open".


All times are GMT -5. The time now is 11:29 AM.