Vim command line mode

The vim command-line mode is the mode we enter when typing Ex commands (:), search patterns (/, ?) or filter commands (!). This mode works a little like insert mode, in that whatever we type, is going to appear in the command line. It is not as powerful as the normal mode, but there are a few combinations we can use to move more efficiently:

  • Ctrl+left, Ctrl+right - Using the arrow keys we can move left and right one character at a time. If we press Ctrl together with the left or right keys, we will move one word at a time
  • Ctrl+B, Ctrl+E - Move to the beginning and end of the command line, respectively
  • Ctrl+W - Deletes the word before the cursor (Only deletes characters at the left of the cursor)
Read More

The Vim statusline

Vim allows us to customize the statusline shown at the bottom of each window. To toggle the statusline visibility, the laststatus option can be used. It can be set to any of these values:

1
2
3
0: never
1: only if there are at least two windows (default)
2: always

If we want to always show the statusline, we can use this command:

1
set laststatus=2
Read More

Writing a vim plugin - Grepfrut

I use grep.vim to grep in multiple files within vim. It usually works pretty well, but I recently needed a little more freedom in how I specify the files in which I want to search, and found that it can’t do what I want. I looked at the code and found that modifying it to do what I want would be hard with the current design, so I decided to create my own plugin.

The plugin I’m going to create is going to be very simple. It is going to provide simple greping functionality with a similar UI to grep.vim, but will allow more freedom on filtering which files to search.

Read More

Introduction to assembly - Assembling a program

I have been working on learning C++ for some time now. I can write and read most code, but there are still a lot of things I don’t understand. One thing that I have noticed about good C++ developers is that they usually know a lot about compilers and the operating system in which they are working. Following their example, I’m going to try to learn about those subjects too.

I’m writing an article about assembly because I have found in some occasions, C++ code being explained in reference to the generated assembly code. Although I had an assembly class in college, I don’t really rememeber anything, so I will have to start from the bottom.

In this article I’m going to be using the x64 (also known as x86-64) architecture, since it’s the architecture most commonly used by modern home computers and servers (for example Intel Core i7).

Read More

Load bash preferences when SSH to a host

I have a laptop computer where I customize my bash using a .bashrc file. Whenever I SSH to a remote host, I always find myself trying to use aliases or other functionality that I have set on my laptop, but they are not there. Today I found a little trick that I can use to copy my .bashrc configuration to a remote host, so I can feel at home.

What we need to do is copy our .bashrc file to the host we are going to SSH to. Something like this would work:

1
scp ~/.bashrc user@host:/tmp/.my-bashrc
Read More

Lock hierarchies to avoid deadlocks

In a previous post I explained how mutexes work and the problem of race conditions.

In this post I introduce another common problem with mutexes, which is deadlocks. Deadlocks are situations when a program can’t make any progress because it is waiting for a mutex that will never be available. This might sound stupid, but it’s something that actually happens very often.

A naive example could be this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <thread>
#include <mutex>

std::mutex mutexA;
std::mutex mutexB;

void doSomething() {
  std::lock_guard<std::mutex> gA(mutexA);
  std::this_thread::sleep_for(std::chrono::seconds(1));
  std::lock_guard<std::mutex> gB(mutexB);
}

void doSomethingElse() {
  std::lock_guard<std::mutex> gB(mutexB);
  std::this_thread::sleep_for(std::chrono::seconds(1));
  std::lock_guard<std::mutex> gA(mutexA);
}

int main() {
  std::thread t1(doSomething);
  std::thread t2(doSomethingElse);

  t1.join();
  t2.join();
}
Read More

Aggregate initialization in C++

I previously wrote an article about variable initialization in C++, but sadly, initialization of variables in C++ is a complicated subject with a lot of options.

Aggregate classes

Before talking about aggregate initialization, we need to know what an aggregate is. Aggregate classes have these properties:

  • All its data members are public
  • Doesn’t define any constructors
  • Doesn’t have virtual functions
  • Doesn’t inherit from any class
  • Doesn’t have any in-class initializers

An example could be:

1
2
3
4
5
class Person {
 public:
  std::string name;
  int age;
};
Read More

AVL Trees

I wrote an article about Binary Search Trees a few weeks ago. AVL trees are a specialization of Binary Search Trees.

AVL trees (named after their inventors, Adelson-Velskii and Landis) were the first kind of self-balancing tree to be invented, so their implementation is somewhat simple compared to newer self-balancing trees.

This type of tree allows you to perform insertions, deletions and searches in O(log n). This tree keeps track of the heights of all the nodes. Every time one node is inserted or deleted, the balancing factor (difference between the heights of left and right subtree) of its ancestors is checked. If the balancing factor is greater than 1 or lower than -1, then the tree is rebalanced.

Read More

Spell and Grammar checking on vim

I recently moved my blog to Jekyll, which means I now write my posts in my favorite editor. One problem I encountered is that vim doesn’t check my spelling by default, which means I probably had a lot of mistakes in the last posts I wrote.

Because I prefer people thinking I know how to write, I decided to look for a tool that would help me with this.

Installation

The first thing I had to do was install Java Runtime:

1
sudo apt install default-jre
Read More