megacolorboy

Abdush Shakoor's Weblog

Writings, experiments & ideas.

Quick Search for file in your workspace in Visual Studio Code

Recently, started using Visual Studio Code as I'm yet figure out a way to install Sublime Text on Void Linux (using it as my current daily driver).

I'm always used to looking for my files using keyboard shortcuts in Sublime Text and I was kind of surprised this feature isn't enabled by default in Visual Studio Code.

Anyway, after a little digging, I found out that adding this line to your settings.json file would allow you to look for files in your workspace:

"search.useIgnoreFiles": false

And, now try Ctrl+P and you'll be able to search for your file(s) easily.

Hope you found this tip useful!

How to install Composer manually?

You may wonder "why not install Composer via apt or yum package manager?" and yes, it can be installed that way too.

But what if you are using old composer packages or you wanted to skip the checks done during the interactive installation routine, this could be the way for you.

I wouldn't really call it an advanced technique or something but if you know what you're doing, then you should be in the right direction.

The below snippet will show you how to install it manually:

wget "https://getcomposer.org/download/VERSION_X.X.X/composer.phar"
sudo mv composer.phar /usr/local/bin/composer
sudo chmod a+x /usr/local/bin/composer

Yes, that's it!

By placing Composer in the /usr/local/bin directory, it will be accessible from any directory within the system and you can run it globally.

To check if you've installed it correctly, just do the following:

composer -V

Hope you found this tip useful!

Prevent VIM from creating swapfiles

I like VIM and use it regularly to write and edit code on a daily basis but I always find the creation of .swp files really annoying.

If you find them annoying too, you disable them temporarily in the editor, like this:

:set noswapfile

Or if you want to disable it permanently, just add this line in your .vimrc file:

set noswapfile

Don't get me wrong, I'm not saying that you should dislike .swp files because if the editor crashes or your computer/server crashes in midway, those files can save your progress.

Hope you found this tip useful!

How to setup rsync with passwordless SSH on UNIX/Linux?

Tired of ensuring if whether each file in every server is synced? Planning on doing automated backups? If so, then this technique should come in handy for you.

Interested? Then follow the steps below:

Check if rsync over SSH works

Before you start, please ensure that you can rsync to your intended server over ssh using a password. With the following example, you can just send a simple file over to the intended server and see if it works or not:

rsync -avz -e ssh test.txt username@REMOTE_SERVER_IP_OR_DOMAIN:/path/to/folder/

Once you execute this command on the terminal, it'll prompt you for a password on the remote server, if it does, then it works.

Generate SSH Keys

If you want to do a passwordless SSH, you need to generate public and private SSH keys on the local server by typing the following command on the terminal:

ssh-keygen
Enter passphrase (empty for no passphrase):
Enter same passphrase again:

If you're prompted to enter a passphrase, just hit Enter and proceed until the key is generated. Once the keys are generated, they'll be in the ~/.ssh directory on your local server.

Copy public key to remote server

Using ssh-copy-id, you can copy the public key to the remote server:

ssh-copy-id -i ~/.ssh/id_rsa.pub REMOTE_SERVER_IP_OR_DOMAIN

Once executed, you'll be prompted to enter the password of the account on the remote server and if successful, the public key will be copied to the remote server and will be stored in it's appropriate location.

Perform Rsync over passwordless SSH

If you've come this far, then you should be able to SSH to the remote server without entering the password:

ssh REMOTE_SERVER_IP_OR_DOMAIN

If it works, then perform the rsync operation again (above) and this time, it shouldn't prompt you to enter any password.

Hope you liked reading this short article!

Render a simple RGB colored image in C++

Recently, I started reading a book called The Graphics Codex, it's an amazing book about Computer Graphics covering a lot of content and I find it quite resourceful. I wanted to build my own toy raytracer for the sake of exploring, so that's why I started to refresh my graphics coding knowledge a bit.

Just like any graphics renderer, in order to view your image. you must be able to write your image to a file, right?

Below, I wrote a really simple code to generate the entire RGB colored spectrum from top to bottom:

#include <iostream>
using namespace std;

int main() {
    const int width = 800;
    const int height = 800;

    std::cout << "P3\n" << width << " " << height << "\n255\n";

    for(int j=height-1; j>=0; j--) {
        for(int i=0; i<width; i++) {
            auto r = double(i) / (width-1);
            auto g = double(j) / (height-1);
            auto b = 0.25;

            int ir = static_cast<int>(255.999 * r);
            int ig = static_cast<int>(255.999 * g);
            int ib = static_cast<int>(255.999 * b);

            std::cout << ir << " " << ig << " " << ib << "\n";
        }
    }
}

You can generate it by simply creating an executable like this:

g++ -o pixels pixels.cpp

Now, when you execute it by typing ./pixels, you'll get a list of numbers that's pretty much a combination and permutation of RGB color values.

Lastly, to generate a colored image, like the one above, just redirect the output to an image format, in this example, I used PPM image format:

./pixels > image.ppm && xdg-open image.ppm

And that's it, you have generated your own colored image! 😁

Hope you found this useful!

Find directories created within a date range

Executing these commands helps me create a sorted list of files/directories created within a specific date range:

touch -t 202104100000 start
touch -t 202104150000 stop
find . -type d -maxdepth 1 -newer start \! -newer stop | sort >> directories.txt

Someday, these commands shall come in handy, bud! 😎

Perform Git operations using path directory

The -C flag means the path of the directory and using this flag, you can perform any Git operations outside the project's directory without having to enter the directory all the time:

git -C /path/to/directory <command>

Hope this tip helps you out!

Exclude directories while searching for a pattern in files

Looking for a specific text pattern in a directory but wanted to avoid some paths? Here's a quick command that you can try:

grep -R --exclude-dir=path/to/directory 'some pattern' /path/to/search

Hope this helps you out!