Skip to content

Commit

Permalink
os: zsh history and editing command history
Browse files Browse the repository at this point in the history
  • Loading branch information
practicalli-johnny committed Mar 3, 2024
1 parent 54a6ba4 commit 0194b5e
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
- architecture: kata page first release
- careers: new section and engineering manager
- os: shell-aliases examples
- os: zsh history and editing command history

### Changed

Expand Down
56 changes: 55 additions & 1 deletion docs/os/shell/zsh.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

Z Shell (zsh) is an advanced shell environment and shell scripting language for Linux/Unix systems.


[Wikipedia - Z Shell](https://en.wikipedia.org/wiki/Z_shell){target=_blank .md-button}


Expand All @@ -11,3 +10,58 @@ Z Shell (zsh) is an advanced shell environment and shell scripting language for

## OhmyZsh


## Command History

++arrow-up++ and ++arrow-down++ navigate through the history of commands.


### Edit last command

Edit the previous command in the history if you know what it should be, e.g. if you typed got status instead of git statusthe you could run the command

!!! NOTE "Edit last command"
```shell
fc -e astro -1
```

> Replace `astro` with either `emacsclient`, `nvim`, `vim`, nano or any preferred text editor.
??? EXAMPLE "Edit last command"
Edit the `got status`to `git status` and save & exit. Now the previous command is `git status`.

Change the `-1` to if the command is further back in history (a known location)


### Edit command history

Edit the whole command history as a file, allowing easy removal of unwanted or duplicate commands using search and replace and other editing tools.

!!! NOTE "Edit entire history"
```shell
fc -W; astro "$HISTFILE"; fc -R
````

> Replace `astro` with either `emacsclient`, `nvim`, `vim`, nano or any preferred text editor.


## Shell aliases for commands

Add aliases to `$XDG_CONFIG_HOME/shell-aliases` or your preferred location of shell alias definitions.

```shell
# Shell history
# edit entire history
alias zsh-edit-history="fc -W; astro \"$HISTFILE\"; fc -R"

# edit previous command in history
alias zsh-edit-last-command="fc -e astro -1"
```

## Alternatives
- [zsh-hist plugin](https://github.com/marlonrichert/zsh-hist)

## References
- [Edit Zsh history - Stack Exchange](https://superuser.com/a/1399768)
- [zsh history setup](https://jdhao.github.io/2021/03/24/zsh_history_setup/)

0 comments on commit 0194b5e

Please sign in to comment.