Terminal stops working on MacOs after opening gedit - macos

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

Related

shell: reuse backgrounded emacsclient windows, when invoking emacsclient

Is it possible to reuse backgrounded emacsclient windows, when invoking emacsclient?
Here's some background information (I mainly use emacs in terminal mode, not gui frames)
When the computer boots, an emacs daemon is started.
In the OS X terminal when I want to open a file, I do emacsclient /filename -nw
Now when I want to do bash stuff I press C-z to background emacs.
Now emacs appears in the jobs command. The fg command would also
make it re-appear.
But while I'm browsing around in bash, I see another file I want to open.
Now, how can I reuse that minimized emacsclient session with a single command?
Yes, put this in your .bashrc:
ec() {
kill %emacsclient 2> /dev/null
emacsclient -nw --eval '(find-file "'"$PWD/$1"'")'
}
Open files with ec file.txt. It's a bit hacky, but I think it will do what you want it to.
I open the file find-file so it'll stay open after you close the Emacs window (C-x C-c). Then when you open a new file, I kill the old Emacs window and open a new one. The effect is that the old file stays open forever, so it seems like you reused the Emacs window.

Sending signal to bash shell launched in GVim

How do I send a signal (say, SIGINT), to a shell launched inside GVim, using a keyboard shortcut?
Neither Ctrl+C nor Ctrl+D seem to work.
Ctrl+D seems to work for me, at least for closing a terminal session. Crl+C however does not. If you only want to kill a running process, you can do this workaround (provided Ctrl+Z) works for you.
Press Ctrl+Z to pause the process, then
kill %1
to kill the process in the background.
Inside vim, use command :shto temporarily exit vim and go to shell
If you want to go back to vim then press Ctrl + D. This work for me for years.

Cannot open multiple files with Emacs on Mac

I can open a single file with Emacs, no problem. But I'm used to Emacs on Linux, where typing emacs test.cpp & would open a new Emacs frame, leaving the terminal free. On OS X, however, when I try to open a new frame with emacs test.cpp &, the terminal shows a process was created but the window fails to pop up. If I push Ctrl+C, the terminal shows "Stopped", but I see the process is still running because I can see it in the task monitor.
How to solve this problem? Thanks!
The issue here is that the command emacs on Linux is generally just the Emacs binary itself, which opens a new frame (that's Emacs-speak for X11 or other GUI window) unless you specify -nw on the command line; in contrast, the emacs command provided by Homebrew is a shell script that calls /usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs -nw "$#" (substituting the version installed for 24.3). So the Homebrew version launches as a terminal app.
You can make a shell script that just runs /usr/local/Cellar/emacs/24.3/Emacs.app/Contents/MacOS/Emacs "$#", and it would behave somewhat as you expect, except that you would be launching a brand-new instance of the Mac app, which would cause an additional Emacs icon to appear on the Dock. Such a thing isn't considered "Mac-like," but it's not the end of the world.
An alternate solution, which is used a lot across different operating systems, is to make one Emacs process a server and then use emacsclient to open files from the command line. Emacsclient can open files in the current terminal, a new frame (GUI window), or an existing instance of Emacs. For an instance of Emacs to run as a server just requires that you run M-x server-start within it, or put (server-start) inside your init file (~/.emacs or ~/.emacs.d/init.el).
My Emacs config has this snippet, which starts server mode automatically when I launch the GUI app:
(when (display-graphic-p)
(server-start))
Then, once Emacs is running (you can make it autostart upon login in System Preferences > Users & Groups > Login Items), type emacsclient -nw test.cpp to open the file in the terminal, or emacsclient test.cpp & to open it in an existing frame, or emacsclient -c test.cpp & to open a new frame. (Note that if you open it in an existing frame, you use C-x # to close the buffer without closing the frame, as opposed to C-x C-c.)
Note that the terminal emacsclient command I gave just now didn't use & but the GUI ones did. & at the end of a command line puts the process in the background, meaning it's not monopolizing your terminal. For whatever reason (probably because it wouldn't be sensible to have a full-screen terminal app running in the background), when you invoke the terminal version of Emacs with &, it just suspends itself. The same thing would happen if you pressed C-z within Emacs. To get it back into the foreground, type fg (actually you can have multiple background processes, in which case fg would just pick the most recent one unless you specified a job specifier; see bash's man page (man bash) and search for JOB CONTROL if you're interested in the details).

Is there a way to revert back to the command line after opening an application through it?

I want to be able to open an application on the command line, but instead of switching to the application, I want to stay on my terminal emulator. Is there a way of accomplishing this? I am using OS X.
Use the -g flag of open, which avoids bringing the app to the foreground.
$ open -g /Applications/TextEdit.app
$
open will start the app, and then return to the command prompt.
After you run the program hit ctrl+z and type bg. You will return to your terminal CLI.
Whenever you want to go back to your program, just type fg.
You can background the job. In the Bash shell, this is done with the &. For example:
some_script_or_application &
Note that some daemons and processes background themselves. For example, on OS X, running open some.pdf will preview the PDF in a GUI while returning the command prompt immediately without needing to do anything special.
See the GNU Bash Manual for more on job control for background jobs.

Linux equivalent of the DOS "start" command?

I'm writing a ksh script and I have to run a executable at a separate Command Prompt window.
xdg-open is a similar command line app in linux.
see https://superuser.com/questions/38984/linux-equivalent-command-for-open-command-on-mac-windows for details on its use.
I believe you mean something like xterm -e your.sh &
Don't forget the final &
maybe it´s not a seperate window that gets started, but you can run some executables in background using "&"
e.g.
./myexecutable &
means your script will not wait until myexecutable has finished but goes on immediately. maybe this is what you are looking for.
regards
xdg-open is a good equivalent for the MS windows commandline start command:
xdg-open file
opens that file or url with its default application
xdg-open .
opens the currect folder in the default file manager
One of the most useful terminal session programs is screen.
screen -dmS title executable
You can list all your screen sessions by running
screen -ls
And you can connect to your created screen session (also allowing multiple simultaneous/synchronized sessions) by running
screen -x title
This will open up the emulated terminal in the current window where executable is running. You can detach a screen session by pressing C-a C-d, and can reattach as many times as you wish.
If you really want your program started in a new terminal window, you could do something like this:
xterm yourtextmodeprogram
or
gnome-terminal -e yourtextmodeprogram
or
konsole -e mc
Trouble is that you cannot count on a particular terminal emulator being installed, so (again: if you really want to do this) you would need to look for the common ones and then execute the first one encountered.
As Joachim mentioned: The normal way to do this is to background the command (read about shell job control somewhere, if you want to dig deeper).
There are also cases where you want to start a persistent shell, i.e. a shell session which lives on when you close the terminal window. There are two ways to do this:
batch-oriented: nohup command-to-run &
interactive: screen
if you want a new windows, just start a new instance of your terminal application: in kde it's
konsole -e whatever
i'm sure the Gnome terminal has similar options
Some have recommended starting it in the background with &, but beware that that will still send all console output from the application you launch to the terminal you launched it from. Additionally, if you close the initial terminal the program you loaded will end.
If you're using a desktop environment like KDE or GNOME, I'd check the alt+f2 launching apps (gnome-open is the one for GNOME, I don't know the name of the KDE app) and see if you can pass them the command to launch as an argument.
Also, if your intention is to launch a daemon, you should check the nohup documentation.
I used nohup as the following command and it works:
nohup <your command> &
then press enter and enter!
don't forget the last &
for example, I ran a python code listening to port 5000:
nohup python3 -W ignore mycode.py &
then I made sure of running by netstat -tulnp | grep :5000 and it was ok.

Resources