The Missing Semester of Your CS Education
Topic 1: The Shell
The $
tells you that you are not the root user.
If you want to provide an argument that contains spaces or other special characters (e.g., a directory named “My Photos”), you can either quote the argument with '
or "
("My Photos"
), or escape just the relevant characters with \
(My\ Photos
).
One thing you need to be root in order to do is writing to the sysfs
file system mounted under /sys
. sysfs
exposes a number of kernel parameters as files, so that you can easily reconfigure the kernel on the fly without specialized tools. Note that sysfs does not exist on Windows or macOS.
Operations like |
, >
, and <
are done by the shell, not by the individual program. echo
and friends do not “know” about |
.
|
|
It’s helpful to know that #
starts a comment in Bash, and !
has a special meaning even within double-quoted ("
) strings. Bash treats single-quoted strings ('
) differently: they will do the trick in this case.
Shell Scripting
To assign variables in bash, use the syntax foo=bar
and access the value of the variable with $foo
. Note that foo = bar
will not work since it is interpreted as calling the foo
program with arguments =
and bar
.
Strings in bash can be defined with '
and "
delimiters, but they are not equivalent. Strings delimited with '
are literal strings and will not substitute variable values whereas "
delimited strings will.
|
|
Unlike other scripting languages, bash uses a variety of special variables to refer to arguments, error codes, and other relevant variables.
-
$0
- Name of the script -
$1
to$9
- Arguments to the script.$1
is the first argument and so on. -
$@
- All the arguments -
$#
- Number of arguments -
$?
- Return code of the previous command -
$$
- Process identification number (PID) for the current script -
!!
- Entire last command, including arguments. A common pattern is to execute a command only for it to fail due to missing permissions; you can quickly re-execute the command with sudo by doingsudo !!
学CS学了这么久,才知道这个快速恢复上一个命令的快捷方式。遇到很多次permissions denied都是手动敲sudo和剩下命令,的确是够蠢🙃。
-
$_
- Last argument from the last command. If you are in an interactive shell, you can also quickly get this value by typingEsc
followed by.
orAlt+.
diff <(ls foo) <(ls bar)
will show differences between files in dirs foo
and bar
.
shell globbing
- Wildcards - Whenever you want to perform some sort of wildcard matching, you can use
?
and*
to match one or any amount of characters respectively. - Curly braces
{}
- Whenever you have a common substring in a series of commands, you can use curly braces for bash to expand this automatically. This comes in very handy when moving or converting files.
|
|
shell Tools
Finding how to use commands
Sometimes manpages can provide overly detailed descriptions of the commands, making it hard to decipher what flags/syntax to use for common use cases. TLDR pages are a nifty complementary solution that focuses on giving example use cases of a command so you can quickly figure out which options to use. For instance, I find myself referring back to the tldr pages for tar
and ffmpeg
way more often than the manpages.
|
|
TLDR 展示也太清晰了!
Finding files
|
|
|
|
Finding shell commands
If we want to search there we can pipe that output to grep
and search for patterns. history | grep find
will print commands that contain the substring “find”.
In most shells, you can make use of Ctrl+R
to perform backwards search through your history. After pressing Ctrl+R
, you can type a substring you want to match for commands in your history. As you keep pressing it, you will cycle through the matches in your history. This can also be enabled with the UP/DOWN arrows in zsh. A nice addition on top of Ctrl+R
comes with using fzf bindings. fzf
is a general-purpose fuzzy finder that can be used with many commands. Here it is used to fuzzily match through your history and present results in a convenient and visually pleasing manner.
Editors (Vim)
Vim has multiple operating modes.
- Normal: for moving around a file and making edits
- Insert: for inserting text
- Replace: for replacing text
- Visual (plain, line, or block): for selecting blocks of text
- Command-line: for running a command
You change modes by pressing <ESC>
(the escape key) to switch from any mode back to Normal mode. From Normal mode, enter Insert mode with i
, Replace mode with R
, Visual mode with v
, Visual Line mode with V
, Visual Block mode with <C-v>
(Ctrl-V, sometimes also written ^V
), and Command-line mode with :
.
Basics
Buffers, tabs, and windows
Vim maintains a set of open files, called “buffers”. A Vim session has a number of tabs, each of which has a number of windows (split panes). Each window shows a single buffer. Unlike other programs you are familiar with, like web browsers, there is not a 1-to-1 correspondence between buffers and windows; windows are merely views. A given buffer may be open in multiple windows, even within the same tab. This can be quite handy, for example, to view two different parts of a file at the same time.
By default, Vim opens with a single tab, which contains a single window.
Movement
You should spend most of your time in Normal mode, using movement commands to navigate the buffer. Movements in Vim are also called “nouns”, because they refer to chunks of text.
-
Basic movement:
hjkl
(left, down, up, right) -
Words:
w
(next word),b
(beginning of word),e
(end of word) -
Lines:
0
(beginning of line),^
(first non-blank character),$
(end of line) -
Screen:
H
(top of screen),M
(middle of screen),L
(bottom of screen) -
Scroll:
Ctrl-u
(up),Ctrl-d
(down) -
File:
gg
(beginning of file),G
(end of file) -
Line numbers:
:{number}<CR>
or{number}G
(line {number}) -
Misc:
%
(corresponding item) -
Search:
/{regex}
,n
/N
for navigating matches
Edits
-
i
enter Insert mode- but for manipulating/deleting text, want to use something more than backspace
-
o
/O
insert line below / above -
d{motion}
delete {motion}- e.g.
dw
is delete word,d$
is delete to end of line,d0
is delete to beginning of line
- e.g.
-
c{motion}
change {motion}- e.g.
cw
is change word - like
d{motion}
followed byi
- e.g.
-
x
delete character (equal dodl
) -
s
substitute character (equal tocl
) -
Visual mode + manipulation
- select text,
d
to delete it orc
to change it
- select text,
-
u
to undo,<C-r>
to redo -
y
to copy / “yank” (some other commands liked
also copy) -
p
to paste -
Lots more to learn: e.g.
~
flips the case of a character