From 805f1b4e2885e90dc24eb4ae6576461c0fa96b04 Mon Sep 17 00:00:00 2001 From: Oliver Davies Date: Tue, 3 Sep 2024 19:03:27 +0100 Subject: [PATCH] Sorting parameter arguments and array keys in Vim --- source/_notes/20.md | 49 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 source/_notes/20.md diff --git a/source/_notes/20.md b/source/_notes/20.md new file mode 100644 index 0000000..5477197 --- /dev/null +++ b/source/_notes/20.md @@ -0,0 +1,49 @@ +--- +title: Sorting parameter arguments and array keys in Vim +date: 2024-09-03 19:03:27 +tags: [PHP, Vim, Software Development] +--- + +```php +$a = [ + 'b' => 2, + 'a' => 4, + 'k' => 5, + 'f' => 1, + 'd' => 3, +]; + +$b = new Foo( + b: 2, + a: 4, + k: 5, + f: 1, + d: 3, +); +``` + +You can use `vi(` and `vi[` to visually select the text within the array or parentheses, and then ":sort" to sort the text within the selected range. + +Using `vi(` and `vi[` instead of `vi)` and `vi]` excludes the lines that around the text to sort. + +After running these commands, you should get this: + +```php +$a = [ + 'a' => 4, + 'b' => 2, + 'd' => 3, + 'f' => 1, + 'k' => 5, +]; + +$b = new Foo( + a: 4, + b: 2, + d: 3, + f: 1, + k: 5, +); +``` + +At some point, I'll look into saving this as a macro with a keymap like `sa` (sort arguments) and `sk` (sort keys).