From a078a1ae515288dd62b6c209f2327a5b198da098 Mon Sep 17 00:00:00 2001 From: Axlefublr <101342105+Axlefublr@users.noreply.github.com> Date: Fri, 30 May 2025 14:58:28 +0800 Subject: [PATCH] add :buffer-nth --- book/src/generated/typable-cmd.md | 1 + helix-term/src/commands/typed.rs | 37 +++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+) diff --git a/book/src/generated/typable-cmd.md b/book/src/generated/typable-cmd.md index 219f6b95f..6cc4b96b1 100644 --- a/book/src/generated/typable-cmd.md +++ b/book/src/generated/typable-cmd.md @@ -89,3 +89,4 @@ | `:read`, `:r` | Load a file into buffer | | `:echo` | Prints the given arguments to the statusline. | | `:noop` | Does nothing. | +| `:buffer-nth`, `:bi` | Switch to the nth buffer, out of those you have open. | diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs index 2013a9d81..eae168176 100644 --- a/helix-term/src/commands/typed.rs +++ b/helix-term/src/commands/typed.rs @@ -321,6 +321,24 @@ fn buffer_previous( Ok(()) } +fn buffer_nth(cx: &mut compositor::Context, args: Args, event: PromptEvent) -> anyhow::Result<()> { + if event != PromptEvent::Validate { + return Ok(()); + } + let n: usize = args[0] + .parse() + .map_err(|_| anyhow!("provided argument is not an integer"))?; + ensure!(n != 0); + let (id, _) = if args.has_flag("reverse") { + cx.editor.documents.iter().nth_back(n - 1) + } else { + cx.editor.documents.iter().nth(n - 1) + } + .ok_or(anyhow!("buffer {n} is out of range"))?; + cx.editor.switch(*id, helix_view::editor::Action::Replace); + Ok(()) +} + fn write_impl(cx: &mut compositor::Context, path: Option<&str>, force: bool) -> anyhow::Result<()> { let config = cx.editor.config(); let jobs = &mut cx.jobs; @@ -3567,6 +3585,25 @@ pub const TYPABLE_COMMAND_LIST: &[TypableCommand] = &[ ..Signature::DEFAULT }, }, + TypableCommand { + name: "buffer-nth", + aliases: &["bi"], + doc: "Switch to the nth buffer, out of those you have open.", + fun: buffer_nth, + completer: CommandCompleter::none(), + signature: Signature { + positionals: (1, None), + flags: &[ + Flag { + name: "reverse", + alias: Some('r'), + doc: "count buffers from the end", + ..Flag::DEFAULT + }, + ], + ..Signature::DEFAULT + }, + }, ]; pub static TYPABLE_COMMAND_MAP: Lazy> =