openbsd-webzine/issues/issue-8/50_TIPS.html

21 lines
1.1 KiB
HTML

<article id="tips">
<div class="puffies" aria-hidden="true">🐡🐡🐡</div>
<h2>Shell tips</h2>
<p>It is easy to add autocompletion to commands in ksh, however they are limited because they have to be evaluated when the shell is starting.</p>
<p>In the following example, we can parse the file <pre>~/.ssh/known_hosts</pre> to get hostnames and use this list to set completion for some commands:
<pre>
HOSTS_LIST=$(awk '{split($1,a,","); print a[1]}' ~/.ssh/known_hosts)
set -A complete_ssh -- $HOSTS_LIST
set -A complete_ping -- $HOSTS_LIST
set -A complete_sndioctl_1 -- $(sndioctl | cut -d= -f 1)
</pre></p>
<p>In this other example, we add different completion depending on the parameter position
<pre>set -A complete_rclone_1 -- ncdu ls copy sync
set -A complete_rclone_2 -- $(rclone listremotes)
</pre>
Adding completion is easy but this will be evaluated at runtime, so you should avoid time consuming evaluations and some commands like scp or git can't receive much useful completion.</p>
<p>More information can be found about this feature <a class="permalink" href="https://man.openbsd.org/ksh#Emacs_editing_mode">in ksh man page</a>.</p>
</article>