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?

  • 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. – Sergiy Kolodyazhnyy Aug 21 '15 at 22:16
  • So you need 5 terminal windows with vim specifically, each on its own workspace, right ? – Sergiy Kolodyazhnyy Aug 21 '15 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. – AmagicalFishy Aug 22 '15 at 00: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. – AmagicalFishy Aug 22 '15 at 01:01
  • Hi AmagicalFishy, the greater part of the answer is also here: http://askubuntu.com/a/621811/72216. Just combine the commands to call the windows with &&. – Jacob Vlijm Aug 22 '15 at 11:58
  • Also, Unity has one workspace by definition, split into viewports. – Jacob Vlijm Aug 22 '15 at 12:09

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

Sergiy Kolodyazhnyy
  • 105,154
  • 20
  • 279
  • 497
  • Hmm 3x3 is 9 :) – A.B. Aug 22 '15 at 06:53
  • @A.B. Ha, I've overdone it and was too tired to notice it o_O – Sergiy Kolodyazhnyy Aug 22 '15 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: http://askubuntu.com/a/621811/72216. IMO OP is overasking with the last step: "VIM to a particular file" – Jacob Vlijm Aug 22 '15 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 – Sergiy Kolodyazhnyy Aug 22 '15 at 13:41
  • I have to wonder, Serg, why you use a bash shebang when you won't use bash features. – muru Aug 22 '15 at 15:46
  • @muru because I'm using array there, and dash complains about syntax :) – Sergiy Kolodyazhnyy Aug 22 '15 at 17:08
  • @Serg who said anything about dash? – muru Aug 22 '15 at 17:09
  • @muru well, if I were to change shebang line to sh instead of bash that would be dash – Sergiy Kolodyazhnyy Aug 22 '15 at 17:10
  • @Serg oh. You do have an array. I was talking of the unspeakably ugly way of doing arithmetic checks. – muru Aug 22 '15 at 17:11
  • @muru em . . . yeah . . . .because I don't know the bash arithmetic features . . . and I spend most of my time in mksh . . . – Sergiy Kolodyazhnyy Aug 22 '15 at 17:13
  • @Serg You cannot tell, since you don't know which is your current workspace. If you e.g. are on ws 2 (assuming the workspaces are horizontally arranged) , a window on ws 1 is between -1366 and 0 (x-wise). Same with the vertical arrangement. You might need to first let a function in the script find out what is the workspace arrangement (cols/rows). I think I did in the linked question (not sure, it is a while ago). Then you can simply calculate with a vector of each workspace towards the origin of the spanning viewports. – Jacob Vlijm Aug 22 '15 at 19:19
  • You need to suffer a bit from a neurotic compulsion to make it work, but it can be done quite well :) – Jacob Vlijm Aug 22 '15 at 19:21