28 KiB
Anki: 2021
- Finding devices
$PATH
environment- Testing systemd timestamps
- Enabling desktop integration
- Flatpak permissions
- The basics of Flatpak apps
- Changing user shell
- Printing a list in the shell
- systemd timestamp example
- Type checking in Bash
- Emacs: The overview of buffers
- The basics of Emacs modes
- Emacs: Eagle's eye view of a window
- Emacs: Point and marker
- Interacting with buffers in Elisp
- String comparison in Emacs Lisp
- Using the help system in Emacs
- Emacs: Word manipulation
- Emacs: Line manipulation
- Vim: Jump to previous jump point
- Vim: Enter jump point
- Vim: Go to file path
- Set as a pager
- Vim: Show outline/table of content
- Detect files through Vim filetypes
- Going to keyword definitions in Vim
- Vim modes
- Word wrapping in Vim
- Using the help system
- Pitfalls and illusions of competence
- org-babel
- org-babel modes
- Creating files with Org mode
- Dynamic content with Org mode
- Org mode: Asciidoctor-styled callouts
- Org mode: Timestamps and durations
- Org mode: Deadlines and schedules
- Org mode: Quick file navigation
- Editing source code blocks in Org mode documents
- Practices for studying
- Focused and diffused mode
- Benefits of sleep
- Bash conditional with an unset variable
- Bash conditional with a set variable
- Shell help system
- Basic music notation reading 1
- Basic music notation reading 2
- Basic music notation reading 3
- Watch the logs of a systemd unit
- Restoring a file in Git
- Setting environment variables with systemd environment directive
- Emacs Lisp conditionals
- Finding out file types in the shell
My collection of tips and tricks I collected in 2021.
Finding devices
Front
How to list devices information? (I mean all sorts of devices.)
Back
lspci
lists PCI devices.lsusb
list USB-connected devices.lsblk
list the block devices which usually includes storage drives and such.
$PATH
environment
Front
How does a shell find the binaries?
Back
Most shell searches through the $PATH
variable, a colon-delimited list of paths containing the binaries.
Testing systemd timestamps
Front
How to test out systemd timestamps?
Back
systemd-analyze {calendar,timestamp,timespan}
To know how the format (i.e., calendar, timestamp, and timespan) looks like, you can refer to man systemd.time.5
.
Enabling desktop integration
Front
How to make desktop environments recognize the desktop files?
Back
Most of the desktop environments and certain applications like Rofi refers to the XDG_DATA_DIRS
environment variable, a list of colon-delimited paths similar to PATH
.
This enables desktop integration with certain tools like Nix and Guix package manager. Here's how to integrate installed Nix packages into the desktop.
XDG_DATA_DIRS=$HOME/.nix-profile/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
Flatpak permissions
Front
What permissions does user-installed Flatpak apps have by default?
Back
By default, they have none. Among the default limitations:
- They can only access their own runtime folder
$HOME/.var/app/${FLATPAK_APP_ID}
. - They cannot access the network.
Some apps are installed with the request to allow the following permissions enabled (e.g., Zotero).
If left with no permissions, you'll see in certain situations like a file browser dialog that the permissions is in effect. Below are some of the examples interacting with the permissions of an app.
# Show the permissions of an app.
flatpak permission-show ${FLATPAK_APP_ID}
# Let the user-installed Flatpak app access the home directory.
flatpak override --user --filesystem=home ${FLATPAK_APP_ID}
The basics of Flatpak apps
Front
What is a Flatpak package? Does it have its own form of managing dependencies?
Back
A Flatpak package can either be a runtime or a standalone app.
Runtimes are the basic dependencies of an application. Only select packages available as a runtime (e.g., Qt, GTK).
Flatpak has its set of runtimes composed of system libraries to be used with the applications. Thus, it stays out of its way with the operating system's libraries. The developer can also bundle its own set of libraries.
Changing user shell
Front
How to change user shell in most Linux distros?
Back
chsh
Printing a list in the shell
Front
Give some ways to print a list.
Back
# Format the string.
printf "%s\n" foo bar 'baz ;;'
# Echo the string as-is.
echo "foo
bar
baz ;;"
# Print with escaped newlines.
echo -e "foo\nbar\nbaz ;;"
foo bar baz ;; foo bar baz ;; foo bar baz ;;
systemd timestamp example
Front
What is *-*-8/4
in systemd calendar format?
Back
Every 4 days, starting from the 8th of the month.
Assuming this was executed on 2021-05-12.
systemd-analyze calendar *-*-8/4
Original form: *-*-8/4 Normalized form: *-*-08/4 00:00:00 Next elapse: Sun 2021-05-16 00:00:00 PST (in UTC): Sat 2021-05-15 16:00:00 UTC From now: 3 days left
Type checking in Bash
Front
Implement a little bit of type checking in Bash.
Back
printf
throws an error if the given argument is not appropriate.
# This is fine
printf "%d\n" 532 429 40 102
# This will return a non-zero exit code
printf "%d\n" this_will_throw_an_error 430 that_previous_number_will_not
# This is also fine
printf "%f\n" 10 43.45 3.14
532 429 40 102 0 430 0 10.000000 43.450000 3.140000
Emacs: The overview of buffers
Front
What is an Emacs buffer?
Back
A buffer is anything that Emacs displays.
It usually display file contents among other examples with butterfly
, doctor
, or the starting buffer when you first open Emacs.
While buffers usually have an associated file path, a buffer doesn't need one. This is one of the concepts that is applied to other text editors (Vim, Atom, Visual Studio Code).
The basics of Emacs modes
Front
What is a mode in Emacs?
Back
A mode is set of behavior quite similar to Vim modes.
Emacs further divides modes into two.
Major modes are Emacs' way of supporting programming languages and file formats.
Programming language support usually comes in major mode — e.g., R-mode
for R files, python-mode
for Python scripts, org-mode
for Org mode documents.
Think of them as an equivalent to Vim's filetype.
Only one major mode can be activated in one buffer at a time and all buffers have a major mode.
Minor modes usually contain little behavioral changes that improve the editing experience. When enabled, some of them are global modes — affecting every buffer in your session. Others are only buffer-local — affecting only the buffer when you activated the mode. Unlike major modes, multiple minor modes can be enabled at any given time.
Emacs: Eagle's eye view of a window
Front
What is considered window in Emacs?
Back
A window is where the buffers are being displayed. One window can display all buffers but only one at a time. To display two buffers at a single time, just add another window.
All windows display the same buffer; if the buffer is modified in one of the window, it will show the changes in all windows.
Emacs: Point and marker
Front
What is a point and a marker in Emacs?
Back
A point is the current location of the cursor in the buffer.
You can get the point with point
function.
Often helpful for interacting with buffers.
A marker is another point in the buffer. It is usually found when interacting with regions when asked for the two points (i.e., the beginning and the ending position). Furthermore, a marker can be used to save locations and jump back to that marker when asked.
Interacting with buffers in Elisp
Front
Give a rough example on how to do the following in Emacs Lisp:
Create a temporary buffer named "hello" containing an Org mode document with a "Hello world" entry.
Back
(with-temp-buffer
(insert "* Hello world")
(rename-buffer "hello")
(org-mode))
String comparison in Emacs Lisp
Front
How to compare two strings in Emacs Lisp?
Back
(string= STR1 STR2)
(print (string= "WHOA" "whoa"))
(print (string= "WHOA" (upcase "whoa")))
(print (string= "Hello world" "HeLL0 World"))
nil t nil
Using the help system in Emacs
Front
Name different ways to use the help system inside Emacs.
Back
help-for-help
is the most comprehensive help section (in my opinion).describe-*
series of functions are the next. Among the list of describe functions, you have:describe-key
,describe-function
,describe-variable
, anddescribe-package
. You can just open up the minibuffer and see what else is there.apropos
is similar to Unix apropos command which searches for every symbol in Emacs.
Emacs: Word manipulation
Front
Give some functions on word manipulations inside Emacs.
Back
The following functions have multiple variations each for a character ($F-char
), word ($F-word
), region ($F-region
), and region or point ($F-dwim
).
capitalize-*
for making the first of the word in uppercase.downcase-*
for making a region all lowercase.upcase-*
for making a region all uppercase.
evil-mode has a keybinding associated with uppercase and downcase a certain region with evil-upcase
and evil-downcase
, respectively.
Emacs: Line manipulation
Front
Give some functions on manipulating lines while in Emacs.
Back
- evil-mode has
evil-join
which works the same way Vim's join complete with smart spacing and everything. fill-region
is useful for formatting requirements/preferences like in the Linux kernel where the maximum width of 80 characters. evil-mode has an associated keybinding function withevil-fill
.sort-lines
is pretty useful for the common task of sorting lines. Though, not useful for items that consist of multiple lines.
Vim: Jump to previous jump point
Front
In Vim, how to get back in previous jump point?
Back
Ctrl + O
Remember it as getting out of the current jump point and back to the previous one.
Vim: Enter jump point
Front
How to jump into a keyword with Vim?
Back
Ctrl + ]
will enter into the definition block of a keyword.K
also has the same effect.
Vim: Go to file path
Front
In Vim, how to go to the file path at point?
Back
gf
(in normal mode) as in goto file.
Set as a pager
Front
How to set Vim as a manpager?
Back
MANPAGER="nvim +Man!"
+Man!
is a command invocation (as if entering :Man
inside Vim).
It can also be written as -c Man!
.
The :Man!
command displays the current buffer as a manual page.
Vim: Show outline/table of content
Front
How to show the table of contents of a document in Vim?
Back
gO
, although the results are filetype-specific (and some don't have any).
Helpful examples include for manual pages and help pages (from :help
).
Detect files through Vim filetypes
Front
How does Vim detect the files?
Back
Vim guesses the file by assigning filetypes, mainly through the file name and reading the file content. A filetype is how Vim knows what plugins to apply to the current buffer.
Vim has a few built-in filetypes such as shell, manual pages, Markdown, Asciidoc, xmodmap, patch files, and JSON among others (that are in $VIMRUNTIME/filetype.vim
).
For more information, run :help filetype
inside Vim.
Going to keyword definitions in Vim
Front
How to go to the keyword definition in Vim?
Back
gd
as in go to definition.
Though, not all the time it will do what its supposed to do.
For better effect, you can generate a Ctags file which Vim has a built-in integration (see :h ctags
).
Vim modes
Front
What is a mode (in Vim)?
Back
A Vim mode is a set of behavior and actions. In this case, it considers editing and navigation (among other modes) to be separate. Thus, you need to switch between them to do those things.
Vim has built-in modes which you can see with :h vim-modes
.
Word wrapping in Vim
Front
How to do word wrapping (in Vim)?
Back
gw
as in go format the words.
By default, it simply line wraps the lines with the 80-character limitation.
Using the help system
Front
How to effectively make use of the help system of Vim?
Back
The usual way is to execute :h
or :help
.
You can view what does a keybinding do with :h ${KEYBINDING}
— e.g., :h gw
to know what gw
does, :h V
for viewing visual line mode.
For keybindings in visual and command line mode, prepend them with v_
and c_
, respectively.
Pitfalls and illusions of competence
Front
Give various pitfalls and illusions of competence to look out when learning.
Back
- The presence of the material itself can cause students to foolishly think they already know about the subject.
- Similarly, studying with solutions can be a trap if you focus on the what and how rather than the why.
- Various common practices such as highlighting, rereading, and mind mapping are not as effective and only applicable in specific situations.
- The einstellung mindset, being invested in an idea that you can't see other solutions.
- Similarly, overlearning can occur if you're aiming for complete mastery when you should move on after understanding the concept.
org-babel
Front
What makes Org mode popular for reproducible research?
Back
org-babel, the library that enables superpowers for Org mode source code blocks.
Among the list of features, org-babel makes the following things easier for creating lab notebooks.
- Execute the source code block and print results.
- Create files from source code blocks, making it possible to create an entire computational report with a single Org mode document.
- Metaprogramming with noweb-inspired system making dynamic content possible.
- Individual control over source code blocks with sessions, export options, and variables.
- Pass values between different source code blocks even in different programming languages.
org-babel modes
Front
How to make org-babel pass values between different source code blocks?
Back
First, configure org-babel to work in functional mode (i.e., :results value
) in a source code block.
With functional mode, it will return values which will be handled by org-babel.
#+name: num
#+begin_src python :results value
return 53
#+end_src
The value cannot be passed unless it has a name that others can reference yet so add a name property to the source code block (i.e., #+name: ${NAME}
).
Now here's a different source code block written in a different language.
To pass a value, you have to configure with :var ${VARNAME}=${NAME}
.
#+begin_src ruby :var num=num :results output
print(num)
#+end_src
Creating files with Org mode
Front
How to create files with Org mode source code blocks?
Back
The :tangle
option enables extracting code blocks into files.
Accepted values include yes
, no
, or a relative path to the Org document where the file will be written.
Dynamic content with Org mode
Front
Is creating dynamic content in Org mode possible? If so, how?
Back
Yes!
By attaching a name to a code block and evaluating them, it is possible.
Here's an example of a source code block with a default argument.
#+name: greeting
#+header: :var name="World"
#+begin_src sh
echo "Hello ${name}"
#+end_src
You can then call the function in different ways:
- For calling it inline,
call_${FUNC_NAME}()
. - For creating a block,
#+call: ${FUNC_NAME}()
. - For invoking inside a code block,
<<${FUNC_NAME}()>>
, but you have to enable noweb (e.g.,:noweb yes
).
You can then pass header arguments by appending in square brackets ([]
) before invoking it — e.g., call_greeting[:results replaces]()
, #+call: greeting[:results replace]()
, <<greeting[:results replace]()>>
.
Org mode: Asciidoctor-styled callouts
Front
Are Asciidoctor-style callouts possible in Org mode? If so, how?
Back
Surprisingly, yes! It is just hidden on the documentation. Specifically, on the Literal examples section of the Org mode manual.
Here's an example to do it.
#+begin_src python
print("Hello world") # (ref:hello)
print(2 + 5) # (ref:num)
#+end_src
In [[(hello)][line 1]], we have printed the traditional "Hello world" program.
In [[(num)][the second line]], we've done a simple arithmetic and printed it into the console.
To create Asciidoctor-styled callouts, create a reference inside of the code block and refer to it (i.e., (${ref})
).
Org mode: Timestamps and durations
Front
How to denote timestamps and durations in Org mode?
Back
# A timestamp looks like this.
<2021-05-07 Fri>
# To make a duration, just put two dashes between two timestamps.
<2021-05-07 Fri>--<2021-05-08 Sat>
To make creating timestamps easier, execute org-time-stamp
(or whatever keybinding you've set).
Org mode: Deadlines and schedules
Front
How to make deadlines and schedules in Org mode entries?
Back
Just prepend the keywords DEADLINE
and SCHEDULED
, respectively.
DEADLINE: <2021-06-30 Wed>
SCHEDULED: <2021-06-29 Tue>
Org mode: Quick file navigation
Front
Give some ways how to navigate Org mode documents quickly.
Back
- I don't need to explain what
org-babel-next-src-block
andorg-babel-previous-src-block
does. org-backward-heading-same-level
is the same asorg-forward-heading-same-level
but moves one headline backwards.org-forward-heading-same-level
moves one headline forward in the same level. Useful for navigating sections and subsections.org-goto
creates an interface for showing the outline and it is a great navigation function. Highly recommend to use it with a completion interface (e.g.,counsel-org-goto
,counsel-org-imenu
).org-num-mode
adds a (non-persistent) counter to the document. Very helpful in navigating larger files.org-sort
will sort the entries into your preferred criteria. It also works on a list of items which is very useful if one of the list items has more than one line.
Editing source code blocks in Org mode documents
Front
What function creates a buffer for certain elements in org-mode but it is especially useful for editing source code blocks where it will open with the correct major mode?
Back
org-edit-special
Practices for studying
Front
Give various practices for studying effectively.
Back
- Recalling is one of the more effective practices compared to rereading or highlighting. Self-testing is one of the better strategies, overall.
- Prefer spaced repetition over cramming as scientifically, the learning process takes some time to settle.
- Get the key ideas ahead and as you're studying, fill the details. This includes skimming — reading through the chapter, looking at the keywords.
- Practice interleaving your studies — that is, studying other subjects and/or moving to later topics as you understand the topic.
- Focus, understand, and practice. Learning can occur bottom-up (learning the details of a problem) and top-down (learning the bigger picture of a topic). Using both creates context and that's where you put your understanding to the test as you learn when to apply what you've learn.
- Use memory palace technique — that is, to create analogies, narratives, and mnemonics.
- Have efficient amount of sleep. Sleep has certain processes that helps our brain like removing toxins in our brain that accumulate when we're awake, eliminating less relevant neural structures in favor of strengthening stronger ones for tomorrow, and thinking of a solution of the thing you worry about.
Focused and diffused mode
Front
What is focused and diffused mode? When those two modes often found in certain situations?
Back
Focused mode is when you are in the middle of a mentally intensive task — e.g., cooking, writing, reading, studying. In this process, you gather all of the information that are immediately required to complete it.
Diffused mode is the state of mental relaxation — e.g., taking a walk, watching a movie, recess and lunchtime. This is when the brain takes the focused task into the background and let random thoughts pass by. This is the reason why we sometimes get a sudden realization (a Eureka! moment).
Benefits of sleep
Front
How does sleep help in the learning process?
Back
Sleep has certain processes that helps our brain.
- Removing toxins in our brain that accumulate when we're awake.
- Eliminating less relevant neural structures in favor of strengthening stronger ones for tomorrow.
- Thinking of a solution in the background of the thing you worry about.
Bash conditional with an unset variable
Front
In Bash, what does ${HOME:-"/home/me"}
means?
Back
If $HOME
is unset, substitute with the given value (i.e., /home/me
).
Bash conditional with a set variable
Front
In Bash, what does ${HOME:+"/home/me"}
means?
Back
If $HOME
has a value, then return the given value (i.e., /home/me
).
Shell help system
Front
Give some ways to make use of the help system in the shell (or most Unix environment).
Back
- help sections (i.e.,
--help
or-h
) - manual pages (i.e.,
man
) - TexInfo (i.e.,
info
) - tldr pages (i.e., with clients like tealdeer)
Basic music notation reading 1
Front
What notes are displayed (in order)?
<<lilypond-paper-config>>
{
c a' f b
}
Back
C A F B
Basic music notation reading 2
Front
What notes are displayed (in order)?
<<lilypond-paper-config>>
{
\chordmode { c1 }
\relative c' { e f a }
}
Back A
C major triad (i.e., C E G). Then the notes E, F, and A.
Basic music notation reading 3
Front
What notes are displayed (in order)?
<<lilypond-paper-config>>
{
c''1 d' <c' a'> <e' b'> <f'' g'>
}
Back
C, D, C-A (a major sixth), E-B (a minor fifth), and G-F (a seventh).
Watch the logs of a systemd unit
Front
What is happening with the following command?
journalctl -u backup -fb
Back
Watch the logs from the systemd unit backup.service
starting from boot time.
The command could also be written in the same way.
journalctl --unit backup --follow --boot
Restoring a file in Git
Front
How to restore a file in Git?
Back
git restore $FILE_PATH
Despite the name, it will also delete the file if it's detected as untracked file in the Git worktree.
Setting environment variables with systemd environment directive
Front
How to set environment variables with systemd?
Back
Create an environment directive file in the specified directory.
You can find the search paths and the syntax from environment.d.5
manual page.
Basically, it is a shell-like config for setting environment variables. The following shows that.
TERM=alacritty
EDITOR=nvim
BROWSER=brave
MANPAGER="nvim +Man!"
PATH=${PATH:+$PATH:}${GUIX_PROFILE:-$HOME/.guix-profile}/bin
XDG_DATA_DIRS=${GUIX_PROFILE:-$HOME/.guix-profile}/share:${XDG_DATA_DIRS:-/usr/local/share:/usr/share}
Compared to exporting values with the user shell (e.g., .bashrc
, .zshrc
) where they are only applied when the shell is called, environment variables generated from the directive can be recognized from applications that called non-interactively (e.g., GNOME desktop search, Rofi).
You can also view the resulting environment with systemctl show-environment
.
Emacs Lisp conditionals
Front
Give some functions for creating a conditional in Emacs Lisp.
Back
Below are the common way to control the flow with Elisp.
(if (and nil nil)
"Hello there!"
"Not hello there, sith lord!")
(unless t
"Not hello there, sith lord!")
(when t
"Hello there!")
Finding out file types in the shell
Front
Give some ways how to show the file type in the shell.
Back
file --mime-type $FILE
xdg-mime query filetype $FILE
I prefer file --mime-type
since it is clearer on purpose and name alone.