serhii.net

In the middle of the desert you can say anything you want

10 May 2021

Day 860

Qutebrowser crashing - again

Ubuntu 18.04, qutebrowser etc, as usual. What helped was creating the environment with these options:

python3 scripts/mkvenv.py --pyqt-version 5.14

jq | less zsh alias

Should’ve done this a long time ago:

lq() {
    jq . "$1" -C | less
}

kitty terminal copy url

From config; I should use them more.

# Select a filename and copy it 
map kitty_mod+p>c kitten hints --type path --program @
#: Select a path/filename and open it with the default open program.
map kitty_mod+p>o kitten hints --type line --program -

update-alternatives & installing another gcc

Nicely described: How to switch between multiple GCC and G++ compiler versions on Ubuntu 20.04 LTS Focal Fossa - LinuxConfig.org

# install stuff
$ sudo apt -y install gcc-7 g++-7 gcc-8 g++-8 gcc-9 g++-9
# Add it to update-alternatives
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-7 7
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-7 7
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-8 8
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-8 8
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-9 9
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-9 9

# choose the default one
$ sudo update-alternatives --config gcc
There are 3 choices for the alternative gcc (providing /usr/bin/gcc).

  Selection    Path            Priority   Status
------------------------------------------------------------
  0            /usr/bin/gcc-9   9         auto mode
  1            /usr/bin/gcc-7   7         manual mode
* 2            /usr/bin/gcc-8   8         manual mode
  3            /usr/bin/gcc-9   9         manual mode
Press  to keep the current choice[*], or type selection number:

From the docs: --install link name path priority

Python pip

Editable installations (pip install -e .) are a thing. TODO - learn more about them.

Qutebrowser config - adding bindings for tabs 20-30

Given that the standard ones are not enough for me, and even my additional ones for 10-20 are not enough, added a third level:

config.bind('1', 'tab-focus 1')
config.bind('2', 'tab-focus 2')
config.bind('3', 'tab-focus 3')
config.bind('4', 'tab-focus 4')
config.bind('5', 'tab-focus 5')
config.bind('6', 'tab-focus 6')
config.bind('7', 'tab-focus 7')
config.bind('8', 'tab-focus 8')
config.bind('9', 'tab-focus 9')
config.bind('0', 'tab-focus 10')
config.bind('<Alt-1>', 'tab-focus 11')
config.bind('<Alt-2>', 'tab-focus 12')
config.bind('<Alt-3>', 'tab-focus 13')
config.bind('<Alt-4>', 'tab-focus 14')
config.bind('<Alt-5>', 'tab-focus 15')
config.bind('<Alt-6>', 'tab-focus 16')
config.bind('<Alt-7>', 'tab-focus 17')
config.bind('<Alt-8>', 'tab-focus 18')
config.bind('<Alt-9>', 'tab-focus 19')
config.bind('<Alt-0>', 'tab-focus 20')
config.bind('<Alt-Ctrl-1>', 'tab-focus 21')
config.bind('<Alt-Ctrl-2>', 'tab-focus 22')
config.bind('<Alt-Ctrl-3>', 'tab-focus 23')
config.bind('<Alt-Ctrl-4>', 'tab-focus 24')
config.bind('<Alt-Ctrl-5>', 'tab-focus 25')
config.bind('<Alt-Ctrl-6>', 'tab-focus 26')
config.bind('<Alt-Ctrl-7>', 'tab-focus 27')
config.bind('<Alt-Ctrl-8>', 'tab-focus 28')
config.bind('<Alt-Ctrl-9>', 'tab-focus 29')
config.bind('<Alt-Ctrl-0>', 'tab-focus -1')

EDIT: Actually, to think of it, in for a penny, in for a pound!

for i in range(30, 60):
    config.bind(','+str(i), 'tab-focus '+str(i))

Takes about 9 seconds to :config-source everything, but then works like a charm! And doesn’t seem to make anything else slower (strangely, even startup is as usual).

pycharm can parse markdown!

Opened a README.md, and see it being rendered nicely to the left. I can also edit it directly. Wow.

Website with references / cheat sheets for a lot of CLI programs

sed Cheat Sheet - very down-to-earth, “praxisnah”, I like it. Except for the idiotic scrolling override animations

jq basics - again

jq Cheat Sheet

  • I should use ' for the filter, " for any string elements inside it

  • select

    • Get full record if it matches something
    • jq '.results[] | select(.name == "John") | {age}' # Get age for 'John'
  • Value VS key-value

    • jq '.something' gets the content of fields something removing the key
    • jq '. | {something}' gets key-value of something
    • Sample:
$ jq '. | select(.tokens[0]=="Tel") | .tokens[]' mvs.json
"Tel"
":"
$ jq '. | select(.tokens[0]=="Tel") | .tokens' mvs.json
[
  "Tel",
  ":"
]
$ jq '. | select(.tokens[0]=="Tel") | {tokens}' mvs.json
{
  "tokens": [
    "Tel",
    ":"
  ]
}
  • |keys to extract keys only

jq Cheet Sheet · GitHub also nice TIl that you don’t need jq '. | keys', jq 'keys' etc is enough.

  • `‘del(.tokens)’ to delete a key
  • Indexing works like in Python, say jq '.[-2:]'
  • 'sort_by(.foo)'

I think now I’m ready for the holy of holies: jq 1.4 Manual

  • {user, title: .titles[]} will return an array of {user, title} for each value inside .titles[]!
  • Putting ()s around an expression means it’ll be evaluated. {(.user): .titles} will use the value of the key user!
$  jq '. | {(.id): .id}' mvs.json
{
  "7574": "7574"
}
  • Putting values inside strings with \(foo)
$ echo "[1,2,3]" | jq '"A string \(.)"'
"A string [1,2,3]"

It’s basically synonymous to python3’s f"My f-{string}"

  • '.a=23' will produce an output with .a being set to 23. Will be created if not there.
    • No “change” is being done, the actual value is the same; .a in the same filter after a comma will still return the old value.
  • |= will “update” the value by running its previous value through the expression:
$ echo '{"one": 23,"two":2}' | jq '.one|=(. | tostring)'
{
  "one": "23",
  "two": 2
}
  • slurp mode - instead of returning objects, return a list of objects! For more ‘correct’ json.

Python JSON parser + jq compact mode

It didn’t read the jq-generated multi-line output without commas between items, but jq compact mode does one record (without comma and not as part of an array) per line, and this gets parsed correctly!

JQ compact mode is jq -c '.' sth.json

Before:

{
  "id": "7575",
  "ner_tags": [
    "6",
    "6"
  ],
  "tokens": [
    "Tel",
    ":"
  ]
}

After:

{"id":"7575","ner_tags":["6","6"],"tokens":["Tel",":"]}

Linux - creating a directory accessible to multiple users via a group

How to Create a Shared Directory for All Users in Linux

# Create the group
$sudo groupadd project 
# Add user to this group
$sudo usermod -a -G project theuser
# Change the group of the directory
$ sudo chgrp -R project /var/www/reports/
# Turn on the `setGID` bit, so newly created subfiles inherit the same group as the directory
# And rwxrwx-rx
$ sudo chmod -R 2775 /var/www/reports/
Nel mezzo del deserto posso dire tutto quello che voglio.