1

Bonus: vsplit to another file. ;)

I have 9 workspaces, and often have multiple terminals running VIM on different files. The setup is pretty standard for a project—but I have to go about opening five terminals, moving each to its own workspace, maximizing, opening a .hpp file in VIM and vsplitting its corresponding .cpp file for each terminal.

I'd like to write a script for this, but am at a loss as to how! Searching Google and testing on gedit, I began with something like

wmctrl -s 4 ; gedit & sleep 3; wmctrl -s 0;

But this only opens up gedit in the workspace I'm currently on. One potential issue (but I'm ignorant enough about this to not know whether or not it's actually a problem) is that I use the Compiz wallpaper feature that allows different wallpapers on different workspaces.

Ideally, I would like to work towards a script that does this for me and not a plugin.

Any hints or ideas?

1 Answers1

0

The script bellow spawns 6 windows for gnome-terminal with vi text editor and moves them to appropriate viewports (aka workspaces ) in Unity desktop.

The script determines mathematically which window belongs on which workspace. It relies on wmctrl to do most of the job. To install wmctrl , do sudo apt-get install wmctrl.

#!/bin/bash
# Author: Serg Kolo
# Date: Aug 22 , 2015
# Description: script that spawns 6 windows and positions
# them on individual workspaces (aka viewports) for Unity
# Written for: https://askubuntu.com/q/664309/295286
#set -x

# get number of vertical and horisontal viewports
HEIGHT=$(gsettings get org.compiz.core:/org/compiz/profiles/unity/plugins/core/ hsize)
WIDTH=$(gsettings get org.compiz.core:/org/compiz/profiles/unity/plugins/core/ vsize)
# Get size of the desktop from wmctrl -d
# ARRAY[0] is width and ARRAY[1] is height
ARRAY=($( wmctrl -d | awk -v div1=$HEIGHT -v div2=$WIDTH '{gsub(/x/," "); print $4/div1" "$5/div2}' ))
# set number of windows
COUNT=6
# while loop that does all the work
while [ $COUNT -ne 0 ]; do
# reset x and y position on each iteration
XPOS=100
YPOS=100
    # spawn terminal window with appropriate number
    # and send to viewport according to number
    gnome-terminal -e 'sh -c "vi;bash"' -t VP$COUNT &
    sleep 0.250 # delay to make sure window spawns
    # determine if it's a window 3 or 6
    if [ $( expr $COUNT % 3 ) -eq 0 ];then 
        XPOS=$( expr $XPOS + ${ARRAY[0]} + ${ARRAY[0]} )
    fi
    # determine if it's window 4 - 6
    if [ $COUNT -gt 3 ];then
        YPOS=$( expr $YPOS + ${ARRAY[1]} )
    fi
    # determine if it's window 2 or 5
    if [ $( expr $COUNT % 3 ) -eq 2 ];then 
        XPOS=$(  expr $XPOS + ${ARRAY[0]} )
    fi
    sleep 0.250
    # bring that window to focus and move it
    wmctrl -R VP$COUNT
    wmctrl -r VP$COUNT -e 0,$XPOS,$YPOS,250,250
    # decrement counter
    COUNT=$( expr $COUNT - 1 )
done

References and additional material:

Script (or software) to open an application window on a specific viewport and position

Creating and Running a script

How do I start applications automatically on login

How do I bind .sh files to keyboard combination