Linux Milestone?

I feel like I’ve passed some kind of Linux milestone today. I was reading up on i3wm configurations, and the unique situation that I have where I want to share a single configuration between a desktop environment with two screens, and a laptop which may be plugged into an external screen. I’ve been meaning to further configure this situation, and make it a little more flexible. I managed to get that working in an XMonad install, since you program that in Haskell and so have some more flexibility than i3wm’s configuration gives you, but doing the same in i3wm is still a little tricky.

The key difference is that i3wm is configured via its own configuration syntax. That makes it easier to configure for somebody who isn’t familiar or comfortable with programming, so it becomes more accessible. The natural trade-off, however, is that it becomes less flexible in what it can do for the user. XMonad, however, essentially is just a framework for a window manager, and you, the user, have to code everything you want in Haskell. Great for customization, but not so good for accessibility for new users. This is also the main reason I didn’t go far in it, because I also don’t know how to program in Haskell!

In any case, in XMonad I managed to get a simple script working that scanned what my setup was—how many monitors, what resolutions, and so on—and adjusted window sizes based on that. In i3wm, however, that information is loaded up when i3wm starts, and so it’s not a thing you can easily do on the fly. So, I’m searching on DuckDuckGo to see if anybody’s solved this problem (after all, why reinvent the wheel here?). I found a post where somebody hacked together a Python 3 script to edit his configurations (sound familiar?) and I had An Opinion. Before, I tended to read and absorb, assuming that the poster would have a good idea. Now, though, I realized I had solved the problem in a way that I felt was more efficient.

To me, that’s always been a good sign of learning something: realizing that you’ve solved something; recognising a moment where you can help somebody else make things better. I’ve never considered myself a very technical person (a consequence, I believe, of knowing people who work in IT professions, and comparing myself to what they know and can do), but this was the first time I felt like an amateur versus a person just hoping not to screw up their install whenever they do something.

Linux Upskill Challenge

A week ago, I started with something called the Linux Upskill Challenge. It’s a monthly set of lessons posted by /u/snori74 (as far as I am aware, not the Icelandic poet come back to life with a passion for Linux). The set of lessons are to teach you about being the administration of a Linux server, with practical challenges. You start off by getting a server online (as suggested by /u/snori74, I chose a $5 server package), and weekday-by-weekday, there are short lessons on how commandline administration of the server.

I figure that, by now, I can call myself an average Linux user: I know enough to solve quite a few problems by myself and I know enough to search for the right solutions to my issues; however, I’m also clearly not to the level of the Linux users who will program their own widget to create an alternative to this or that program that they have an issue with. This program has really worked for me so far. I know enough to easily progress through the first couple of lessons, and yet each had really new information for me in the extensions.

I can really recommend it to anyone that wants to learn a little more about Linux.

My First Sed Script (That Became a Bash Script?)

Last post, I wrote about a Sed script I wrote to help me switch configurations. As always, I’ve learned a little in the meantime, and I’ve updated the script to reflect that. As a result, it’s not a Sed script anymore, but a Bash script that calls on Sed directly to perform tasks.

I noticed that I was not being very efficient. For one, I created three files to do something that could be done in one. For another, two of those files (the Sed scripts) were virtually identical, with only two words being changed in four instances. When saw what I had done, I was reminded of a thing I read somewhere, but sadly cannot recall where:

If you’re doing something more than once, you’re not using a computer correctly.

Anonymous?

It’s a little provocative, of course, but I like the central idea of it. A computer is meant to automate tasks, and if you, the human user, are repeatedly performing the same task, surely you should let the computer handle that? Furthermore, there’s an added risk of error: I could edit one file and forget to edit the others to match. Or, of course, were I to write a much, much larger program, copy-pasting the same code only leads to a less legible program in the end.

So, I figured to replace the repeated words with a variable, set in the Bash script, and that reduced the need for two separate Sed scripts. Now that I was down to just the one small script, I realized I could just put it directly in Bash. I would lose the Sed syntax highlighting in Vim (as it would be highlighting Bash), but to be fair the Sed highlights aren’t that great anyway. So, long story short, I ended up with the following, single script:

#!/bin/bash
# switch_config.sj -- Bash script to switch over i3wm configs
# $1 = switch argument

# Check what file to change
case $1 in
    -d|--desktop) to=Desktop
	    	  from=Laptop
		  ;;
    -l|--laptop)  to=Laptop
	    	  from=Desktop
		  ;;
    *) echo "Select either -d, --desktop, -l, or --laptop"; exit 1
	    	  ;;
esac

# Back up file, using \cp to avoid interactive alias
\cp config -f config.old

# Catch potential errors
if [ $# != 1 ]
	then echo "Error: please provide one argument."; exit 1
fi


echo "Swapping i3wm configuration over from $from to $to."

# Using sed, comment out one set, and uncomment the other
sed -i '
/# '"$from"'/,/^$/{
	/# '"$from"'/b
	/^$/b
	/^#/!s/\(.*$\)/\#\1/
}

/# '"$to"'/,/^$/{
	/# '"$to"'/!s/#//
}
' config

My First Sed script

2020.07.22 edit: There’s a followup to this post, that you can read here. It has an updated version of the script.

