If it is okay that all the tabs will open in one new terminal window instead of the current one, you can use something like this below instead of simulating key presses with xdotool
:
find "$(realpath .)" -mindepth 1 -maxdepth 1 -type d -printf "--tab --working-directory '%p'\n" | xargs gnome-terminal
Explanation:
The same formatted in a more readable way:
find "$(realpath .)" \
-mindepth 1 -maxdepth 1 -type d \
-printf "--tab --working-directory '%p'\n" |
xargs gnome-terminal
What it does is:
realpath .
gets the absolute path of the current directory
find [...]
lists all files and folders inside the directory given as first argument which match the following criteria, in the specified way.
-mindepth 1
and -maxdepth 1
disables recursion, so that you only get results from directly inside your search folder, without that folder itself and without sub-subdirectores.
-type d
limits the results to only directories, no files etc.
-printf "[...]"
tells find
to print the results it finds to its standard output, using the following format string. %p
will be replaced by the result's path, starting with the search directory (which is why we used realpath
earlier to make it absolute instead of just using .
)
So far, the output of just running the part before the |
pipe would look like:
--tab --working-dir '/tmp/test/folder1'
--tab --working-dir '/tmp/test/folder2'
--tab --working-dir '/tmp/test/folder3'
xargs
gets the output from above piped into its standard input. It will read all input lines and use them to construct and run a command line, by appending them to its own arguments. The resulting command it would run with the example above would be:
gnome-terminal --tab --working-dir '/tmp/test/folder1' --tab --working-dir '/tmp/test/folder2' --tab --working-dir '/tmp/test/folder3'
Now that constructed command will open a new gnome-terminal
window, with one tab per --tab
argument, and applying all arguments after each --tab
(and before the next one) to that specific tab. --working-dircetory /PATH/TO/SOMEWHERE
sets the initial working directory to the given folder.
cd
happens in your current tab/shell, not in the new tab's shell. You'd have to emulate typing that command in there after a specific waiting time too, which gets really uncomfortable, as you not only have to wait for it to finish typing, if you press any key during that or focus another window, everything will break. – Byte Commander Jun 13 '18 at 09:50