TIL about recursive macros in vim. As the name implies, it consists of calling your macro within the macro your are building. Pre-requisites:

  1. Having a clear register (qa -> q to clear register a)
  2. Having a break point through t or f if we don’t want to run on all lines

Suppose we have the following file:

 1  abc,123
 2  bcd,4
 3  cde,54321
 4  def,88

And we want the end result to be:

hash["abc"] = 123
hash["bcd"] = 4
hash["cde"] = 54321
hash["def"] = 88

In order to build our recursive macro:

qa Start macro
02cw Go to beginning of line and change two words
hash"[<esc>  Insert hash[" characters and exit normal mode
f, Find the next , character
i"] = <esc> Insert "] = characters and exit normal mode
lx Move left and delete a character (the , in this case)
j Go to the line below
@a Call your macro. This creates the recursion
q Finish macro

After that calling @a will change every line.

TIL about the flock system call which applies or removes an advisory lock on an open file. The thing to pay attention is the word advisory, other processes can ignore this lock if they wish to do so. This syscall will lock the entire file, which can be pretty bad for highly concurrent systems.

For more information: man 2 flock

For a more fine grained locking mechanism: man 2 fcntl

TIL about the FromStr trait in Rust that is implicitly used through str’s parse method.

We could have a struct called Period that parses a string like: “10 days” into Period { value: 10, timeframe: "days" }. The way to use it would be:

let period = "10 days".parse::<Period>();

TIL about the term cookie tossing, an attack leverages the feature that a subdomain can put a key=value pair in a cookie which can then also be read by the domain above it. For example, todayilearned.dev doesn’t know if this key comes from it or from cookie.todayilearned.dev.

This becomes problematic when a website allows untrusted people to host subdomains under its domain. They can then abuse this feature to write manipulated cookie information.

TIL about Count-Min Sketch, an algorithm used to answer questions like:

  • What’s the frequency of our samples?
  • What’s our most frequent samples?

Similar to bloom filters, it uses k distinct hash functions. Every observed value serves as an input for these functions and the output for each function is a number corresponding to a bucket.After finding the buckets we just need to increment a counter in each of these buckets.

In order to retrieve the frequency of an observed value we find the buckets for this value and return the minimum counter among all the buckets: min(bucket_x, bucket_y, bucket_z)

A short version the paper can be found here.

TIL about :scriptnames in order to get a list of scripts that vim has loaded. It’s pretty useful since we can infer if any of our scripts are being overridden by another one. We can also run this command as scr

TIL that Spotlight allows us to make calculations, get exchange rates and even the weather. Let’s see a few examples:

  • ceil(), floor() or sqrt()
  • 100USD in CAD
  • Weather Montreal

TIL about configuring a shortcut in iTerm2 to delete a word.

  1. Open the preferences (⌘ + ,) and go to the Keys tab.
  2. Add a shortcut key
  3. In the Action dropdown, select Send Hex Code and input 0x17

TIL about the status bar on iTerm2. It’s pretty useful with a mix of builtin components like Current Directory or git state and user defined variables or functions. To access and configure the status bar go to Preferences > Profiles > Session. Turn on Status bar enabled then click Configure Status Bar.

Again, scripting plays a huge part in how useful the status bar is.

As an example:

# In ~/.bash_profile
function iterm2_print_user_vars() {
  iterm2_set_user_var rubyVersion $(ruby -v | awk '{ print $2 }')

And the variable can be read inside a Custom Action > Configure Component > Configure Action through \(user.rubyVersion).

TIL that curl --data and curl --data-binary when used with a @filename outputs a different result. The most common one being that --data will strip newlines from the file but --data-binary will not.

Copied to clipboard