Today I Learned

Quick calculation in NVIM.

Wondering how to evaluate a math expression in nvim quickly?

Just type := (2+5)*10-20 and hit enter!

That’s it!

How to create custom OSX startup script?

If you wondering how you can create a script that lunch things on startup?

Create a file ~/Library/LaunchAgents/com.user.startup.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple Computer//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
  <dict>
    <key>Label</key>
    <string>com.user.startup</string>
    <key>ProgramArguments</key>
    <array>
      <string>sh</string>
      <string>-c</string>
      <string>"$HOME/.config/startup.sh"</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
  </dict>
</plist>

And your script file ~/.config/startup.sh:

#!/bin/bash

# run what you want

Run launchctl load -w ~/Library/LaunchAgents/com.user.startup.plist

That’s it!

How to replace text in files?

Wondering how you can replace text content in every file using regex?

Just create and alias using your shell. Im my case it will be fish alias:

Add to your ~/.config/fish/config.fish this line:

alias replace "perl -p -i -e $argv"

and here you go!

~ $ replace 's/hello/goobye/g' *.md

BTW! Hope you have perl installed. I have ;)

How to Quick Solve a Git Conflict?

When you are in the conflct state simply type:

# git checkout --ours .
# git add .

to keep all your changes, or:

# git checkout --theirs .
# git add .

to do the opposite.

How to Back to Previously Checked Git Branch

Right after dealing with master you can easily back to your previous branch with:

$ git checkout master
$ git checkout -

Can't Compile Rust Code to asmjs/wasm

When following error brakes your compilation to asmjs/wasm:

 = note: shared:ERROR: fastcomp is not compatible with wasm object ...

Just update emstcripten, and install upstream compiler.

$ emsdk install latest-upstream
$ emsdk activate latest-upstream

Do Not Eclose Markdown Content Into <p> Tag

If you are using for example Gatsby like I do, don’t enlose html from markdown into <p> tag. The content of <p> will be missing in some cases. Anyway<p> should not include any block elements such as <p>.

Instead of:

<p className="article-post" dangerouslySetInnerHTML={{ __html: html }} />

use:

<div className="article-post" dangerouslySetInnerHTML={{ __html: html }} />

How to Rotate Screen in QT5 QML?

QML Code:

import QtWebEngine 1.5
import QtQuick 2.10
import QtQuick.Layouts 1.3

Item {
  rotation: 90

  StackLayout {
    width: parent.height
    height: parent.width
    x: (parent.width - parent.height) / 2
    y: -(parent.width - parent.height) / 2
    Widget {}
    Widget {}
  }
}

How to Get State of Any GenServer in Elixir?

To get state of any process in erlang/elixir use :sys.get_state/1

By name:

:sys.get_state(MyGenServer)

By pid:

:sys.get_state(pid)

How to Make SSH Service Discoverable in Elixir Using mDNS?

To make service discoverable we need following services registered:

[
  # create domain for an ip
  %Mdns.Server.Service{domain: "somedomain.local", data: :ip, ttl: 450, type: :a},

  # make service discoverable
  %Mdns.Server.Service{domain: "_services._dns-sd._udp.local",data: "_ssh._tcp.local",ttl: 4500, type: :ptr},

  # register ssh service
  %Mdns.Server.Service{domain: "_ssh._tcp.local",data: "SOME NAME._ssh._tcp.local",ttl: 4500, type: :ptr},

  # point service to our domain and port (22)
  %Mdns.Server.Service{domain: "SOME NAME._ssh._tcp.local",data: {0,0,22, 'somedomain.local'},ttl: 4500,type: :srv},

  # empty txt service (some tools expext that)
  %Mdns.Server.Service{domain: "SOME NAME._ssh._tcp.local",data: [],ttl: 4500,type: :txt})
] |> Enum.each(&Mdns.Server.add_service/1)

Free PubSub in Elixir Phoenix Application

If you need a pubsub, to connect LiveViews for example just use YourAppWeb.Endpoint.

YourAppWeb.Endpoint.subscribe("topic")
YourAppWeb.Endpoint.broadcast("topic", "event", %{data: "data"})
def handle_info(%{event: "event", topic: "topic", payload: payload}) do
  # Do whatever you want
end

How to Quick Escape SSH Session?

Just type: [enter]~.

Yes, Enter, tilde and dot!