Just wrote a script that would open 3 new tabs in a window.
tab_init.sh:
#!/bin/bash
# options="--hide-menubar --geometry=140x42"
options=""
options_each=()
# tabs
cmds[1]="cd ~/a; clear"
cmds[2]="cd ~/b; clear"
cmds[3]="cd ~/c; clear"
for i in 1 2 3; do
options_each+=(--tab -e "bash -c '${cmds[i]} ; bash'" )
done
gnome-terminal $options "${options_each[#]}" &
exit 0
Current result:
After executing the script, there will be 3 more tabs opened in current window, plus the original tab, there would be 4 tabs.
Desired result:
But what I want is to open the new tabs in a standalone window, without including any other tab.
The questions are:
How to close the original tab automatically from the script, so that there would be only 3 tabs(the new ones) after executing the script?
Or, can I open the 3 new tabs in a new window, not in the original window, so that it doesn't matter whether I close the original tab.
Kind of found a way to do this.
First, make soft link of the script into $PATH.
e.g link it as tab_init somewhere,
Then, define command shortcuts in ~/.bashrc, to add the extra behavior.
e.g
alias tabinit='tab_init; exit' # init tabs, and close original tab,
alias tabinitne='tab_init' # init tabs, and keep the original tab,
Now, could use command tabinit or tabinitne to choose the behavior desired.
Related
Usually I get an excel spreadsheet with dozens of filenames, for which I then need to go and search individually.
Spreadhseet
Is there a way that I could simply:
Select All filenames in e.g. row A of Excel,
then Search for all these files on "This Mac"
then Copy all found files into the New Folder on the Desktop
So far I've tried the first part of searching and this is what i get :a)
Automator with Variable. But the problem is, it only searches for 1 file from selection
b)
Automator with Shell Script (Copy to Clipboard > Open Finder > CMD+F (to highlight Search dialog) > CMD+V). It opens a new Finder window, but it doesn't paste the clipboard into search dialog
c) /usr/bin/pbcopy
on run {input, parameters}
tell application "System Events"
keystroke "f" using {command down}
keystroke "v" using {command down}
end tell
return input
end run`
End result, is same as option b). I was planning to run this in Automator as a 'Service', which I could later assign to Keyboard Shortcut.
I am pretty sure there should be a simple shell option for this - any advice would be much appreciated.
I made a bash script that does what you want. You would basically select a bunch of filenames in Excel, or any other app, and copy them to the clipboard with ⌘C. After that you need to run the script and it will take items from the clipboard and search for TIFF or JPEG images that match that name and copy them to a directory on your Desktop called Selected Files:
#!/bin/bash
# Get contents of clipboard into bash array
files=( $(pbpaste) )
# Create output directory - no checks for already existing or already containing files
OUTDIR="$HOME/Desktop/Selected Files"
mkdir -p "$OUTDIR"
# Iterate through fetching files
for ((i=0;i<${#files[#]};i++)) ; do
name=${files[i]}
result=$( mdfind "kMDItemDisplayName == \"${name}.*\" && (kMDItemKind==\"TIFF image\" || kMDItemKind==\"JPEG image\")" )
if [ -f "$result" ]; then
echo $name: $result
cp "$result" "$OUTDIR"
else
echo ERROR: Searched for: $name, found $result
fi
done
I am not sure of your level of familiarity with bash, so you may be able to ignore the following...
Make a new directory for your own scripts:
mkdir -p $HOME/scripts
Save the above script in that directory with filename:
$HOME/scripts/gather
Make the script executable by typing this into Terminal:
chmod +x $HOME/scripts/gather
Edit your login profile ($HOME/.profile) and add your $HOME/scripts directory to your PATH:
export PATH="$PATH":$HOME/scripts
Then start a new Terminal and you can use any script that you have saved in $HOME/scripts without needing to specify the full path to it, e.g.:
gather
Following information kindly contributed by #user3439894 in comments section, as I am out of my depth on this aspect...
To use a keyboard shortcut, you'd have to create an Automator "Service workflow" with a "Run Shell Script" action, which you can assign a keyboard shortcut to under: System Preferences > Keyboard > Shortcuts > Services
Is there a way to list all window names and depending on the result, creating a new window with a specific name into this (running) session.
How to create a new screen session with assigned window names is documented in the man pages, but i could find information about a solution to the problem above.
From outside the screen session, I don't think so.
But if you are starting from inside, in one of the windows of the right screen session, then yes:
for window_name in foo bar baz quux ; do ## ...
screen -t $window_name
done
You can even get fancy and run some initial commands in each window! This snipped of copy-paste bash helps me get back to work quickly after a reboot. Once I've started the screen session:
for n in $(seq 1 8) ; do ## ...
screen -t proj_$n bash -c "cd /src/foo/proj_$n*/ ;"\
' eval `set_proj_env_vars.sh` ; svn status ; make clean ; make ;'\
' exec bash --login'
done
...and as a great side effect the screen windows are numbered for the various checkouts, where each one can be working on a different bug/feature. Overkill? Totally! But it's a fun hack.
Say I have a bash window like this:
# cmd 1
output of cmd 1
# cmd 2
output of cmd 2
# (cursor here)
Is it possible to move the cursor (with keyboard only) to previous line, say output of cmd 1, copy some words and paste them to the current cursor position?
I.e. Is there a bash equivalent of the following command in vim:
kkkvllyGp
Thanks
You can use GNU screen.
In a screen session, ctrl+a, esc enters copy mode, where you can move the cursor with the arrow keys, mark start and end points with space, and then paste with ctrl + a, ]
bash has no knowledge of what the terminal displays. If you want to capture the output of cmd 1 and do something with it, you either need to redirect it to a file or capture it in a variable. eg:
cmd 1 > cmd1out.txt
or
CMD1OUT="$(cmd 1)"
Then, once you've captured that output within the shell, you can use it as you like. For example, to execute that output directly as another command, you could do
source cmd1out.txt #if you sent it to a file
or
eval "$CMD1OUT" #if you captured it in a variable
I know how to open all files each in one tab or each in one window, but is it possible to make them open in a combination of tabs/windows?
I am using gvim 7.3
I think you have the notion of tab pages backwards. From :h tabpage:
A tab page holds one or more windows.
I'm not sure if it's what you want, but you can create two tabs each with two windows through liberal use of -c on the command line:
gvim -p2 first.txt third.txt -c "sp second.txt" -c "tabn" -c "sp fourth.txt"
This leaves the focus on the 4th file. You can rearrange the arguments (or add more) if you want to do something different. Also note that gvim will confusingly tell you that it's only opening two files.
You can use :[count]tab {cmd} (tab-page-commands) in combination with :[n]sbnext [N] (buffer-list) as follows:
vim file1 file2 file3 file4 +sbn +"tab sbn" +sbn
Just add more +"tab sbn" +sbn for more files. If you want to split vertically, replace +sbn by +"vert sbn".
My xterm $prompt variable in my .tcshrc is:
set prompt="%{\033]0;%m:%~\007%}%{^[[;37;1m%}%B%{^[[;34;1m%}%m%{^[[;34;1m%}:%b%c%# "
The highlighted part above (%{\033]0;%m:%~\007%}) puts the hostname (%m) and the current directory (%~) in the title bar. (At least I think that that's what puts it in the title bar; it's been a while since I fiddled with this prompt).
When I run screen, however, the current directory stops getting updated when I change directories.
My questions:
How can I make this prompt work in screen?
Is there a better way to display the current directory in the title bar?
I am running linux with xterm and tcsh.
I think there is no direct way, because of the way screen works. However screen can display its own status bar, that you can define in .screenrc. Here's mine for instance :
hardstatus alwayslastline
hardstatus string '%{= kG}[ %{G}%H %{g}][%=%{=kw}%?%-Lw%?%{r}(%{W}%n*%f%t%?(%u)%?%{r})%{w}%?%+Lw%?%?%= %{g}][%{B}%Y-%m-%d %{W}%c %{g}]'
Firstly, to make it work you must check where exactly is the line with set prompt=blah-blah in your .tcshrc. For example, the code below that perfectly works in plain xterm would not work under screen in xterm:
switch ($TERM)
case "xterm*":
set prompt="%{\033]0;${HOME:t}#%m:%l:%c08\007%}%{\033[36m%}%l:%c02%#%{\033[0m%} "
# update xterm title to display current cmd in it
alias postcmd 'echo -n "\033]0;${HOME:t}#`hostname`:${tty} \!#:q\007"'
...
because screen by default sets $TERM variable to screen and not xterm! So you must add:
case "screen":
# lame, but prevents an error in screen after 'su - root'
if (! $?WINDOW) setenv WINDOW 1
set prompt="%{\033]0;${HOME:t}#%m:${WINDOW}:%c08\007%}%{\033[36m%}%c02%#%{\033[0m%} "
alias postcmd 'echo -n "\033]0;${HOME:t}#`hostname`:${WINDOW} \!#:q\007"'
...
Secondly, make sure yo have this line in ~/.screenrc:
termcapinfo xterm* 'hs:ts=\E]2;:fs=\007:ds=\E]2;\007'