NUS Hackers Wiki
NUS Hackers Wiki
  • NUS Hackers Wiki
  • Hackerschool
    • Virtual Machines and Linux
    • Beginners' Guide to the Terminal
      • Introduction to the Terminal
      • Modern Shell Tools
      • Shell Scripting
      • Real World Scripting
      • Resources
    • Self-Hosting: Three Easy Pieces
      • 1. Setting up your server
      • 2. Running Services
      • 3. Monitoring your server
    • Vim
    • Introduction to Zig
      • Language Basics
      • Error Handling
      • Memory Management
      • Working with C
      • Exploring comptime
    • CI/CD with Github Actions
      • Background
      • Basics of Github Actions
        • Target workflow
        • Running unit tests
        • Linting code
        • Deploying to Github Pages
      • Advanced use cases
        • Pollers
        • Github script
        • Executing third-party scripts
        • Reusable workflows
      • Cookbook
    • Lightning Git
      • Git Concepts
      • Getting Started with Git
      • Making your first commit
      • Branching
      • Merge Conflicts
      • Integrating remote repositories
      • Collaborative Workflows
      • Commit Manipulation and Reflog
      • Interactive rebasing
      • filter-repo
  • Orbital
    • JavaScript
      • Browser Developer Tools
      • Getting Started
      • Datatypes
      • Operators and Operations
      • Loops and Conditions
      • Functions
      • Strings
      • Arrays
      • HTML
        • Getting Started
        • Tag Attributes
        • HTML Forms
        • Browser Inspector
      • CSS
        • Selectors
        • Colors in CSS
        • Measurements in CSS
        • The Box Model
        • Adding Styles - Part 1
        • Adding Styles - Part 2
      • Working with the DOM
        • Querying the DOM - Selectors
        • Querying the DOM - Element Attributes
        • Querying the DOM - Element Styles
        • Events with JS and HTML
        • Exercise: Click Counter
        • Editing the DOM
        • Fetch Requests
        • Exercise: The NUSMods API
    • React
      • Setup
      • State
    • React Native
      • Setup
      • Intro to JSX
      • Basic Syntax
      • Handling UI
      • Props
      • State Management
    • Git
      • Setup
      • Command Glossary
      • Fundamental Concepts
        • Getting Started
        • Integrating Remote Repositories
        • Branching
        • Merge Conflicts
      • Collaborative Workflows
        • Fork and PR Workflow
        • Branch and PR Workflow
      • Advanced Concepts
        • Ignoring Files
        • Commit Message Conventions
        • Github Collaborators
        • CI/CD with Github Actions
        • Advanced Git Commands
      • FAQ
    • Telegram Bot
      • Creating a TeleBot
      • API Calls
      • Telebot Basics
      • Integrating API's
    • Relational Database
      • Database Overview
      • Database Design
      • Entity Relationship Diagram
      • SQL Basics & PostgreSQL
    • TypeScript
      • Types and Interfaces
      • Utility Types
      • Typing Component Props, Events, and Hooks
      • Why You Should Avoid Using any (and What to Do Instead)
      • TypeScript Tricks You’ll Use All the Time in React
Powered by GitBook
On this page
  • Shell Tools
  • Finding out how to use commands and installing them
  • Finding Files
  • Finding code or text
  • Fast directory navigation
Edit on GitHub
Export as PDF
  1. Hackerschool
  2. Beginners' Guide to the Terminal

Modern Shell Tools

PreviousIntroduction to the TerminalNextShell Scripting

Last updated 8 months ago

Shell Tools

All these are quite nice if you're working with a bare shell, or an uncustomized shell. However, in this day and age, there's a lot of new commands and tools to enhance your terminal experiences.

Finding out how to use commands and installing them

Before we get started with tools, we need to know how to install them and also how to learn what they do. For commands, we can often pass in flags to tell the program how we want it to run. One of the universal flags is the --help flag.

chun@legion:~$ cat --help

To install a program, we often times use what is known as a package manager. This allows us to search and install packages without having to googling the tool and trying to find the correct downloadable file

If you're on Linux or WSL, you should have a package manager installed. If you're on Ubuntu/Debian-based distros, this should be apt. If you're on anything else, you should try and figure out what the package manager is based on your distro.

If you're on MacOS, you'll need to install brew:

Now to install a program, you can just do:

You will need to update the package lists before installing a package!

sudo apt-get update
sudo apt-get install <package-name>
brew install <package-name>

Finding Files

sudo apt-get install fzf
brew install fzf

Once installed, do:

fzf stands for fuzzy finder. It allows you to find anything with a fuzzy search (you can make spelling errors). If you just run fzf, it will do a fuzzy find on your current directory

fzf

But we can do so much more than that! To do so, we need to add some keybindings by running this command:

eval "$(fzf --bash)"

Now try out these new keybindings:

  • CTRL-T - Paste the selected files and directories onto the command-line

  • CTRL-R - Paste the selected command from history onto the command-line

  • ALT-C - cd into the selected directory

Now notice that if you quit the terminal or start a new terminal, these keybindings won't be available. To make the change permanent, we need to save it into a config file. For bash, this config file is is at ~/.bashrc

nano ~/.bashrc

## Inside the editor, add this line
eval "$(fzf --bash)"

Finding code or text

So we can find specific files and directories, but what about finding specific contents within a file? ripgrep is a program that aims to solve this.

sudo apt-get install ripgrep
brew install ripgrep
# Find all python files where I used the requests library
rg -t py 'import requests'
# Find all files (including hidden files) without a shebang line
rg -u --files-without-match "^#\!"
# Find all matches of foo and print the following 5 lines
rg foo -A 5
# Print statistics of matches (# of matched lines and files )
rg --stats PATTERN

Fast directory navigation

It is quite troublesome so jump around directories, especially if you're copying something, or the path is really, really, really long. A good way around this is to have a program guess what directory you want to jump to based on keywords and frequency of which directory you jump to! That is exactly what zoxide does.

curl -sSfL https://raw.githubusercontent.com/ajeetdsouza/zoxide/main/install.sh | sh
brew install zoxide

You can use z to jump to directories, similar to how you use cd. For example, if you have a directory you frequently go to, like:

 /home/user/downloads/temp_dir/funny_project

You could do something like z funny to jump into it.

If all you want to do is learn how to use the terminal, you can just stop here! The next section goes above an beyond and talks about how you can build your own scripts and commands which you can then run.

LogoHomebrewHomebrew
https://github.com/junegunn/fzf
https://github.com/ibraheemdev/modern-unix
https://github.com/ajeetdsouza/zoxide
https://github.com/BurntSushi/ripgrep