I hope you all had a good weekend and Easter. Today’s post is going to be a small follow on to Saturday’s post on editing your Bash profile. One of the comments gave me an idea of allowing you to open the file in TextEdit and edit the file from there, rather than trying to use Terminals built in file editor. I am going to expand on this and show you how to open a file from Terminal so you can edit in a different app. This is useful since you don’t have to fight with Terminal’s text editors, especially if you have limited knowledge of the workings or don’t like to use them.
Opening files is very simple. Within Terminal the function to open a file is called
open. In guessing for why they picked that name. The Terminal program can be used in various different ways, for example the following:
Will open the file in TextEdit. If you want to apply this to the file in the previous post you can write:
Here you can edit the file in TextEdit, which makes life a lot easier.
You can also use the open program on a whole load of other file types. The following will work perfectly well, the last one will open a Finder window to a location instead of a file in an app.
Each one will open the file, and URL’s, in the default app as supplied by your operating system. If you want to change which app it opens with you can modify how the app works. This is done with the following command.
open -a "/Applications/TextMate.app" text.txt
The “-a” command says “use this program instead”. Make sure when you list the file path you give the .app file extension (which is always present, just hidden) otherwise you will get an error.
If you want to open the program in the background use the -g option as such:
open -g text.txt
Finally, instead of opening the file you can show it in Finder. This only works if you can already see the file, if the file is hidden (i.e starts with a dot) you just get the Finder window.
open -R /path/to/file/file.txt
Overall the program is very useful, and I will begin to use it to edit files. I haven’t tested this however I have a feeling if you add “sudo” to the front of the command you will be able to edit files that you would normally need a password for. Try it out and see what happens. If you want more information on this command simply type “man open” into Terminal, there is a couple of other bits of information, the important bits are listed here.
If you have any questions or comments please leave them below.