aboutsummaryrefslogtreecommitdiffhomepage
path: root/print.html
diff options
context:
space:
mode:
authorjohannst <johannst@users.noreply.github.com>2023-11-18 11:39:35 +0000
committerjohannst <johannst@users.noreply.github.com>2023-11-18 11:39:35 +0000
commitb0991484624c0ddd4bdd45eb64619144f189b42a (patch)
treee35b8ec8cafe8a92cb0f10e79d841b769c1a4aa9 /print.html
parent5b434ad58ba8cfe4c3b9c87273daa9917cc7ae02 (diff)
downloadnotes-b0991484624c0ddd4bdd45eb64619144f189b42a.tar.gz
notes-b0991484624c0ddd4bdd45eb64619144f189b42a.zip
deploy: 947ca7041270c1a0bf8ac39387e611931f606aa1
Diffstat (limited to 'print.html')
-rw-r--r--print.html142
1 files changed, 112 insertions, 30 deletions
diff --git a/print.html b/print.html
index ebd80f0..640587d 100644
--- a/print.html
+++ b/print.html
@@ -720,10 +720,16 @@ shell restart</li>
-x export to ENV
-u unexport from ENV
</code></pre>
+<h3 id="special-variables-ref"><a class="header" href="#special-variables-ref">Special Variables <a href="https://fishshell.com/docs/current/language.html#special-variables">ref</a></a></h3>
+<pre><code class="language-sh">$status # exit code of last command
+$pipestatus # list of exit codes of pipe chain
+
+$CMD_DURATION # runtime of last command in ms
+</code></pre>
<h3 id="lists"><a class="header" href="#lists">Lists</a></h3>
<p>In <code>fish</code> all variables are lists (start with index <code>1</code>, but lists can't
contain lists.</p>
-<pre><code class="language-text">set foo a b c d
+<pre><code class="language-sh">set foo a b c d
echo $foo[1] # a
echo $foo[-1] # d
@@ -731,40 +737,39 @@ echo $foo[2..3] # b c
echo $foo[1 3] # a c
</code></pre>
<p><code>$</code> can be seen as dereference operator.</p>
-<pre><code class="language-text">set foo a; set a 1337
+<pre><code class="language-sh">set foo a; set a 1337
echo $$foo # outputs 1337
</code></pre>
<p>Cartesian product.</p>
-<pre><code class="language-text">echo file.{h,cc}
+<pre><code class="language-sh">echo file.{h,cc}
# file.h file.cc
echo {a,b}{1,2}
# a1 b1 a2 b2
</code></pre>
-<h3 id="special-variables-lists"><a class="header" href="#special-variables-lists">Special Variables (Lists)</a></h3>
-<pre><code class="language-text">$status # exit code of last command
-$pipestatus # list of exit codes of pipe chain
-
-$CMD_DURATION # runtime of last command in ms
-</code></pre>
-<h4 id="path"><a class="header" href="#path"><code>*PATH</code></a></h4>
+<h4 id="path-ref"><a class="header" href="#path-ref"><code>*PATH</code> <a href="https://fishshell.com/docs/current/language.html#path-variables">ref</a></a></h4>
<p>Lists ending with <code>PATH</code> are automatically split at <code>:</code> when used and joined
-with <code>:</code> when exported to the environment.</p>
-<pre><code class="language-text">set -x BLA_PATH a:b:c:d
-echo $BLA_PATH # a b c d
-env | grep BLA_PATH # BLA_PATH=a:b:c:d
+with <code>:</code> when quoted or exported to the environment.</p>
+<pre><code class="language-sh">set -x BLA_PATH a:b:c:d
+echo $BLA_PATH # a b c d
+echo &quot;$BLA_PATH&quot; # a:b:c:d (quoted)
+env | grep BLA_PATH # BLA_PATH=a:b:c:d (env)
+
+set FOO_PATH x y z
+echo $FOO_PATH # x y z
+echo &quot;$FOO_PATH&quot; # x:y:z
</code></pre>
<h2 id="command-handling"><a class="header" href="#command-handling">Command Handling</a></h2>
-<pre><code class="language-text"># sub-commands are not run in quotes
+<pre><code class="language-sh"># sub-commands are not run in quotes
echo &quot;ls output: &quot;(ls)
</code></pre>
<h3 id="io-redirection-1"><a class="header" href="#io-redirection-1">I/O redirection</a></h3>
-<pre><code class="language-text"># 'noclobber', fail if 'log' already exists
+<pre><code class="language-sh"># 'noclobber', fail if 'log' already exists
echo foo &gt;? log
</code></pre>
<h2 id="control-flow"><a class="header" href="#control-flow">Control Flow</a></h2>
<h3 id="if--else"><a class="header" href="#if--else"><code>if</code> / <code>else</code></a></h3>
-<pre><code class="language-text">if grep foo bar
+<pre><code class="language-sh">if grep foo bar
# do sth
else if grep foobar bar
# do sth else
@@ -773,7 +778,7 @@ else
end
</code></pre>
<h3 id="switch"><a class="header" href="#switch"><code>switch</code></a></h3>
-<pre><code class="language-text">switch (echo foo)
+<pre><code class="language-sh">switch (echo foo)
case 'foo*'
# do start with foo
case bar dudel
@@ -783,18 +788,18 @@ case '*'
end
</code></pre>
<h3 id="while-loop"><a class="header" href="#while-loop"><code>while</code> Loop</a></h3>
-<pre><code class="language-text">while true
+<pre><code class="language-sh">while true
echo foo
end
</code></pre>
<h3 id="for-loop"><a class="header" href="#for-loop"><code>for</code> Loop</a></h3>
-<pre><code class="language-text">for f in (ls)
+<pre><code class="language-sh">for f in (ls)
echo $f
end
</code></pre>
<h2 id="functions"><a class="header" href="#functions">Functions</a></h2>
<p>Function arguments are passed via <code>$argv</code> list.</p>
-<pre><code class="language-text">function fn_foo
+<pre><code class="language-sh">function fn_foo
echo $argv
end
</code></pre>
@@ -805,39 +810,116 @@ the function lazily if found.</p>
<p>This is the preferred way over monolithically defining all functions in a
startup script.</p>
<h3 id="helper"><a class="header" href="#helper">Helper</a></h3>
-<pre><code class="language-text">functions # list al functions
+<pre><code class="language-sh">functions # list al functions
functions foo # describe function 'foo'
functions -e foo # erase function 'foo'
funced foo # edit function 'foo'
# '-e vim' to edit in vim
</code></pre>
+<h3 id="argument-parsing-and-completion"><a class="header" href="#argument-parsing-and-completion">Argument parsing and completion</a></h3>
+<p><code>argparse</code> puts options into variables of name <code>_flag_NAME</code>.</p>
+<p>References:</p>
+<ul>
+<li><a href="https://fishshell.com/docs/current/language.html#argument-handling">Argument Handling</a></li>
+<li><a href="https://fishshell.com/docs/current/cmds/argparse.html"><code>argparse</code></a></li>
+<li><a href="https://fishshell.com/docs/current/completions.html">Writing your own completions</a></li>
+<li><a href="https://fishshell.com/docs/current/cmds/complete.html"><code>complete</code></a></li>
+</ul>
+<pre><code class="language-sh">function moose --d &quot;my moose fn&quot;
+ # h/help : short / long option (boolean)
+ # color : only long option (boolean)
+ # u/user= : option with required argument, only last specified is taken
+ # f/file+= : option with required argument, can be specified multiple times
+ #
+ argparse h/help color= u/user= f/file=+ -- $argv
+ or return
+
+ if set -ql _flag_help
+ echo &quot;usage ...&quot;
+ return 0
+ end
+
+ set -ql _flag_file
+ and echo &quot;file=$_flag_file | cnt:&quot; (count $_flag_file)
+
+ set -ql _flag_color
+ and echo &quot;color=$_flag_color&quot;
+
+ set -ql _flag_user
+ and echo &quot;user=$_flag_user&quot;
+end
+
+# Delete all previous defined completions for 'moose'.
+complete -c moose -e
+
+# Don't complete files for command.
+complete -c moose --no-files
+
+# Help completion.
+# -n specifies a conditions. The completion is only active if the command
+# returns 0.
+complete -c moose -s h -l help -n &quot;not __fish_contains_opt -s h help&quot; \
+ --description &quot;Print usage help and exit&quot;
+
+# File completion.
+# -F force complete files (overwrite --no-files).
+# -r requires argument.
+complete -c moose -s f -l file -F -r \
+ --description &quot;Specify file (can be passed multiple times)&quot;
+
+# Color completion.
+# -a options for completion.
+# -x short for -r and --no-files (-f)
+complete -c moose -x -l color -a &quot;red blue&quot; \
+ --description &quot;Specify a color.&quot;
+
+# User completion.
+# -a options for completion. Call a function to generate arguments.
+complete -c moose -x -s u -l user -a &quot;(__fish_complete_users)&quot; \
+ --description &quot;Specify a user&quot;
+</code></pre>
<h2 id="prompt"><a class="header" href="#prompt">Prompt</a></h2>
<p>The prompt is defined by the output of the <code>fish_prompt</code> function.</p>
-<pre><code class="language-text">function fish_prompt
+<pre><code class="language-sh">function fish_prompt
set -l cmd_ret
echo &quot;&gt; &quot;(pwd) $cmd_ret&quot; &quot;
end
</code></pre>
<blockquote>
-<p>Use <code>set_color</code> to manipulate terminal colors.</p>
+<p>Use <code>set_color</code> to manipulate terminal colors and <code>set_color -c</code> to print the
+current colors.</p>
</blockquote>
<h2 id="useful-builtins"><a class="header" href="#useful-builtins">Useful Builtins</a></h2>
-<pre><code class="language-text"># history
+<p>List all builtins with <code>builtins -n</code>.</p>
+<pre><code class="language-sh"># history
history search &lt;str&gt; # search history for &lt;str&gt;
history merge # merge histories from fish sessions
# list
-count $var # count elements in list
+count $var # count elements in list
+
+contains /bin $PATH # return 0 (true) 1 (false)
+contains -i /bin $PATH # additionally print index on stdout
# string
string split SEP STRING
+
+# math
+math -b hex 4096 # output dec as hex
+math 0x1000 # output hex as dec
+math &quot;log2(1024)&quot; # call functions
+math -s0 7/3 # integer division (by default float)
+
+# status
+status -f # abs path of current file
</code></pre>
<h2 id="keymaps"><a class="header" href="#keymaps">Keymaps</a></h2>
-<pre><code class="language-text"> Shift-Tab ........... tab-completion with search
- Alt-Up / Alt-Down ... search history with token under the cursor
- Alt-l ............... list content of dir under cursor
- Alt-p ............... append '2&gt;&amp;1 | less;' to current cmdline
+<pre><code class="language-text"> Shift-Tab .............. tab-completion with search
+ Alt-Up / Alt-Down ...... search history with token under the cursor
+ Alt-l .................. list content of dir under cursor
+ Alt-p .................. append '2&gt;&amp;1 | less;' to current cmdline
+ Alt-Left / Alt - Right . prevd / nextd, walk dir history
</code></pre>
<h2 id="debug"><a class="header" href="#debug">Debug</a></h2>
<pre><code class="language-text"> status print-stack-trace .. prints function stacktrace (can be used in scripts)