Skip to content

feat: add tab-local AIChat toggle and per-tab chat buffer reuse#203

Closed
Konfekt wants to merge 4 commits intomadox2:mainfrom
Konfekt:tab
Closed

feat: add tab-local AIChat toggle and per-tab chat buffer reuse#203
Konfekt wants to merge 4 commits intomadox2:mainfrom
Konfekt:tab

Conversation

@Konfekt
Copy link
Contributor

@Konfekt Konfekt commented Feb 26, 2026

Let's one toggle one AIchat window per tab

@madox2
Copy link
Owner

madox2 commented Mar 6, 2026

Hi, could you explain in more detail what is this feature doing?

It looks like it is something that should work - opening and focusing last chat within the current window.

If you look at this line: https://github.com/madox2/vim-ai/blob/main/autoload/vim_ai.vim#L253 it says it is trying to re-use the chat in the active window.

However it doesn't seem to work, it only checks >>> AI chat buffer name, but it needs to be more robust and check for >>> AI chat 2, >>> AI chat 3, etc.

It is a bug but the fix should not be that complicated like the code in this PR, but maybe I am missing something.

@Konfekt
Copy link
Contributor Author

Konfekt commented Mar 6, 2026

Maybe there's a confusion between tab and window. A tab is a window container in Vim. Now there's exactly one AIChat window per tab that can be toggled by :AIChat: If the AIChat window is closed, then :AIChat restores /opens the one in the tab it was closed. On other tabs, it will create a new AIChat window, one per each tab

@Konfekt
Copy link
Contributor Author

Konfekt commented Mar 6, 2026

Maybe clearer would be to bind one :AIchat window to each tab (as window container).

@madox2
Copy link
Owner

madox2 commented Mar 6, 2026

I see now. But the code still looks quite complicated. Isn't enough to set tab variable and if exist use it over l:last_scratch_buffer_name in the OpenChatWindow function?

Konfekt added 2 commits March 7, 2026 09:46
Avoid toggling chat buffers through a separate entry point and fix
tab-local chat reuse when buffers are hidden or visible elsewhere.
Reuse or create chat windows in one code path, track tab chat buffers
consistently, and open escaped scratch buffer names safely.

- Removed the unused `force_new` parameter from `s:OpenChatWindow()`.
- Removed the forwarding-only `vim_ai#AIChatToggleRun()`.
- Removed the duplicate `settabvar()` from `vim_ai#AIChatRun()`.
- Added `abort` to `s:ReuseOrCreateChatWindow()`.
- Kept tab ownership based on `bufnr`, not scratch names.
- Escaped scratch names with `fnameescape()` when calling `:file`.
- Inlined trivial one-use window visibility logic into `s:ReuseOrCreateChatWindow()`.
Copilot AI review requested due to automatic review settings March 7, 2026 11:44
@Konfekt
Copy link
Contributor Author

Konfekt commented Mar 7, 2026

OpenChatWindow() always opens a new window first. If the chat is already visible in the current tab, opening first and then switching buffers creates a duplicate window instead of focusing the existing one. Hopefully the latest commit simplifies overall changes

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates vim-ai’s chat UX so :AIChat reuses a dedicated chat buffer per tab (instead of reusing “last scratch buffer” across the whole session), and adds a buffer-local :AI command inside aichat buffers to continue the conversation.

Changes:

  • Implement tab-local chat buffer tracking/reuse via tab variables in chat window creation logic.
  • Add a buffer-local :AI command in aichat buffers to continue chat completion.
  • Update user-facing docs/help text to describe the new tab-local chat behavior.

Reviewed changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
autoload/vim_ai.vim Reworks chat window reuse/creation to be tab-local and removes the old “last scratch buffer name” reuse logic.
ftplugin/aichat.vim Adds a buffer-local :AI command in aichat buffers that routes to chat continuation.
doc/vim-ai.txt Updates :AI/:AIChat documentation to reflect the new tab-local semantics.
README.md Updates :AIChat description and usage notes to reflect the new behavior.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Konfekt and others added 2 commits March 7, 2026 14:23
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
@madox2
Copy link
Owner

madox2 commented Mar 7, 2026

@Konfekt I implemented this feature in a slightly different way (also fixed the issue I mentioned above). Would you check it out and let me know your thoughts? It is in PR #207

@Konfekt Konfekt closed this Mar 8, 2026
@Konfekt
Copy link
Contributor Author

Konfekt commented Mar 8, 2026

I am sorry for having overread the bug report above. In any event, #207 seems now the way forward

@Konfekt
Copy link
Contributor Author

Konfekt commented Mar 9, 2026

Reopening to address #207 not working with dirty configurations, including "open_chat_command": "preset_right", being set

@madox2
Copy link
Owner

madox2 commented Mar 9, 2026

Should be working, if it is not, please create an issue. Please explain what "dirty configuration" is in your case and provide steps to reproduce

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants