From cea3bc05b5a40c075f14e24e7136382edeccd0bf Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Thu, 20 Feb 2025 17:42:32 -0600 Subject: [PATCH] Add Fix Previous Command With fc as a Unix TIL --- README.md | 3 ++- unix/fix-previous-command-with-fc.md | 26 ++++++++++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 unix/fix-previous-command-with-fc.md diff --git a/README.md b/README.md index 98da4b5..e15838f 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ pairing with smart people at Hashrocket. For a steady stream of TILs, [sign up for my newsletter](https://crafty-builder-6996.ck.page/e169c61186). -_1595 TILs and counting..._ +_1596 TILs and counting..._ See some of the other learning resources I work on: - [Ruby Operator Lookup](https://www.visualmode.dev/ruby-operators) @@ -1532,6 +1532,7 @@ See some of the other learning resources I work on: - [Find Newer Files](unix/find-newer-files.md) - [Find Occurrences Of Multiple Values With Ripgrep](unix/find-occurrences-of-multiple-values-with-ripgrep.md) - [Find Top-Level Directories Matching A Pattern](unix/find-top-level-directories-matching-a-pattern.md) +- [Fix Previous Command With fc](unix/fix-previous-command-with-fc.md) - [Fix Shim Path After asdf Upgrade](unix/fix-shim-path-after-asdf-upgrade.md) - [Fix Unlinked Node Binaries With asdf](unix/fix-unlinked-node-binaries-with-asdf.md) - [Forward Multiple Ports Over SSH](unix/forward-multiple-ports-over-ssh.md) diff --git a/unix/fix-previous-command-with-fc.md b/unix/fix-previous-command-with-fc.md new file mode 100644 index 0000000..18798f0 --- /dev/null +++ b/unix/fix-previous-command-with-fc.md @@ -0,0 +1,26 @@ +# Fix Previous Command With fc + +The `fc` command is a Bash and ZSH built-in command that allows you to interact +with the history of commands issued in the shell. The most straightforward use +case I know of for using this command is to fix or edit some aspect of the +previous run command. + +When `fc` is executed with no arguments or flags, it will grab the latest entry +to the command history and load it into your default editor. For me, that is +Vim. + +I can make edits in that Vim session like I'd do in any other Vim session. When +I write and quit (`:wq`) the file, the updated command will be executed. This +is useful if, say, I've made a typo in the previous command and would prefer +the ergonomics of my default editor to fix it. Or let's say I have a really +long command with many flags and long file path arguments. It would be much +easier and quicker to edit those paths from my editor than from the terminal +prompt. + +If I've opened my editor (Vim) with `fc` and I decide I don't want to execute +the command after all, I can _compiler quit_ Vim (exit with an error code) +using `:cq`. The command will not be executed in this case. + +See `man zshbuiltins` for more details about this command and all of its flags. + +[source](https://www.computerhope.com/unix/uhistory.htm)