How do I close a GNOME terminal by giving a command in another terminal in linux? - gnome-terminal

I have opened a gnome terminal as a new different window terminal using the gnome command in Linux. Now what I want is that I want to close only that gnome terminal by giving a certain command in the other terminal.
I had tried a command as 'kill PID', but with that both the terminals are closed.
Can anyone help me with how to close only the gnome terminal by writing command in the other terminal??

Use "xdotool" to send alt+F4 to the target window. Replace zeros with your windowID:
xdotool windowactivate --sync 00000000 key --clearmodifiers alt+F4
You can use "xdotool search" to get the windowID. Insert between quotes the terminal title:
xdotool search --name "text_to_search"
Joining all commands:
xdotool windowactivate --sync $(xdotool search --name "text_to_search") key --clearmodifiers alt+F4
If you need, add "--delay 100" to get a better performance.
To install "xdotool" type:
sudo apt-get install xdotool

Related

How to choose a default tab focus in gnome-terminal?

I have a bash script which opens several tabs inside one window and I want to choose one tabe to be the default focus
for example the below script opens 3 tabs on one window:
gnome-terminal --tab --geometry="100x20" --title="TAB1" -- bash -ic "command1"
gnome-terminal --tab --geometry="100x20" --title="TAB2" -- bash -ic "command2"
gnome-terminal --tab --geometry="100x20" --title="TAB3" -- bash -ic "command3"
So, let's say I want tab2 to be the focus after running the script and openning the window. Is there a way to specify that from the script?
I highly recommend installing terminator, it's a program which offers flexible management of multiple running gnome terminals. It allows you to choose a tab as the default focus while still viewing any other amount of terminals on the same view. It can be installed via:
$ sudo apt install terminator

Linux: Gedit won't run in foreground

If I run gedit in the Linux terminal ($ gedit) it opens as a background job; which is not what I'm after at the moment.
Other programs (such as emacs) run in the foreground with commands like ($ emacs) and only run in the background if I've specified it via something like ($ emacs &).
I've tried searching for a solution but almost everything is about the opposite (trying/struggling to get things to run in the background).
Any ideas?
As it works for me: I run gedit /tmp/file.txt for the first time and it runs in the foreground. Then I open another terminal tab and run gedit /tmp/file2.txt while Gedit is still open — the second command instructs running instance of Gedit to open second tab and exits immediately. Gedit is still in the foreground in the first terminal tab.
According to gedit help, it has an option
-w, --wait Open files and block process until files are closed
If in the second terminal tab I run gedit --wait /tmp/file3.txt, then it opens a new tab in the existing Gedit window but the command stays in the foreground until I close that file tab.
Just in case: it was tested under KDE, Ubuntu 16.04, gedit version is 3.18.3

Using of gnome terminal to get new tab

I'm new to scripting.
I’m using the below code in my project, but the terminal is getting closed instantly. I want the hold the terminal and keep running
#!/bin/bash/
gnome-terminal --tab --working-directory="/home/sandhya/OpenBTS/public/openbts/trunk/apps/" -e "file = grep OpenBTS /home/sandhya/OpenBTS/public/openbts/trunk/apps \
if [ -f $file ] \
then \
echo " file exits" \
sudo "/home/sandhya/OpenBTS/public/openbts/trunk/apps/OpenBTS" \
fi"
I want the terminal to be stay and keep running the application.
Please correct me in case the method or syntax I used is wrong.
In gnome-terminal,
--go to Edit -> Profiles.
--Double click on Default profile
--Click on Title And Command tab.
--Select Hold Terminal Opens from when command exits list which located at bottom of window.
--close windows
Now run your script it will work
Or you can also add your profile and run script as follows
Click the New tab. and give your profile name
Select Hold the terminal from the drop-down menu labelled When command exits. You should create a new profile for that and execute with
gnome-terminal --window-with-profile=NAMEOFTHEPROFILE -e command
To make the terminal stay when the command exits:
In konsole there is a --noclose flag.
In xterm, there is a -hold flag.

wmctrl to open window without focus

