Moving faster around the terminal

Published at Jul 14, 2024 - 09:48 AM

#Linux#Shell Improvements#Hotkeys

Contents

Why would we want to move faster?

One of the first questions I imagine is 'why?'. In this case I work in penetration testing. It's an industry dominated with tools that are all built for the terminal. impacket, ffuf, nxc all of these tools require you to use your chosen shell. Why, then, should we learn to go faster:

  • We spend hours upon hours inside the terminal
  • We need to be precise when running commands
  • It looks really cool

I like to think that the shell is a bike. It gets us from point A to B. You can pedal a lot, and get there, no matter what. I'd just like to give some pointers on how to use the gears :p.

Tricks

Movements

The first thing we want to learn to go faster in a shell is how to move around more quickly. To start this process, let's learn how to get to the front and back of our current input:

  • ctrl + a - Start of line
  • ctrl + e - Start of line

Pretty handy! but moving around inside our input can be a little bit more annoying, which is why I prefer to add this to my shells config file:

bindkey "^[[1;5C" forward-word
bindkey "^[[1;5D" backward-word

Now we can jump around a lot easier:

  • ctrl + left - back one word
  • ctrl + right - forward one word

Modifications

Alright, we have an easy way to move around, but how about deleting or changing the contents? Well, let's learn some cool hotkeys:

  • ctrl + k - Delete from cursor to end of line
  • ctrl + u - In bash, delete from cursor to start of line. In zsh, delete line.
  • ctrl + w - Delete last word
  • alt ​ + d - Delete word
  • ctrl + l - Same as running the command clear
  • ctrl + y - Did you just delete something using a hotkey? This pastes it back in!

I do really want to mention that the final hotkey there, ctrl + y, is incredibly useful.

Okay, now we want to get a previous command we've run before. The default implementation of ctrl + r works… but it's not too useful. Let's fix that by installing either:

  • Atuin (Full command history db and search tool)
  • fzf (fuzzy finder that can be used to search the shell history)

For Atuin, I prefer up arrow behaving normally in my shell, so I turn off that Atuin feature in my shell's config file:

eval "$(atuin init zsh --disable-up-arrow)

Want to locate files?

  • locate — Search the entire disk, pretty quick (cause it needs to build a db of disk locations)

Well how about when I'm just tying commands? Try out using fzf to open a fuzzy finder when looking for files:

  • cat **<TAB>

You can even improve the speed of this search by installing the rust tool fd and configuring fzf in your shell config:

FZF_CTRL_T_COMMAND='fd --type f --hidden --exclude .git --exclude .cache'
FZF_ALT_C_COMMAND='fd --type d --hidden --exclude .git'

Some more cool tools

This is a short list of tools I'd recommend you investigate before next racing off into the terminal:

  • Atuin - Cool searches + your shell history won't vanish
  • Zoxide - I'm not a fan personally, but it's pretty cool
  • ripgrep - Just use rg instead of grep, you won't regret it
  • csvlens - Finally, I can look at csvs in the terminal

Conclusion

Well I hope you can take away at least one new hotkey from this, and happy hacking!

f3rn0s © 2024