Skip to content

Commit

Permalink
Add how to bash completion
Browse files Browse the repository at this point in the history
  • Loading branch information
Kopylov Vladislav committed Oct 25, 2024
1 parent 2e73f77 commit 496cb0d
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
38 changes: 38 additions & 0 deletions docs/terminal/002.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# How to add completion for a bash script


1 - you have a script `dothis`

```bash
if [ -z "$1" ]; then
echo "No command number passed"
exit 2
fi

exists=$(fc -l -1000 | grep ^$1 -- 2>/dev/null)

if [ -n "$exists" ]; then
fc -s -- "$1"
else
echo "Command with number $1 was not found in recent history"
exit 2
fi
```

2 - In order to write completion create a file `dothis-completion.bash`

```bash
#/usr/bin/env bash

complete -W "now1 tomorrow1 never1" dothis
```

3 - In order to add completion to your current bash session run

```bash
source ./dothis-completion.bash
```

4 - Voilà! Now you can push <tab> and see complection

[source](https://iridakos.com/programming/2018/03/01/bash-programmable-completion-tutorial)
1 change: 1 addition & 0 deletions docs/terminal/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ Welcome to terminal page. Please open menu and enjoy.
## Menu

- [Terminal moving](terminal/001.md)
- [Add bash completion](terminal/002.md)

0 comments on commit 496cb0d

Please sign in to comment.