code . & exit in cmd terminal doesn't work as expected - windows

I would like to open vscode in the cwd and exit the Windows cmd terminal from which the command is issued at the same time.
code . (with a . for cwd) launches vscode in the cwd. exit closes the terminal. But as issuing the first command makes the terminal loses focus, I need to give it back to it to issue the second command.
I would like to type a single line command that does both actions at once, thus without the terminal losing its focus.
code . & exit doesn't work as the terminal window remains open and hangs infinitely with a flashing cursor. Ctrl-C doesn't bring the cmd prompt back and clicking the upper right x is the only way to close the terminal window when in that state. (vscode opens correctly in the cwd thought.)
How to do so?

call code . & exit does the trick!

Related

Terminal stops working on MacOs after opening gedit

I have MacOs 10.14 and I installed gedit on my Mac via HomeBrew . When I open gedit typing :
gedit nameoffile
Terminal stops responding to commands.
The only thing I can do in order to use Terminal again is to close gedit and terminate the session of Terminal and reopen it.
Is there anything I can do to solve this ?
*** I managed to find a solution for not closing gedit when I terminate the session of Terminal : I have to open gedit with
sudo gedit nameoffile
However also in this way Terminal stops responding to any command ...
When you run a command from terminal, the command takes over. It's possible that after you close the gedit window, the process continues to run for some reason.
You can send a command to the background when starting it by adding & to the end:
gedit nameoffile &
This should instantly bring you back to the prompt when the process starts.
Alternatively, you could pause the process by hitting Ctrl-Z, then do a variety of things with that process like: kill it using kill, unpause the process and send it to the background using bg, bring it back to the foreground using fg, see list of running jobs with jobs and more.
You can read more about how terminal processes work here

How to activate command line? Only one line shown after I opened terminal

There is only the first line showing 'last login' here. Whatever I typed in the command line and pressed 'Enter', it doesn't execute...
I accidentally entered this mode and forget how to get back to the 'users-MacBook-Pro:~ username$' mode
BTW, my mac system is 10.10.5
Usually to exit any terminal based application control-c works to return to bash but you can also use control z to exit the current shell, but it doesn't terminate the program

Check if there are any running processes in the current tab of terminal

I have a script that opens up as many terminal tabs as are devices plugged in, then it runs tests on those devices. I would like to clean up all the terminal tabs after my tests are done. I run some things in the background, and I don't know when each process will be done.
How can I check if there are process running in the current tab of terminal?
I plan to do a Command W in AppleScript to kill each terminal command after each tab of terminal has no running processes.
Thanks!
If you use AppleScript, you can check the busy property:
tell application "Terminal"
repeat with t in tabs of windows
if busy of t is false then
do script "exit" in t
end if
end repeat
end tell
exit closes a tab if you set "Preferences > Settings > Shell > When the shell exits" to "Close the window".
One simple solution would be to take each command that you're running in a terminal and append "; exit" (Without the quotes) to it.
For example, if one of your commands was "ls", you would change it to "ls; exit".
Unfortunately, this doesn't work if you want to leave the terminal windows up to see results of what's being displayed. That can be solved by outputting the results of the first commands to some file, though.
Again using the example of ls, you could run "ls >> testfile.txt; exit" to output the results of ls to a file, and then have the terminal window close after it finishes executing.
You can use "jobs" to check if there are any processes running in the background.

how to close geany terminal

Whenever a program finishes to run when executing it with geany, the terminal doesn't close.
geany terminal waits for a keystroke to close. How do I change this behavior and make it close automatically when the program reaches it's end? Is there a script I can write?
This is not really an answer, but more like an alternative suggestion. In newer versions of Geany you can tell it to run programs in the Vitual Terminal Emulation, i.e. the terminal tab at the bottom (Settings -> Terminal -> Run Programs in VTE).
This way, the program starts more quickly (without first starting a terminal), you can still see the output generated by the program (exception stacktraces!), and the focus/cursor stays in the editor window, too.
I actually found a way of doing what you want to do in the terminal instead of using Geany's VTE. The perk of that is that you can run multiple scripts at the same time (or a single one multiple times), while the VTE only executes one script at the time (at least that's the case for me).
Just go to Build -> Set Build Commands. The last type of options says "Execute commands". The standard option is python "%f".
Just add && exit so that it reads
python "%f" && exit.
That way the terminal window will close instantly once it's finished if the programm doesn't give you an error code. If it does, the error message will be displayed and the window is kept open.
A variant of pohly's answer worked for me:
In Build -> Set Build Commands -> Execute commands
I replaced "./%e" by "./%e" && exit
better this way, let the terminal to stay for a moment so that we could see the results in case you want to
python "%f" && sleep 20 && exit

How do I get a Mac ".command" file to automatically quit after running a shell script?

In my shell script, my last lines are:
...
echo "$l" done
done
exit
I have Terminal preference set to "When the shell exits: Close the window". In all other cases, when I type "exit" or "logout", in Terminal, the window closes, but for this ".command" file (I can double-click on my shell script file, and the script runs), instead of closing the window, while the file's code says "exit", what shows on the screen is:
...
$l done
logout
[Process completed]
...and the window remains open. Does anyone know how to get a shell script to run, and then just automatically quit the Terminal window on completion?
Thanks!
I was finally able to track down an answer to this. Similar to cobbal's answer, it invokes AppleScript, but since it's the only window that I'd have open, and I want to run my script as a quick open-and-close operation, this more brutish approach, works great for me.
Within the ".command" script itself, "...add this line to your script at the end"
osascript -e 'tell application "Terminal" to quit' &
exit
SOURCE: http://forums.macosxhints.com/archive/index.php/t-2538.html
This worked perfectly for me.. it just closes that execution window leaving other terminal windows open
Just open Terminal and go to Terminal > Preferences > Settings > Shell: > When the shell exits: -> Close if the shell exited cleanly
Then just add exit; at the end of your file.
Use the 'Terminal > Preferences > Settings > Shell: > When the shell exits: -> Close if the shell exited cleanly' option mentioned above, but put
exit 0
as the last line of your command file. That ensures the script really does 'exit cleanly' - otherwise if the previous command doesn't return success then the window won't close.
Short of having to use the AppleScript solutions above, this is the only shell script solution that worked (exit didn't), even if abruptly, for me (tested in OS X 10.9):
...
echo "$l" done
done
killall Terminal
Of course this will kill all running Terminal instances, so if you were working on a Terminal window before launching the script, it will be terminated as well. Luckily, relaunching Terminal gets you to a "Restored" state but, nevertheless, this must be considered only for edge cases and not as a clean solution.
There is a setting for this in the Terminal application. Unfortunately, it is relative to all Terminal windows, not only those launched via .command file.
you could use some applescript hacking for this:
tell application "Terminal"
repeat with i from 1 to number of windows
if (number of (tabs of (item i of windows) whose tty is "/dev/ttys002")) is not 0 then
close item i of windows
exit repeat
end if
end repeat
end tell
replacing /dev/ttys002 with your tty
I'm using the following command in my script
quit -n terminal
Of course you have to have the terminal set to never prompt before closing.

Resources