Written by Krzysztof Slonka
Published February 18, 2016

Confessions of a command line addict

tldr; Set low key repeat interval. Establish one way of editing text, use it everywhere you can. Use fuzzy search, vim, tmux, zsh, fuzzyclipboard. AKA more effective utilization of command line.

Chapter 1 – The disease

Hello, my name is Christopher and I have a couple of confessions to make. The thing you’re about to see makes my skin crawl and makes me want to punch some people in the face…

So you’re typing a command and after hitting enter you discover that you made a mistake. And now the “fun” begins, you press ← and wait… after 10 years you finally get to the character that you wanted. This is not acceptable.

But there is an easy way to fix it! You can change this by setting key interval in the settings:

keyboard-screenshot_edited

 

Or even better this:

defaults write NSGlobalDomain KeyRepeat -int 1

For linux (gnome) it’s:

gsettings set org.gnome.settings-daemon.peripherals.keyboard repeat-interval RATE
gsettings set org.gnome.settings-daemon.peripherals.keyboard delay DELAY

(This is persistent through sleep / awake)

Or using

xset r rate DELAY RATE

where DELAY is delay after the key is repeated – in milliseconds

RATE how many keypresses per second.

This command makes your OS repeat the key you press and hold much faster than the default speed and even faster than you can set it in settings. You can go for 0 instead of 1, but it’s too fast for me. That is all good, but still this is a half-measure. Wouldn’t it be nicer to use 5 keystrokes to jump right to the word you want?

Yeah, that’s better. But did you notice the “[NORMAL]” text appearing on the right (and a cursor moving)? That’s weird right? Some of you may recognize it as a mode from a very popular editor – vim and before you ask any questions: yes, I do have a vim inside my terminal, but we’ll get back to that.

Chapter 2 – Choose your weapon carefully

If you’re using cmd – get out, if you’re using PowerShell – go jump of a roof (you can’t polish a turd), if you’re using bash – you don’t know what you’re missing… But seriously, after a year of using zsh now other shells feel like doing this:

You can find a lot of information about zsh on the net, so I’m just going to list some which blew my mind.

  • files completion – you can write cd and press tab and you see this (image below) and it’s a menu, you can navigate through that

Screen Shot 2016-02-15 at 08.23.34

  • path expansion – you can write cd /u/l/b/ and press tab and have it expanded to /usr/local/bin/
  • right prompt – you can have the info on the right side of the prompt (like I have for which mode I’m in)
  • shared command history – you can have multiple shells open and share history between them
  • environment variable editing – normally you would do: “export PATH=$PATH:something” but what if you want to edit something in the middle? Type “vared PATH” and you get a pretty editor and after you hit enter it is saved in your current session
  • much more via oh-my-zsh – amongs them autocompletion for almost everything (grunt? – no problem you have task autocompletion)

Chapter 3 – The expensive context switch

The problem with editing in multiple IDE’s, editors, command line, browser etc. is that whenever you switch the environment you have to adapt to different keyboard shortcuts what leads to you using shortcuts that are not defined or other irritations. This is why we should have a common editing method that we can use everywhere. This method should allow for some basic stuff in terms of movement like: jump to specific line, jump between words and sentences, scroll half page down, put the cursor in the middle of the screen, search etc.,, and some more advanced stuff like jumping through the history of editing places, code completion. Also we should be able to use this movements in conjunction with editing, i.e. deleting a whole word, replacing a sentence, and so on.

My solution to this problem is using vim everywhere.

y6b9a

Whenever you can, use vim for editing, or install plugins for your favourite editor / IDE. Here is a list of applications and plugins that support vim key bindings:

This should be consistent in all places where you expect some functionality to work. For example you don’t expect code completion in the terminal because you don’t write your code in the terminal. If you want to, you can use a plugin for code completion in code editor but if your shell doesn’t support jumping through words and you’re using it in the context of writing text (which is the same in terminal and in code editor) then you should change the shell. This is the only way to ensure you acquire the habit of using that function.

Chapter 4 – Search

 

fuzzy-search-all-the-things

I search for things all the time, and the way my mind works is that I never remember the exact thing that I’m looking for.

Fuzzy search is an algorithm that finds approximate substring matches inside a given string. That sound a bit complicated, let me give you an example. When I need to modify the header of a website, I remember that the file was called ‘something-header’, it was located in aviews’ or ‘view’ folder and it was in publication specific folder so ‘aftenposten’. That way I create the search string:

aposten view header

Zrzut ekranu 2016-01-31 o 09.57.15

Voilà – we see all the files with highlighted parts that match this pattern.

I not only use it in an editor but also inside zsh using fzf, and the best thing about this plugin is that you can search through history (.bash_history or .zsh_history). I also use this for clipboard management, I wrote a small app that does exactly that. It saves all the stuff you put inside your clipboard and then let’s you search through it using fuzzy search algorithm.

Chapter 5 – Terminals galore

Screen Shot 2016-02-15 at 12.12.39

The other thing that pisses me off royally is juggling the terminal windows. Alt-tabbing or using “exposition” (or some other desktop trick to look through opened windows). But this can be easily fixed by using a tool called tmux. Tmux allows you to create sessions, windows, tabs and then switch between them easily. I use it as a window manager for the terminal and it’s pretty good at that. It has a lot of plugins and a rich ecosystem, but the one plugin that I use the most is tmuxinator, which allows me to script opening windows, panes and programs. This is really useful when you have a project that requires a lot of preparation to start. Opening them every day when you have to start working on a project is a major pain in the butt. It has one more interesting use case – pair programming. You can see the whole tutorial here.

Written by Krzysztof Slonka
Published February 18, 2016