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?

  • I've written couple scripts before for opening multiple terminal windows. Particularly this one. wmctrl . What I've noticed with Unity just now is that wmctrl -d reports just one desktop even though i have multiple workspaces enabled. That might be an issue. Aug 21, 2015 at 22:16
  • So you need 5 terminal windows with vim specifically, each on its own workspace, right ? Aug 21, 2015 at 22:33
  • @Serg Right (well, the setup is actually 6 terminals, 2 in one workspace, but I figure I can just change things accordingly, haha). Also, wmctrl -d reports only one desktop for me also. Aug 22, 2015 at 0:53
  • @Serg This talks briefly about how Compiz uses viewports and not workspaces—so while there's technically only one workspace, I have nine viewports. Here's is another related post I'm reading right now. Aug 22, 2015 at 1:01
  • Hi AmagicalFishy, the greater part of the answer is also here: askubuntu.com/a/621811/72216. Just combine the commands to call the windows with &&. Aug 22, 2015 at 11:58

1 Answer 1


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.

# 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
# while loop that does all the work
while [ $COUNT -ne 0 ]; do
# reset x and y position on each iteration
    # 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]} )
    # determine if it's window 4 - 6
    if [ $COUNT -gt 3 ];then
        YPOS=$( expr $YPOS + ${ARRAY[1]} )
    # determine if it's window 2 or 5
    if [ $( expr $COUNT % 3 ) -eq 2 ];then 
        XPOS=$(  expr $XPOS + ${ARRAY[0]} )
    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 )

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

  • Hmm 3x3 is 9 :)
    – A.B.
    Aug 22, 2015 at 6:53
  • @A.B. Ha, I've overdone it and was too tired to notice it o_O Aug 22, 2015 at 11:43
  • Hi Serg, wmctrl can, but you have to make sure the targeted window position makes the window completely inside the targeted workspace. Another issue is that the answer should place the windows relative to the current workspace. That implies that you "translate" the positions from relative (from the current workspace) to absolute (on the spanning viewports) and vice versa. Complicated stuff, but very interesting, see: askubuntu.com/a/621811/72216. IMO OP is overasking with the last step: "VIM to a particular file" Aug 22, 2015 at 11:57
  • @JacobVlijm Thank you for the link and explanation. As I understand , if my viewport 2 starts on (1366,0 ), then I'd have to position the window around (1400,100), so that it's fully inside, not just on the edges - (1366,0). Is that correct ? Also, I probably was doing something wrong, as -e flag works as I'd expect it. Strange Aug 22, 2015 at 13:41
  • I have to wonder, Serg, why you use a bash shebang when you won't use bash features.
    – muru
    Aug 22, 2015 at 15:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.