How do I merge four windows in to four evenly-spaced panes? - tmux

I have four windows called PP1, PP2, PP3, and PP4. I would like to join them so that they would look something like this:
-----------------
| | | | |
|PP1|PP2|PP3|PP4|
| | | | |
-----------------
Currently I'm using join-pane, but I am unsure of how to make them evenly spaced.
tmux join-pane -h -s PP2 -t PP1
tmux join-pane -h -s PP3 -t PP1
tmux join-pane -h -s PP4 -t PP1
I've also tried adding this to the end of the script, but to no avail:
tmux select-layout -t PP1 even-horizontal

Using the keyboard, you can press your activation key (Ctrl + B by default) and then press Alt + 1. Alternatively, repeated Ctrl + B, Space will cycle through the common layout options.

Related

Most frequently used commands during the last x months

I know how to get the most used shell commands in zsh with
history 1 | awk '{$1="";print substr($0,2)}' | sort | uniq -c | sort -n | tail -n 20
but is there a way to restrict myself to let's say the last two or three months?
I need this because I would like to create aliases for the commands I am currently using most.
history in zsh have several flags to show date and time stamp. For this to work, you have to add setopt extended_history to your .zshrc file.
If you have extended_history enabled, history -i will show full time-date stamps in ISO8601 `yyyy-mm-dd hh:mm' format. Dates in this format can be compared as strings. So just change your awk script and use it to select only lines after some date.
history -i 1 | awk '{ if ($2 >= "2020-05-01") { $1=$2=$3="";print $0; } }' | sort | uniq -c | sort -n -r | head -n 20
Be aware that if you have HIST_IGNORE_ALL_DUPS or HIST_IGNORE_DUPS options enabled, this will not work as intended.
You can also use date command to get older date automatically.

grep multiple files get count of unique cut

I think I'm close on this, and saw similar questions but couldn't get it to work as I want. So, I have several log files and I would like to count the occurrences of several different service calls by date.
First I tried the below, the cut is just to get the first element (date) and 11th element (name of service call), which is specific to my log file:
grep -E "invoking webservice" *.log* | cut -d ' ' -f1 -f11 | sort | uniq -c
But this returned something that looks like:
5 log_1.log:2017-12-05 getLegs()
10 log_1.log:2017-12-05 getArms()
7 log_2.log:2017-12-05 getLegs()
13 log_2.log:2017-12-04 getLegs()
What I really want is:
12 2017-12-05 getLegs()
10 2017-12-05 getArms()
13 2017-12-04 getLegs()
I've seen examples where they cat * first, but looks like the same problem.
cat * | grep -E "invoking webservice" *.log* | cut -d ' ' -f1 -f11 | sort | uniq -c
What am I doing wrong? As always, thanks a lot!
Your issue seems to be that grep prefixes the matched lines with the filenames. (grep has this behavior when multiple filenames are specified, to disambiguate the results.) You can pass the -h to grep to not print the filenames:
grep -h "invoking webservice" *.log | cut -d ' ' -f1 -f11 | sort | uniq -c
Note that I dropped the -E flag, because it is used to enable extended regex support, and your example doesn't need it.
Alternatively, you could use cat to dump the content of files to standard output, and pipe that to grep. That would work, because it removes the need for filename parameters for grep:
cat *.log | grep "invoking webservice" | cut -d ' ' -f1 -f11 | sort | uniq -c

Tmux: Switch the split style of two adjacent panes

I realize that reshaping pane layout in general is not trivial to describe, but I'm interested in at least being able to swap the layout of two adjacent panes.
Suppose I've got these panes laid out like this
_____________
| | |
| | 2 |
| |________|
| 0 | |
| | |
| | |
| | 3 |
|____| |
| 1 | |
|____|________|
And I now discover that I want even more vertical space with pane 3. It's clearly not trivial to consider how I could rearrange it with pane 0 or 1 but it would be really nice if I could tell 3 and 2 to flip splitting axis:
_____________
| | | |
| | | |
| | | |
| 0 | | |
| | 3 |2 |
| | | |
| | | |
|____| | |
| 1 | | |
|____|_____|__|
(note here tmux will most likely reorder it so the old 3 becomes the new pane #2)
Currently when I try to run join-pane -h to reorganize it it tells me "cannot join pane to its own window". Yeah, well, I'm just trying to shuffle it without disrupting the entire layout.
I guess a workaround is to break it out to its own named window and then immediately join it back, though it looks like it won't be able to determine the orientation to switch to (horizontal vs vertical).
Note this is different from the operation of swapping the locations of two panes, for which there exists a command ready to use. I'm looking to swap the splitting axis.
Prefix + Space is bound to next layout
# Here is a jewel of a bind which does the task of flipping the
# orientation of the current pane with the pane before it (in the
# ordering) -- I had a SO question on this and nobody answered.
bind -n M-f move-pane -t '.-'
bind M-f move-pane -t '.-'
bind -n M-r move-pane -h -t '.-'
bind M-r move-pane -h -t '.-'
The comment is a lie, this functionality is not intuitive and frankly sucks, but it does work in a pinch.
Update: I have since updated this to remove the -n binds in my config, basically you do want to leave open the ability to "pass through" the keystroke through tmux. For example after a while I desired the same exact functionality inside Vim, which I bound to the same combination.
UPDATE: Can combine with the great solution here: https://stackoverflow.com/a/70024796/340947
This allows you to grab a pane and walk it all around. Makes my binds obsolete but both could be kept (like I will) for the familiar behaviors. I don't really like that mine will do something confusing if you happen to run it on the first pane ('.-' would pick the bottom right pane in this case).
C-b space (bound to next-layout by default) cycles through available layouts
#flipping the orientation of the current pane with the pane <arrow-way>-of
bind -n S-Up move-pane -h -t '.{up-of}'
bind -n S-Right move-pane -t '.{right-of}'
bind -n S-Left move-pane -t '.{left-of}'
bind -n S-down move-pane -h -t '.{down-of}'
here are my example to change the otiantation with the pane in the way of the arrow only Press Shift and Arrow-key or rebind the command how you want
If you really want to do exactly what you said, I would recommend breaking the pane out to a new window, then joining it back in.
For example...
Make sure you have one of the two panes active and break it out:
prefix break-pane
Go back into the main window (prefix w) and Perform any operations on the remaining panes to get it to the layout you want
Make sure you have the pane active you want the other pane to come in adjacent to
Make sure window is named (or assigned name is known)
Go to the new window that pane was broken out into (prefix w again)
join-pane -t <Main Window Name> -h (or -v if you want a vertical split)
To reiterate:
You have panes
laid out like
this:
_____________ _____________
| | | | | | |
| | 2 | | | | |
| |________| | | | |
| 0 | | but you want | 0 | | |
| | | them to look | | 3 |2 |
| | | like this: | | | |
| | 3 | | | | |
|____| | |____| | |
| 1 | | | 1 | | |
|____|________| |____|_____|__|
Since you know all the pane numbers (particularly the pane numbers of the panes you want to rearrange), the short answer is to type:
CTRL+B :move-pane -s 3 -t 2 -h ENTER
But if you don't know all the pane numbers, here is a simple way to reposition a pane:
If your mouse support is not already turned on, turn it on with:
CTRL+B :set-option -g mouse on ENTER
Using your mouse, click on the pane that you want to tear off -- that is, click on the pane that you want to reposition to be to the right or below another pane. (In your example, this will be pane 2, because you want to tear it off an reposition it to the right of pane 3.)
Next, click on the pane to which you want to attach/reposition the torn-off pane. (In your example, this will be pane 3, as you want to attach pane 2 to the right of it.)
Finally, type:
CTRL+B :move-pane -s ! -h ENTER
Explanation:
The magic is mostly happening in step 4, with the -s ! switch. It tells tmux to move the next-to-last pane you clicked on to the last pane you clicked on.
The -h switch in step 4 tells tmux to reattach the pane horizontally -- that is, to the right. If you want to attach it below, either use the -v switch instead, or leave out the -h switch altogether.
Notes:
These steps will place pane 2 to the right of pane 3. If you want pane 2 to the left of pane 3, then simply run through the steps with pane 3 as the "tear-off" panel, and pane 2 as the panel to attach to. (Or you can swap the panels; you can read through man tmux to figure out how.)
If you run step 4 via the Unix shell prompt, remember to quote the !, like this:
tmux move-pane -s '!' -h
The line:
set-option -g mouse on
is a great line to put inside your ~/.tmux.conf file, if you don't already have it in there.
As you said above, tmux will assign new pane numbers after you reorder them.
To see the pane number of a particular pane, click on the page you want (to make it current), and type:
CTRL+B   i
If the status message that shows you the pane number disappears too fast for you, you can lengthen the time it is visible with:
CTRL+B :set-option -g display-time 4000 ENTER
To quickly see the pane number/index of all the panes, type:
CTRL+B   Q
If the pane numbers disappear too fast for you, you can lengthen the time they are visible with:
CTRL+B :set-option -g display-panes-time 4000 ENTER
Combine your prefix (usually crtl+b) and then space.
When you have 2 panes, it will switch between horizontal to vertical mode.

Get just the staus of autosys (main) job/box

I have a main autosys box (first_start_main_job)that has 2 different sub box.
When I enter autorep -j first_start_main_job -d I get something like:
JOb Name | Last Start | Last Run | ST | RUN | Pri/Xtx
first_start_main_job | some_time | some_time | SU | some_text
first_start_sub_job | some_time | some_time | SU | some_text
second_start_sub_job | some_time | some_time | SU | some_text
I just want ST(status) of first_start_main_job and store that in a variable.
Please let me know how to accomplish this.
Thanks in advance..
Use the print level switch -L, with level 0, (zero) to list only the outermost box. Then use your favorite script tool to get and store the ST value.
For example:
autorep -J main_job_box -d -L0
man autorep from a AutoSys command prompt will give you more information if you need it.
Just adding up to above posted answer.
To get the Status of Job in a variable we can filter out the status using awk.
e.g.
autorep -J first_start_main_job -d -L0 | awk '/SU /{print $6}'
It will check for the first line and if it contains "SU ", than the status will be printed.

sorting ls-l owners in Unix

I want to sort the owners in alphabetical order from a call to ls -l and cannot figure out a way to do it. I know something like ls-l | sort would sort the file name but how do i sort the owners in order?
The owner is the third field, so use -k 3:
ls -l | sort -k 3
You can extend this idea to sorting based on other fields, and you can have multiple -k options. For instance, maybe you want to sort by owner, and then size in descending order:
ls -l | sort -k 3,3 -k 5rn
I am not sure if you want only the owners or the whole information sorted by owner. In the former case superfo's solution is almost correct.
Additionally you need to remove repeating white spaces from ls's output with tr because otherwise cut that uses them as a delimiter won't work in all directories.*
So in the end you get this:
ls -l | tr -s ' ' | cut -d ' ' -f 3 | sort | uniq
*Some directories have a two digit value in the second field and all other lines with a single digit get an additional whitespace to preserve the layout.
How about ...
ls -l | cut -d ' ' -f 3 | sort | uniq
Try this:
ls -l | awk '{print $3, $4, $8}' | sort
It will print the user name, the group name and the file name. (File name cannot contain spaces)
ls -l | awk '{print $3, $4, $0}' | sort
This will print the user name, group name and the full ls -l output, sorted by the user name first, then the group name, then what ls -l prints first

Resources