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

21 lines
1.2 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 autocompletion entries 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 autocomplete differently depending on the parameter position
<pre>set -A complete_rclone_1 -- ncdu ls copy sync
set -A complete_rclone_2 -- $(rclone listremotes)
</pre>
Adding autocompletion entries is easy but in ksh it is 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 the ksh man page</a>.</p>
</article>