bash open .html file not in browser but with $editor - macos

I want to open all of my html/css/js etc. not in the Browser by typing open example.html, but with my default Editor. I did create the alias EDITOR already, but it does not work with every kind of file.
How to fix that?

Depending on the actual behavior you're looking for, you will probably want one of the following, from the open man page:
-e Causes the file to be opened with /Applications/TextEdit
-t Causes the file to be opened with the default text editor, as deter-
mined via LaunchServices
-f Reads input from standard input and opens the results in the default
text editor. End input by sending EOF character (type Control-D).
Also useful for piping output to open and having it open in the
default text editor.
-W Causes open to wait until the applications it opens (or that were
already open) have exited. Use with the -n flag to allow open to
function as an appropriate app for the $EDITOR environment variable.

Try reading the man page: man open
I see three options:
-a
-a application
Specifies the application to use for opening the file
open -a SomeEditor file.html
-e
Causes the file to be opened with /Applications/TextEdit
open -e file.html
-t
Causes the file to be opened with the default text editor, as determined via LaunchServices
open -t file.html

If you use TextMate or Sublime Text, you can use mate or subl. You can create aliases like this for other editors:
alias wrang="open -a TextWrangler"
open -t opens files in the default application for public.plain-text files. You can change it by adding a line like this to a duti configuration file:
com.macromates.TextMate.preview public.plain-text all

Related

Katacoda Scenarios not recognizing open command in the terminal

I am learning to create my own scenario in katacoda. I want to open a file that I created in the katacoda editor, but it's not letting me use the open command in my background.sh file. This is what I have in it so far:
touch my-project/new.py
echo "print('Hello World')" >> my-project/new.py
open new.py
This creates the python file, but it does not open it. I tried running open new.py in the terminal, but it gives me an error
bash: open: command not found
I can click on it in the file tree to open it, but I want it to open automatically. What other command can I use to open it in the katacoda editor?
After messaging support, you are not able to put code in a file that is opened on start in Katacoda. You can embed links that will add code when the user clicks on it, but that's a different task. Katacoda does not support the "open" command, but you can use vim or nano.

Opening Finder from terminal with file selected

I want to open Finder from the terminal with a specific file selected. I know that by using open . I can open the current directory in Finder, but I also want to select some file in the Finder window.
The basic thing I want to do is run a script that randomly selects a file among many in a folder and for that I need to open a new Finder window with the file selected.
The . in your open . command just means path at current location (which would be a folder) so open decides that the correct application to use is Finder. If you were to do open myTextFile.txt which is at your current location in the terminal open will decide to use a text editor instead. You can however specify the application to open the file with by using the -a flag so your command would look like this: open -a Finder myTextFile.txt.
What Faisal suggested will also work, the -R flag is an equivalent to using ⌘↩ (Command Return) in Spotlight.
this and some other nice shell tricks with the open command are described in this post: Shell tricks: the OS X open command
For me, code below works fine.
open -R your-file-path
You can do it like that
osascript -e "tell application \"Finder\"" -e activate -e "reveal POSIX file \"<your file path>\"" -e end tell

open file with particular application where spaces in application name

I want to open ex1.py in the terminal. If I just go:
open ex1.py the file opens in textwrangler.
But I want to use sublime. So I tried this:
open -a SublimeText ex1.py
Unable to find application named 'SublimeText'
open -a Sublime Text 2 ex1.p
The files /Users/macuser/Documents/pyleo/Text and /Users/macuser/Documents/pyleo/2 do not exist.
When I control click sublime > show in finder the name is "Sublime Text 2". Presumably the spaces are causing an issue.
How can I open ex1.py in Sublime text 2 using the terminal?
Wrap the application name in single quotes:
open -a 'Sublime Text 2' ex1.py

Open files with VIM in tabs instead of windows

I have searched and found threads related to this, but not exactly the same case.
I use VIM in Windows7. It's set as the default editor for text files.
The problem is when I click in some text file in order to open it, it is opened in a new VIM window no matter if I already have one or more VIM windows opened.
How to change this behavior?
Thanks!
UPDATE:
This program solved my problem: http://defaultprogramseditor.com/
You should manually set up your file associations in Windows to launch Vim with a custom command. If you're an administrator and don't mind messing with EVERYBODY's config, you can use the ftype and assoc commands in the cmd.exe shell. Otherwise, you can manually create registry entries in HKEY_CURRENT_USER\Software\Classes.
The registry method actually sets up the same structure that you would set up automatically with ftype and assoc. First you must create a file type like "sourcecode" under that Classes key, with shell\open\command key having the exact command you want to open Vim with, using %1 in place of the file name to pass in. Then create a association key like .c with a value of the filetype you created, in this case "sourcecode".
Some details on file associations here: http://vim.wikia.com/wiki/Windows_file_associations
Specifically for setting up "launch in tabs" behavior, you want --remote-tab-silent, as done here: http://vim.wikia.com/wiki/Launch_files_in_new_tabs_under_Windows
vim has a "remote-server" concept, which allows you to open files in single/certain vim instance.
vim's man page (help doc too: :h remote) has the explanation. in short:
vim --serverlist #list names of all found vim servers
vim --servername {name} #current instance becomes as server named {name}
vim --servername {name} --remote file #open file in {name} vim-server
You can still send keys to the remote vim, when the buffer load by --remote-send or with command ... --remote +{cmd} file
For your requirement, you want to open files in a vim server's tab page, you could:
vim --servername FOO --remote-tab yourfile

What's the difference between 'open -a TextMate.app' and 'mate'?

I've thought that mate is virtually the same as 'open -a TextMate.app', but I guess I'm wrong in this.
As when I run the following command, when there's no hello2.txt, I get this error.
open -a TextMate.app hello2.txt
The file /Users/smcho/hello2.txt does not exist.
But, it's OK to run mate.
mate hello.txt --> opens the text mate.
What's the difference between the two?
I even tried
open -a TextMate.app --args hello2.txt
But this time, TextMate run with the file name 'Untitled', not 'hello2.txt'.
And this code opens the 'hello3.txt' without any problem.
[NSTask launchedTaskWithLaunchPath:#"/Applications/TextMate.app/Contents/MacOS/TextMate" arguments:[NSArray arrayWithObjects:#"hello3.txt", nil]];
open will open the given file with the default or a specific application.
open -a TextMate.app hello2.txt
means "Open the file hello2.txt using the application TextMate.app".
If there is no hello2.txt, there's nothing open could open, with or without TextMate.app, hence the error.
open -a TextMate.app --args hello2.txt
means "open nothing specific in the application TextMate.app (i.e. only open TextMate.app) and pass 'hello2.txt' as additional argument". This is a different kind of argument than the first example. TextMate.app can decide what to do with that additional argument. Apparently it chooses to ignore it.
mate is a utility optionally installed by TextMate.
mate hello.txt
means "I'd like to edit a file called hello.txt in TextMate", which is exactly what TextMate will let you do. It's a different utility with different behavior and different purpose, and it seems to better suite what you want it to do.

Resources