Mac Terminal Basics & Commands for Clojure

Everything you need to get started, all in one place

ClojureHub
4 min readDec 8, 2021

Here are some basic commands for the terminal that may prove useful. All this information and more is available online. I personally created this list to use as a quick reference. It’s not comprehensive but covers most of the basics. In the upcoming sections, I also list all specific commands necessary at each step of the way. If you would like to see a comprehensive list of everything to know if you’re starting to learn web apps with Clojure, you can check

If you cannot find the terminal window in Mac, just hover your mouse at the top of the screen to see the top menu bar. Then, find the magnifying glass and click on it. From there, you can type in “terminal” and the correct application will show up. Then, when you have it open, you can set it to stay in the dock for easy access.

Basic commands

help — Gives help about whatever command you want more information about. For example tree —help.

pwd — View the current working directory, stands for Print Working Directory.

cd — Change Directory. For example, if you create a new project, you will want to run the command cd my-project to change your working directory to be that of the new project.

touch — Creates a new file. Example: touch core.clj creates a new file called core.clj within your current working directory.

open — Opens a file in the default application for that type of file. For example, open core.clj will open the core.clj file using Emacs if you’ve set Emacs to be the default text editor for files with the .clj extension.

open . — Opens up your working directory.

mkdir — Make a new directory. For example, if you were starting a project from scratch, you might want to run the command mkdir new-project.

ls — Lists the files in your working directory.

tree — Shows the file tree of your working directory. Careful, this can be really long if you are working with a big project.

tree -L n — Only descends n levels deep in the directories. For example, tree -L 1 shows only the first level. This is useful if you only want to see the top level or top and second levels of your file tree, or so on and not all the files.

sudo — Sometimes you’ll see this command being used, especially if you are debugging an error with the terminal and you search for answers on what to do online. It basically means you’re overriding any user privileges to make changes as a superuser on the computer.

Specific to using Postgres

psql

psql — Opens a psql shell in the terminal so that you can use psql terminal commands with your Postgres database.

psql my_database — Opens a psql shell in the terminal, specifically in the database my_database.

CREATE USER cool_user WITH PASSWORD ‘coolpassword';

In this example, you create a new user with a password. Notice the semi-colon ; at the end. With psql, you need to end each statement with a semicolon for it to be evaluated.

CREATE DATABASE cool_database WITH OWNER cool_user;

In this example, you create a new database and assign it the owner you created with the previous command.

SELECT * FROM table

This command shows you everything from a particular table. It can be helpful to see if your migrations are working correctly and see if all the columns show up that are supposed to.

\d — Shows the list of relations in the database of the shell you are in.

\q — Quits the psql shell.

Clojure commands

lein commands

lein run — Technically this calls the -main function in the namespace specified as :main in project.clj. For beginner purposes, it essentially takes care of the build and run processes so that we can see our app in localhost. For more info see https://www.flyingmachinestudios.com/programming/how-clojure-babies-are-made-lein-run/

lein serve — This is like lein run, except it’ll also automatically pull up your app in localhost:3000 (or whatever port of your choosing) in your default browser. I noticed this works with Cryogen but not necessarily with Luminus.

lein repl — Starts a repl (Read-Eval-Print-Loop) in the terminal.

lein new app my-app — Creates a new app using the lein template called my-app.

lein new luminus cool-app

Creates a new app using the luminus template.

lein new luminus cool-app-2 +shadow-cljs +postgres +http-kit

Creates a new app using the luminus template, and adds shadow-cljs, postgres and http-kit. Note that there are many other options that can be added, this just happens to be what I’ve seen the most commonly used in beginner and intermediate level tutorials.

shadow-cljs commands

npm install — installs the node modules necessary for Clojurescript.

npm install --legacy-peer-deps — I’ve had to use this command before, if there are slightly different versions required by a dependency. It seems to be best practice to just resolve those issues and make sure the correct version is consistently listed, however in a hurry, I’ve used this command without any issues.

npx shadow-cljs compile app

Compiles the clojurescript files.

npx shadow-cljs watch app

Compiles the clojurescript files and watches for changes, updating automatically any time a change is made in the .cljs files.

Conclusion

This list is not comprehensive. Please let me know if there is anything missing you’d like to see here or have questions about. Questions? Let me know! I’m eager to help.

--

--

ClojureHub

Follow us for beginner Clojure web app tutorials & content. See more at clojurehub.com!