🚀 LAUNCHING A THING TODAY 🚀
My friend @ellane and I are pleased to launch a new mini-course:
🧑💻 Ellane learns the command line 🧑💻
She mentioned this to me a while ago, so we're going to do it live on Mastodon. Here, in this thread. We're thinking of it like a correspondence course.
You can't fit that much in a post, and formatting here isn't great, so the full posts will be on our mini blog at:
commandline.johnnydecimal.com.
We'd love you to follow along.
Ellane and Johnny have a conversation about the command line.commandline.johnnydecimal.com
@Ellane W
My theory was just that it was a GUI (Graphical User Interface) element. Part of the Terminal app, but not actually part of the command line.
I didn't even think about interrupting a running process 😆
@Johnny ‘Decimal’ Noble
Hmm no, this is not normal.
Here's a little video. I type out `printenv`, hit return, see the result, hit return a few times, do it again. This is what should happen.
You're saying after your first screen of results the window becomes unresponsive?
Try typing vs. pasting, though that shouldn't matter.
Hmm okay let's just keep an eye on that. If it happens again, let me know.
Preparing my response as we speak! Should be there tomorrow.
06. The structure of a command @ellane
Commands are all structured in the same way. They all follow one of a few patterns. When you learn those patterns, you learn the syntax of _all commands_.
We know that `pie lunch cook` isn't a valid sentence, even if we're not a chef.
And soon you'll immediately recognise that `--food pie &meal lunch cook` couldn't ever be a valid command: it just doesn't _look_ right.
commandline.johnnydecimal.com/…
Our first look at the fundamental structure of a command.commandline.johnnydecimal.com
I had no idea what `view` did, by the way. Had to type it to see, at which point I realised it was a synonym for `vim`.
You learn something every day…
And you did the right thing hitting the red button aka the ejector seat. 👍
08. File paths @ellane
File paths are core to this whole experience: many things you do involve a file, or being in a specific folder before you take an action. So let's go a bit deeper.
This lesson introduces ~, ., .., and /. Tiny symbols but they mean so much!
We also talk about relative vs. absolute file paths.
commandline.johnnydecimal.com/…
How do we get around the file system if the folder we want isn't a direct subfolder of where we are?commandline.johnnydecimal.com
use quotes. If you do something like :
$ ls "My
[tab]When you press the tab, it will complete the name like :
My folder
My file
Etc ...
10. Escape arguments, completely.
By trying to `cd` to a folder with a space in it, you've accidentally given that command two arguments.
So let's talk about **arguments** and what they are. Then I'll show you two ways to get round this problem without renaming_every_folder:
1. Escaping with `\`.
2. Tab autocompletion.
The first is something you need to know. The second is something you'll actually use!
commandline.johnnydecimal.com/…
The CLI doesn't handle spaces in file names very well. We use 'escaping' to work around it.commandline.johnnydecimal.com
Look at this one more closely!
It should be `this\that` — the same backslash that you use for the escape character.
The puzzle is: how do you include the escape character in an argument _if it’s the escape character?_
12. Case-sensitivity, ttys, and the prompt.
There’s a bit of feedback here, so I’ve made one post addressing these questions. And in the next post we’ll continue with the concepts.
Case-sensitivity: is your macOS case-sensitive? (A: kinda.)
ttys000: what is it? (A: 1800s technology.)
% vs. $: why? (A: style. 🕺🏽)
13. Flags and options.
We almost understand the syntax of a command. To recap, we've met the command, and its (often optional) arguments.
Now let's meet flags and options, which are two sides of the same coin. They modify the _behaviour_ of a command.
commandline.johnnydecimal.com/…
The final piece of the command puzzle, we introduce 'flags' and 'options'.commandline.johnnydecimal.com
Hi both, just wanted to say thank you for this open and transparent communication about this learning experience!
I work in IT for some time and its not only a great reminder on how things work but also how many things are logical and others are ambiguous and sometimes confusing. 🙏😌
15. Less is more.
Let's learn how to view plain text files. Do you have any of those handy?
Oh wait.
---
Then we'll use this same skill to explore the system's built-in 'manual pages', which are more up-to-date than the web link I sent you last time.
These pages are a treasure-trove of information. They tell you, in great detail, how to use any command.
jut m'y two cents, when you do :
$ cd a\ dir\ with\ spaces
you can also use double quotes:
$ cd "a dir
[tab]And it will expand it to ;
$ cd "a dir with spaces"
I have a question, which is slightly off-topic:
It looks like you have been less technical person in the past? If yes: How do you look on IT and apps, now that you took the red pill and left the matrix?
Do you think that "apps should just do their magic" or that this will help you achieve more? Do you believe that programming or IT Stuff is much more different that you thought and now want to unlearn about the truth?
Im just curious on your perception. 😌
Thanks for your reply, I fully agree that it will take some more time to fully identify the impact of this exercise.
Needed to ask it early on, it might sit in the back your head so you can tell me closer to the end. 😉🙏
(I started programming as a hobby again after 12 years and I see for myself how the different the world is or how rusty I have become? 😬😅
Lots of feedback! I've done some inline comments and I'll keep the next lesson a separate page for neatness.
Not at all! This is a whole other world here.
In the years running up to 2018 I tried and stalled and tried and failed to start learning some sort of programming language. Started one, stopped. Changed my mind. Started another.
So many times.
The one thing that finally got me was a JavaScript course, sadly now gone*, by a Google engineer. You should have seen how slowly he went.
Pain. Stakingly. Slowly. Like proper baby-steps slowly.
*It was free and now it’s $300/month.
1/5
And that’s what I needed. That was the ah-ha moment.
Now I can do JavaScript, to an okay standard. Nowhere near many people, but good enough for what I need to do.
I still lean on the web. But because I understand the fundamentals, what I find myself needing is the pointer.
I’m in a dark room and you just need to put my hand on the switch. Then I can do the rest myself.
That’s what we’re going for here.
Fundamentals. Deep understanding.
Slowly. Until the light comes on.
2/5
*Also*, it’s my job as the teacher to notice and adapt, but that can be hard as we’re just doing this by text.
So if at any point you think we’re going too quickly, please say so.
If there’s a concept that needs to be reinforced, re-explained, whatever: tell me.
3/5
I just realised something! You have access to Learn Excel with Lucy.
Re-watch ‘Functions & Formulas’, and in pt. 2 pay close attention to the TEXTJOIN function.
_Syntactically_ it’s totally different to the CLI.
_Conceptually_ it’s **identical**.
- What’s the command?
- Does it have flags?
- Does it have options?
- Does it take arguments?
This is the thing about computers, and languages: once you internalise the _idea_, the rest is just translation.
4/5
So the job is to train your brain to think in this really un-human way.
You’re becoming a programmer. The CLI is just a computer to be programmed.
Excel or the CLI. It doesn’t matter.
(For anyone following along, I’ll make that lesson free in the next couple of days and edit this post with a link.)
5/5
Great. Yeah I was going to write a short summary post to reinforce what’s important to focus on and what’s not. I’ll do it here.
Important:
- The basic <command> <flags/options> <argument> idea.
- This is such a massive leap from clicking a mouse.
- We’ll do a LOT more of this over time.
- Viewing a document; getting around inside there.
Not important:
- Any of the specific flags or arguments to any particular command.
- None of the contents of the man pages! Scary nonsense.
Okay, terribly late on this one, and I still haven’t written a proper post for our little site. But I figure if I just type this at the kitchen table rather than agonising over perfection, that’s probably better.
(I will update the site later.)
So we’re going to ‘pivot’, as we say in the tech industry. I set out to teach you ‘the CLI’, but that’s a _massive_ undertaking, and your actual goal is to ‘learn plain text accounting, specifically hledger’.
Rather than possibly useless…
1/x
…theory, therefore, we’re going to focus on the goal, and cover topics _as required_ on our way to that goal.
In your latest blog post you mentioned that you can read stuff, but you can’t yet edit. So let’s do that now.
Now, this qualifies as a ‘potentially dangerous’ command. Because if you edit the wrong file, you could mess something up! So just make sure you’re in the right place, and maybe create some test files to start.
The command 🥁 is 🥁
pico
2/x
That’s it. You can just type `pico` and it’ll open.
And I’m going to leave you there. Because if you look around, I think you’ll work it out. The hints are all on-screen: see the bar along the bottom.
This is a pretty rudimentary text editor. It’s not the legendary `vim`. But it’s easy to use, and it’ll do the job.
And think about this: once this is all set up, there’s no reason to use the CLI to edit your plain text files. They’re just plain text files.
3/x
You can use whatever you like to edit them. And just dive down to the CLI as required.
But still, I think this is a valuable exercise. So have a play with pico, create a few files, edit a few files, and let me know how you get on.
Next, we’re going to go straight for the hledger installation page. 😬
4/4
Okay, we have two possibilities here.
Just like with any other text editor, you can either:
1. Open it ‘empty’, and start typing a new document, or
2. Open it with an existing document.
It never hurts to have a look at the first line of the man page, `man pico`:
`pico [ options ] [ file ]`
So:
- `pico`, then
- some optional [options], then
- optionally, a [file].
Ignoring the optional options, this gives us two paths... which might sound familiar?…
I did it! Pico is more familiar with its ⌃T commands, but you've introduced me to j and k for moving around, and so I've been spoiled with single letter commands!
So… I jumped off the cliff into vim land. And I like it! With :q! under my belt I'm having fun learning how to navigate a doc. #LearnCLI
Ha! What! This is excellent.
There are many, many fine vim tutorials out there. And really once you’ve got the hang of it, ten commands gets you by.
Next stop: Homebrew. 🚏 Give me a day or so. We’re heading to the National Library to work today, it’s our happy focus place.
Okay, let's turn in the direction of hledger. We're going to install it.
First, we need to install Homebrew. But first: what is it?
# Package managers
We're all used to installing software, and keeping that software up to date.
How do you install and keep updated applications for your Terminal? There are a bunch of ways, but Homebrew has become the de-facto standard for the Mac. Because it's just really nice.
So the purpose of Homebrew is just to install hledger. How might we do that?
Homebrew's web site is amazing so we might as well double-check. Head over there and search for hledger using the box at the top. You should find yourself on the formulae page for the app.
In this case, the page tells us exactly what we need to do:
`brew install hledger`
Amazing progress!
So I'm going to throw caution to the wind — because it's Lucy's birthday today and I'm required out back to do chicken-related things — and give you the 'move a file' command.
Be careful with this one, because if you move the wrong file to the wrong place, there's no 'undo' in this world.
The command: `mv`, short for `move`.
And the same command serves as a rename! You just 'move' the file from its old name to its new name.
Check `man mv` but it's pretty obvious.
The equivalent to copy a file is `cp`. Maybe create some dummy stuff on your desktop and mess about there for a bit.
When you're sick of that stuff, `rm` (remove) will delete it.
What happens if you try to remove a whole folder? Can you find the (potentially VERY dangerous) flag that allows you to 'recursively' delete an entire folder?
What is/isn't hledger doing? Is there an error? Or can you post a screenshot?
The goal of this was to get you using hledger so let's focus our efforts there, and what you learn along the way is what you learn.
Nice one!
As a rule, if something's already installed then brew isn't going to want to mess with it. Brew needs to install it so that brew can control it, because files are in different places and were installed in different ways.
So you could remove Firefox and re-install using brew to bring it under its control?
Those SHOUTY THINGS you can ignore.
And you can `brew update` then `brew upgrade` to do a manual upgrade of your installed apps at any time.
Little CLI hint: `history` shows you stuff you've previously typed.
If you want to repeat one of them, `!xx`, where xx is the number before the command, will do that.
Hitting the up-arrow repeatedly will also call back previous commands. There's a nice way to improve this that I'll tell you about later.
You asked what I'd like to learn next, and the main thing is taking the instructions in the hledger docs and applying them to what I've learned so far. I'm struggling a little with what does what.
Now some of these questions will be best put to the hledger forum, but there are some generalities here too. Like recognising when the manual is giving me an argument or a command, and putting it in the right order.
Also, you mentioned showing me how to set 'bookmarks' to save time.
OK, here @ellane asks about Control-E vs Control-E, and @johnnydecimal settles on the side of "not case sensitive."
But in the previous lesson, "Less Is More," in learning how to navigate a text file with search, "To find the next instance of the same search, hit `n`. The previous, `N`."
Isn't that an example of case sensitivity? Or is that a rare exception? Seems akin to "`tab` to move forward, `shift-Tab` to move backward" in many forms... 🤔
@natedunnmi I assumed that code would only recognise the US spelling, color, so that's the one I use.
…Just tried it out, and 'colour' is an unrecognized (not unrecognised) option.
@johnnydecimal
We use Homebrew to install and remove some stuff.commandline.johnnydecimal.com
@natedunnmi Feels like a default that the community has settled on over the years.
Control-[CHARACTER] = not case sensitive, i.e. Ctrl-n doesn't do a different thing to Ctrl-N. Therefore, pick a default, which is to express it as a captial. Maybe that's just clearer?
Whereas 'n' to go forward vs. ‘N' to to back, they're two different commands.
'n' without shift does one thing. 'n' with shift, that is to say ’N’, _does something different_.
Lots of this is just historical decisions…
@natedunnmi Also note how Mac + Windows is explicit about 'Shift'.
Ctrl+E on a Mac, means ‘control and the E key’.
You want a shift in there, you have to say it. 'Ctrl-Shift-E’.
Oh yeah, and look at your menus! Copy = ⌘C. But you don't have to press Shift to get a capital C.
Paste and match style = ⌥⇧⌘V. Needs a Shift, so the Shift is explicit.
Okay let’s get back on this #LearnCLI horse!
Bookmarks. How do you move quickly to another folder?
1. Define an alias
You can create an ‘alias’ which is like a shortcut. You define <some short thing> which does <some longer thing>.
`alias m='cd ~/Movies'` creates an alias, m, which issues the command to take you to your Movies folder.
So you can create yourself an 'hl' alias, say, which changes you to your hledger folder.
Typing `alias` shows you your defined aliases.
1/x
That's great, but you'll notice that these aliases aren't persistent: close the window and it's gone.
To make them persistent, we need to add them to a file which is run whenever a new prompt is launched.
This introduces your first 'dotfile'. So called because it starts with a dot.
Dotfiles are very common in CLI world. They're universally used as configuration files.
Because your system – including the macOS Finder – hides them by default. So they don't get in the way.
2/x
This particular dotfile is called .zshrc, and it lives in your home folder. So – and I'm _so proud_ that you're using vim – let's edit that.
vim ~/.zshrc
You might already have one with stuff in it. If not, you'll just see an empty file. Either way, add your alias as a new line.
Hint: lines starting with # are comments. So you might add a section like this:
# Ellane's aliases
alias hl='cd ~/some/deep/path/to/hledger'
to keep things neat.
Save that, open a new window, and test.
3/x
While we're in .zshrc, here's another quality-of-life improver that I always add to mine.
Normally when you press up-arrow to search through your history, it's just a dumb list of previous commands.
It's far more useful if you can type the start of a command you know you've previously typed, and have up-arrow search your history. To do that, add these lines:
# Enable history substring search
bindkey '^[[A' history-search-backward
bindkey '^[[B' history-search-forward
4/x
And one last hint: rather than having to open a new window every time, you can 'use' your .zshrc in the current window by typing:
source ~/.zshrc
Okay, this is just a quick one but I promise to stick with it now! And what I'll do is let it play out here for a bit, then summarise to the website.
Which, to remind our viewers, is commandline.johnnydecimal.com.
So! Tell me how you're getting on, and what you'd like to do next.
5/5
Ellane and Johnny have a conversation about the command line.commandline.johnnydecimal.com
Oh yeah, and the point of that history thing is that I often find myself typing
cd ~/path/to
and knowing that this is enough to be able to trigger up-arrow, find what I want in history, and away I go.
Can be quicker day-to-day than defining an alias. Although defining the alias is the better thing to do, long-term.
6/5
I'm pleased to see a way to do this inside the CLI. Thus far I've been creating Keyboard Maestro shortcuts to save keystrokes and it works well, but I like that there's an integrated option outside of apps like that.
So I'm in the .zschrc document (which said it was a new doc, BTW), and have entered the path following your pattern. Testing it in a new window did nothing, however, so now we need to figure out what I did wrong.
How I'm getting on is …rather lost!
I've added the lines you suggested for aliases and history search, but don't know how to make them do their thing. (This reminds me that the intuition I've developed for GUIs doesn't apply here—I need different skills.)
What does zsh stand for? If I just see it as 3 letters, it won't stick as a concept. Looking it up on the internet doesn't help as most explainy people think I already know what it *is*, just not how to use it.
And what's the rc for that comes after zsh?
It looks like the next step is helping my CLI to see the .zshrc file.
mupdf is a viewer ...
To create from markdown, you can use pandoc like this :
$ pandoc file.md -o beautiful.pdf
Yes, PDF from Markdown would be good to know how to do but TBH I'm probably not going to use the CLI for that so maybe another topic would be better tackled first.
Where I'm at right now is saving CSV files from hledger with `-o file-name.csv`, and learning more vim. I'm open to suggestions for other things to learn that have an immediate application to this kind of work.
@hyde
for vim if you have a small budget I recommend this book
pragprog.com/titles/dnvim2/pra…
Quickly learn Vim’s core functionality and tackle your trickiest editing and writing tasks to become a more efficient developer--with more than 120 tips.pragprog.com
The book teach VIM a text editor software who allows write and edit text very fast.Internet Archive
It seems odd that the file was created new rather than having already been there. What do you see with
ls -a .zs*
It seems like there should have already been a .zshrc with something in it.
Also, somewhere in the back of my mind is the need to log off and back on for the change to work. Or you can use the command 'source ~/.zshrc' to make the change work I think.
Note: I know nothing about your OS, so I probably should stay out of this. It's just so puzzling.
IIRC modern macOS doesn't come with a .zshrc.
Hmm I wonder ... Ellane, what do you see if you do:
cd ~/.zsh
ls -la
Edit: just realised I have a test user account on this machine that I don't configure. No .zshrc anywhere to be seen. So a blank file was to be expected, Ellane.
Okay so I've got the blank .zshrc file, and have entered my first alias. I'm unclear as to whether the " or ` were supposed to be entered as well. Tried both, neither worked.
I'm seeing zsh: command not found: hl
Try this.
You can see my current aliases there.
I wish you could do syntax highlighting here. It'd be nice to indicate exactly what's a command.
`Backticks`, being used for inline markdown, are often used to delimit the thing to be typed. In almost all cases you should not include them.
Sometimes people (or code blocks on websites) indicate a command with a $ at the front. That represents your prompt and should also not be typed.
(Sometimes when you copy the code block off a website the $ also gets copied! This is wrong.)
There are many different kinds of shells, and most end with "sh" (standing simply for "shell") - like sh, bash, csh, tcsh, ksh, dash, fish etc. As far as I know, the "z" in "zsh" is the initial of someones name.
The "rc" stands for "run commands" (although there are different interpretations) and indicates that it is a configuration file.
Thanks for the vid, I've watched it a few times but don't know what I was seeing. What's lutetium? You typed quotation marks before the cd...?
Sorry I'm not catching on too quickly this time!
Quelquefois c'est frustrant d'essayer d'apprendre une nouvelle langue !
But once you know the vocab, it comes more easily. One piece at a time.
Not sure if you got your answer:
Yes there are quotation marks before the cd and at the end of it. These are around the command that ellane equals.
And on the next screen after saving this, notice that he typed
source .zshrc
That lets the shell see this change you've made. The other option is to log off and then login again.
lutetium is the name of this laptop, so that's just part of my prompt.
The alias I added was:
alias ellane="cd ~/Movies"
(Confusing things by using "double quotes vs. 'single last time. Sorry! But they both work the same.)
Then we exit vim and I 'source' that file to activate the new alias. Or I could have opened another shell.
Because this .zshrc file is run by every new shell on launch. That's what it's for. Hence putting a command in it will make it available in every new shell.
So you'll want to add something like
alias hl="cd /path/to/your/hledger/directory"
Then save, either 'source' the file or launch a new shell, and then
hl
_should_ jump you there.
You can use the `echo` command to test your .zshrc is being executed. `echo` just types out text. Try it:
echo Help I am stuck in the computer!
So you can put an `echo` in your .zshrc and every time you launch a new shell, you should see what you wrote.
01. Introduction & goals
On Mastodon, @ellane mentioned that she'd like to 'learn the command line'. What she told Johnny was that she wants to:
- understand what the command line IS
- learn how not to stuff up your machine
- learn what language it speaks
- understand what commands DO, where things go
- what's Homebrew, and why use it and what does it do, and how does it do it?
#LearnCLI
commandline.johnnydecimal.com/…
Introduction & goals
commandline.johnnydecimal.com02. What is the command line?
A very high-level introduction to the command line. What even is it? Why would we use it?
You can click a button because some designer in Seattle designed the program to have a button.
But what if you want more granular control? What if there isn't a button for the thing you want to do?
Then you crack open the command line.
Over to you, @ellane.
#LearnCLI
commandline.johnnydecimal.com/…
What is the command line?
commandline.johnnydecimal.comThanks, Johnny, this is outrageously exciting to my aspiring-geeky heart!
So I've done my assignment re- layers of abstraction. Makes perfect sense.
Just like my car's ignition button, I'm aware there's an organised, predictable series of events that take place once the button is pushed. Each event relies on those that came before it (pistons, oil, petrol, etc), but I don't have to think of any of that when I'm buckling up.
Computers be no different!
#LearnCLI
04. Let me show you around your new car
@ellane Oh good analogy! That's going to be helpful.
Learning 'the command line' is like learning how to drive. But before we can drive, we need a vehicle.
Let me show you around your new car.
#LearnCLI
commandline.johnnydecimal.com/…
Let me show you around your new car
commandline.johnnydecimal.com