For the past weeks, I’ve been reading through O’Reilly’s Sed & Awk, 2nd Edition, to learn those powerful commandline tools. The other day, I had a practical problem for which Sed seemed like the perfect solution out of the box. It’s small, and quite trivial, but it made me proud nonetheless.

In brief, the situation I run into is that I run Manjaro Linux using the i3WM windows manager on both my desktop and laptop, and I would like to share configuration files between them; however, due to their different resolutions, I want most but not all settings to be shared. Specifically, I have a few windows I keep on a scratchpad (basically: floating windows that you can hide, as well as keep consistent across workspaces). I still tweak these files every so often as I try to add features I like or adjust the look of this or that. I then share the config file between my two PCs, which leads me to having to adjust a few settings:

 # Laptop configuration
 for_window [class="^popup_term$"] resize set 960 540
 for_window [class="^popup_note$"] resize set 455 758
 bindsym $mod+equal [class="^popup_note$"] scratchpad show, move position 906 5
  
 # Desktop configuration
 #for_window [class="^popup_term$"] resize set 960 540
 #for_window [class="^popup_note$"] resize set 455 1070
 #bindsym $mod+equal [class="^popup_note$"] scratchpad show, move position 2540 324

By the way, you can see the constant tweaks here, because I used to have the popup-term class take different sizes depending on PC or laptop, but I recently settled on using the same resolutions for both. So, that one, for instance, will have to be edited out again into the general settings. The settings above used to be part of those settings, but I split them off to separate the desktop/laptop-specific configurations. Then, using my newly-gained Sed skills I made a tiny script that just comments out one set of code and removes the comments for the other:

# Commands to switch to desktop configuration

# Comment out laptop configurations
/# Laptop/,/^$/{
        /# Laptop/b
        /^$/b
        /^#/!s/\(.*$\)/\#\1/
}

# Uncomment desktop configurations
/# Desktop/,/^$/{
        /# Desktop/!s/#//
}

It’s a fairly straightforward substitution: anything between the start of the Laptop section to the first blank line gets subjected to the code. Using a branch to nowhere (using a branch without a label in Sed means it skips to the end of the script), I make sure to skip the title (I want to keep that a comment), and to skip the blank line (just to keep it looking neat). Then, a basic regex takes every line that doesn’t start with a comment and replaces those lines with a # and the line. Lastly, we do the inverse in the Desktop section: we simply replace the # character with nothing in all lines except the title. Since we’re only removing a character, there’s no need to filter out anything else.

Because I have two scripts, one for the move to desktop and one to laptop, I made a small bash script to switch between them:

#!/bin/bash
# Bash script to switch over i3wm configs

computer=

# Back up file, using \cp to avoid interactive alias
\cp config -f config.old

# Catch potential errors
if [ $# != 1 ]
        then echo "Error: please provide one argument."; exit 1
fi

# Check what file to change
case $1 in
    -d|--desktop) computer=desktop;;
    -l|--laptop) computer=laptop;;
    *) echo "Select either -d, --desktop, -l, or --laptop"; exit 1
esac

echo "Swapping configuration over to $computer."

# Implement the change using sed
if [ $computer == desktop ]
        then sed -i -f desktop.sed config
        else sed -i -f laptop.sed config
fi

This way, I don’t have to take the individual steps myself, and just use the one command to take care of it. Plus, if one day I figure out a way to check if the config file has changed from a previous version, I could use this to automate an adjustment. For now, I’m just happy I figured out a practical means of automating something I used to have to do manually each time I edited the configuration file.

Crimping Cable

Today was another step in the process of making this apartment ours: networking. We have fibre optic internet, so a theoretical 1Gbit network for our ISP; in reality, of course, we get somewhere between 7-800Gbit, but that’s still pretty great. However, up to this point, we had a ratty WiFi connection that only got us about 5-60Mbit—quite unsatisfactory. The harrowing tale of getting the Internet hooked up is for another day (possibly October 31st, given the tale), but for now I’m just happy that the first set is done.

In total, there’s a couple of stages to this project: drawing a cable from the router in the living room to the switch in the living room; installing an ethernet wall outlet there; drawing it through the bedroom, installing outlets on the other side; installing the office switch and hooking up the PCs; and, finally, drawing a cable from the living room switch to the media player switch, and connecting all the Internet media to that switch.

Now, to be clear: I’ve never done something like this before in my life. That is to say, I’ve built a network out of switches and cables, but all the cables were pre-cut, and I never put in a wall outlet before. So, today was a fun experience learning a new skill. I crimped a single cat6 cable, quite nervous about making mistakes (as the videos on it always emphasize how you need to push the cables through, and if you have a slight issue here or there, you’ll lose out on performance!). Fortunately, cutting it down to size was a breeze with the kit I bought, and crimping a new connector on it turned out to be the simplest thing ever. In fact, tacking the cable to the plinth in the living room turned out to be more of a challenge than crimping that cable was!

It’s the first of many cables, so I’m looking forward to continuing the process over the next week.

Gross Keyboard Cleaning

Every now and then, you just need to really clean your keyboard. I snack at my desk (something I really shouldn’t do with a mechanical keyboard), so every once in a while mine really needs a clean. I figured at least some of you must like watching gross stuff like this, right? In case you’re creeped out by that, just go ahead and skip this one, because there’s pictures. Lots of them.