I am using the command:
wmctrl -a **id of application** -i
This is very close to what I am looking for, however Is there a away I can use wmctrl without bringing focus to the application.
For example if i run the command to bring up a window on my 2nd screen in terminal the focus of the mouse and keyboard stay on terminal.
xdotool may come in handy in your situation.
You should save your active window's ID to temporary file:
xdotool getactivewindow > ~/.window_id
Then focus desired window:
wmctrl -a **id of application** -i
And then restore focus to the previous window:
xdotool windowfocus $(cat ~/.window_id)
xdotool windowactivate $(cat ~/.window_id)
Using xbindkeys, xdotool and wmctrl combined create a very powerful tool-set.

How to copy from tmux running in putty to windows clipboard

ENV: I am running tmux in putty, on a windows 7 laptop. I do ssh to linux systems while working.
I have to solve two glitches with tmux, before using it for all my needs.
I have to copy some text to the windows notepad from vim or the tmux terminal. I have enabled mouse support for both vim and putty.
I have tried "*y and "+y but both of them doesn't work. If I use putty's ability for copy then it copies the content of adjacent grids also.
I have to copy some content from vim file in one pane to vim file opened in another pane. Is there any vim trick for this. I don't want to use <C-b>[ way of doing it.
Problem 1 is haunting me more, because there is a w/a for problem 2.
I use putty v0.62 and tmux v1.8.
tmux config: setw -g mode-mouse on
I want to copy some text from tmux to system clipboard, I press and hold SHIFT and select the text by mouse, then click left-button of mouse.
I want to paste some text into tmux, press SHIFT and click right-button of mouse.
This is absolutely possible. All of the config lines below were copied from machines in different tmux panes back to my Windows machine. Yes, I putty into one machine and tmux and ssh from there to others and can move my vim yanks to any machine, including my Windows browser.
I do run an X server on Windows (doesn't need to be on Windows, but you need one somewhere).
Essential ingredients:
putty 0.62 (on Windows)
tmux 1.8 (on one Linux machine)
xclip (on Linux machines)
MobaXterm (on Windows for their x server)
Your network setup might differ a bit, especially on the X server display config but here it goes:
tmux
# allow mouse to select panes
setw -g mode-mouse on
set-option -g mouse-select-pane on
# allow yank into system clipboard
#bind C-y run "tmux save-buffer - | xclip -i"
bind C-y run-shell -b "tmux show-buffer | xclip -selection clipboard -i"
# move x clipboard into tmux paste buffer
bind C-p run-shell -b "xclip -o -selection clipboard | tmux load-buffer - ; tmux paste-buffer"
vim
" get some autoselect interaction with the system clipboard
set guioptions=aA
MobaXterm
I keep this running but don't ever have to run tmux in a xterm shell. Make sure that the shared clipboard is selected in the X11 settings. I suppose xming should work but the settings for my other needs never seemed to work out. MobaXterm has other goodies I like anyway (file browsers etc).
I suppose I could run ssh session with tmux in MobaXterm but I prefer the putty window where I have my solarized color palette.
putty
ssh to the machine with tmux. At this point, I set the DISPLAY environment variable and put it in an env file that is sourced by bash. This way, every subsequent window created will have it set. Setting it on other machines that I hop to from the tmux machine is a separate exercise entirely.
In any machine that will be sharing the clipboard, make sure you have the proper $DISPLAY set. You should be able to run xeyes and see it on your local machine.
Everyone will be sharing the clipboard. vim will copy to the clipboard when you yank. To paste in another tmux pane, use the bind-key ctrl-p (ctrl-a ctrl-p for me).
If it's not working, you can always try xclip -o -selection clipboard to see what should paste. you also will be able to move any "tmux selected" text (mouse or bind-key [) to the clipboard (and thus to a regular windows window) with bind-key ctrl-y (i.e. tmux yank).
PuTTY is just a terminal emulator; the Vim registers * and + are concerned about the X selection and clipboard; there's no overlap.
PuTTY only allows you to copy the selected terminal contents to the Windows clipboard; when you run tmux, that will inevitably include the window layout.
You need to switch from PuTTY to something that allows real integration, like the Cygwin XWin server, which is a real X Server that integrates the X clipboard with the Windows clipboard. Instead of inside the PuTTY session, you'd ssh -X into your server, and launch Vim in a Linux terminal, or GVIM directly. Then, yanking via "+y will work as you'd expect.
Alternatively, if you want to keep using PuTTY, you'd have to use some workaround, like :writeing the selection to a local file, and transferring that to Windows via scp, for instance.
With some trickery, it is possible to get the tmux buffer back through PuTTY and onto the client. I accomplished this using ANSI escape codes for the "AUX" port (serial printer).
Here is just one implementation of that method of transfer:
1) In server-side tmux.conf, add:
# Send the tmux copy buffer to a file. The file is read for ANSI printing by "t" alias in .bashrc
bind -t vi-copy y copy-pipe 'cat > ~/.tmux-buffer'
2) In server-side .bashrc, add:
t() {
# Configure a PuTTY profile to send "t" as the "Remote command". This
# function will automatically reattach to an existing tmux session if one
# exists, or start a new one. This function also repeatedly sends our
# homemade tmux clipboard back to the PuTTY client in the form of an ANSI
# printer escape sequence. The contents of the homemade clipboard are
# populated by `bind -t vi-copy y copy-pipe 'cat > ~/.tmux-buffer'` in
# tmux.conf. It is expected that the PuTTY client will be configured to
# print to a "Microsoft XPS Document Writer" which saves the printer output
# to a file. The file is subsequently read by an AutoHotkey macro, and the
# contents are made available for paste.
[[ "$TERM" == "xterm" ]] || return 0 # This prevents recursive runs, in case t() is called after tmux is started.
{ while :; do tput mc5; cat ~/.tmux-buffer; tput mc4; sleep 5; done } &
tmux attach || tmux
}
3) On client-side (Microsoft Windows), create new printer:
Add a Printer
Create a new port > Local Port
Enter a port name > "PuTTY_Printer_File"
Driver > Microsoft XPS Document Writer
Printer name > "PuTTY Printer"
Optional: Print a test page and make sure it shows up in contents of file # "%USERPROFILE%\Documents\PuTTY_Printer_File"
4) In client-side PuTTY configuration:
Set Terminal > "Printer to send ANSI printer output to:" to newly created printer named "PuTTY Printer"
Set Connection > SSH > "Remote command:" to "t" (referencing the .bashrc function above)
At this point you can send the contents of the tmux buffer to your PuTTY client by highlighting some text in tmux's copy-mode, and pressing y. The selected text will end in up %USERPROFILE%\Documents\PuTTY_Printer_File back on the client. If you want to go a step further and emulate "pasting" out of this file, you can use a hotkey sequence to read the contents of the file and insert it. Here's an example that leverages AutoHotKey, but it is probably possible to achieve the same result in PowerShell if you prefer.
5) Client-side AutoHotKey macro:
;### Get contents of PuTTY ANSI printer device output and paste it
#v:: ;Winkey + v
FileRead, PuTTYPrinter, %USERPROFILE%\Documents\PuTTY_Printer_File
SendInput %PuTTYPrinter%
PuTTYPrinter = ; Free up memory
return
6) Complete usage procedure:
Connect to server with PuTTY and get dropped into tmux by t() function.
When ready to select text for copy, use tmux hotkey for copy-mode ( Ctrl + b, [ )
Move cursor with arrow keys
Begin selection with spacebar
End selection and copy it with y
Back on client-side running PuTTY, WindowsKey + v will paste the selection
Since pictures are worth 1,000 words, here's an overview of what's happening:
https://media.licdn.com/mpr/mpr/AAEAAQAAAAAAAAfiAAAAJDYzM2RmMzYzLTk1NmQtNGQxMi1iN2YyLTQ4NGUxNjExMmVlOA.png
Assuming mouse mode is already on, add this to your tmux config and selecting will now copy to your clipboard.
bind -Tcopy-mode MouseDragEnd1Pane send -X copy-selection-and-cancel\; run-shell -b "tmux show-buffer | clip"
Tested on v2.6

Resources