trouble.nvim
trouble.nvim copied to clipboard
Can I use trouble to replace quickfix window after :make?
Currently the default quickfix or loclist will open automatically after :make
.
Is it possible to open trouble's quickfix list after :make
?
I have a similar question: How can I use trouble to open itself everytime a quickfix window is actually being displayed? I.e., in general not just when using make? Is there a nvim hook to attach to?
And thanks for the great work! Nice plugin 👍
Got the same questions here. I was using a plugin that showed diagnostics inside a native quickfix list, but is not as good as this plugin.
You should be able to do this using the QuickFixCmdPost
autocmd. In the autocmd, just close the quickfix window and open trouble instead.
@folke Thanks for the hint. I configured like this, trouble did open after :make, but quickfix window is not closed, do you know why? I can confirm QfTrouble is invoked.
{
"folke/trouble.nvim",
requires = "kyazdani42/nvim-web-devicons",
config = function()
vim.cmd([[
function! QfTrouble()
execute 'ccl'
execute 'TroubleToggle'
endfunction
augroup trouble
autocmd!
autocmd QuickFixCmdPost make call QfTrouble()
augroup END
]])
end,
}
Not sure. I'm traveling right now and don't have my laptop here. Maybe there's an option where you can disable opening quick fix after make? If not you could try postponing the close in a vim.defer_fn
. will probably work with a timeout of zero. If not increase the timeout to something like 10ms
Any update on this? This would be an excellent recipe to have, the standard lists are bit lackluster compared to Trouble
. Sorry, but I'm spoiled... 😅
Hi, I came up with this solution, using ftplugin
-- file: after/ftplugin/qf.lua
local buftype = "quickfix"
if vim.fn.getloclist(0, { filewinid = 1 }).filewinid ~= 0 then
buftype = "loclist"
end
local ok, trouble = pcall(require, "trouble")
if ok then
vim.api.nvim_win_close(0, true)
trouble.toggle(buftype)
else
local set = vim.opt_local
set.colorcolumn = ""
set.number = false
set.relativenumber = false
set.signcolumn = "no"
end
I'm using packer as package manager, so I load it like this
use({
"folke/trouble.nvim",
requires = { "kyazdani42/nvim-web-devicons" },
config = function()
require("plugin.trouble.config")
require("plugin.trouble.keymap")
end,
ft = { "qf" }, -- load on FileType qf
event = { "DiagnosticChanged" }, -- or on this event
module = "trouble", -- or when using `require('trouble')`
})
Thanks to @fitrh's answer, here is how I did it without packer:
Add this to any lua file that you use to source:
function ToggleTroubleAuto()
local buftype = "quickfix"
if vim.fn.getloclist(0, { filewinid = 1 }).filewinid ~= 0 then
buftype = "loclist"
end
local ok, trouble = pcall(require, "trouble")
if ok then
vim.api.nvim_win_close(0, true)
trouble.toggle(buftype)
else
local set = vim.opt_local
set.colorcolumn = ""
set.number = false
set.relativenumber = false
set.signcolumn = "no"
end
end
Then you can add an autocmd in vimrc or in lua but I prefer to do in vim script:
autocmd BufWinEnter quickfix silent :lua ToggleTroubleAuto()
This way you can open stuff from Telescope to quickfix and it automaticaly opens it with Trouble. Hope it helps to someone!
@furkanbiten thanks! I have an issue however, this only works for me the first time. After that, if I close the Trouble window and try opening quickfix, I'm getting a Vim:E813: Cannot close autocmd window
. Any tips? Have you ran into that?
@thesergiomiguel this works for me
In lua file:
function ToggleTroubleAuto()
local ok, trouble = pcall(require, "trouble")
if ok then
vim.defer_fn(function()
vim.cmd('cclose')
trouble.open('quickfix')
end, 0)
end
end
In vimrc:
autocmd BufWinEnter quickfix silent :lua ToggleTroubleAuto()
I think this would be a great feature if trouble would replace quickfix by default.
I think this would be a great feature if trouble would replace quickfix by default.
Not by default, but maybe a hijack_quickfix
option similar to how nvimtree replaces netrw.
I pilfered @EddiG's code from https://github.com/folke/trouble.nvim/issues/70#issuecomment-1315718808, and modified it to only automatically replace the quickfix list when it matched a specific list, rather than all lists.
In my case, this is the QF list from tsc.nvim, but it could be anything.
Here's my solution if it helps anyone:
-- Replace the quickfix window with Trouble when viewing TSC results
local function replace_quickfix_with_trouble()
local title = vim.fn.getqflist({ title = 0 }).title
if title ~= "TSC" then
return
end
local ok, trouble = pcall(require, "trouble")
if ok then
vim.defer_fn(function()
vim.cmd("cclose")
trouble.open("quickfix")
end, 0)
end
end
local group = vim.api.nvim_create_augroup("ReplaceQuickfixWithTrouble", {})
vim.api.nvim_create_autocmd("BufWinEnter", {
pattern = "quickfix",
group = group,
callback = replace_quickfix_with_trouble,
})
The previous code snippets missed some aspects to suit my needs, so I shamelessly combined them to a version that works for me and my configuration. Maybe it is of interest to anyone.
It replaces the quickfix and location list by immediately closing the native one and opening the trouble counterpart. And it works with the :cnewer
and :colder
commands.
The following can be put in after/ftplugin/qf/init.lua
or an autocommand:
local function use_trouble()
local status_ok, trouble = pcall(require, "trouble")
if status_ok then
-- Check whether we deal with a quickfix or location list buffer, close the window and open the
-- corresponding Trouble window instead.
if vim.fn.getloclist(0, { filewinid = 1 }).filewinid ~= 0 then
vim.defer_fn(function()
vim.cmd.lclose()
trouble.open("loclist")
end, 0)
else
vim.defer_fn(function()
vim.cmd.cclose()
trouble.open("quickfix")
end, 0)
end
end
end
use_trouble()
https://github.com/folke/trouble.nvim/issues/70#issuecomment-1528094026 This is great @benj9000 , thanks! Would definitely love an option to hijack quickfix like @daephx suggested.
https://github.com/folke/trouble.nvim/issues/70#issuecomment-1522085016 considering tsc.nvim
now supports watch mode I modified your solution to auto-close the trouble.nvim
window when tsc
returns no errors:
-- Replace the quickfix window with Trouble when viewing TSC results
local function replace_quickfix_with_trouble()
local qflist = vim.fn.getqflist({ title = 0, items = 0 })
if qflist.title ~= "TSC" then
return
end
local ok, trouble = pcall(require, "trouble")
if ok then
-- close trouble if there are no more items in the quickfix list
if next(qflist.items) == nil then
vim.defer_fn(trouble.close, 0)
return
end
vim.defer_fn(function()
vim.cmd("cclose")
trouble.open("quickfix")
end, 0)
end
end
The previous code snippets missed some aspects to suit my needs, so I shamelessly combined them to a version that works for me and my configuration. Maybe it is of interest to anyone.
It replaces the quickfix and location list by immediately closing the native one and opening the trouble counterpart. And it works with the
:cnewer
and:colder
commands.The following can be put in
after/ftplugin/qf/init.lua
or an autocommand:local function use_trouble() local status_ok, trouble = pcall(require, "trouble") if status_ok then -- Check whether we deal with a quickfix or location list buffer, close the window and open the -- corresponding Trouble window instead. if vim.fn.getloclist(0, { filewinid = 1 }).filewinid ~= 0 then vim.defer_fn(function() vim.cmd.lclose() trouble.open("loclist") end, 0) else vim.defer_fn(function() vim.cmd.cclose() trouble.open("quickfix") end, 0) end end end use_trouble()
Thanks for your snippet! However, I can't get this to work when using multiple windows
eg: :h
, and press gO
, it will only work when I make the help page the only window by
typing :only
after running :h
My configuration in lazy.nvim:
{
"folke/trouble.nvim",
lazy = true,
ft = "qf",
event = "DiagnosticChanged",
keys = {
-- not important
},
config = function()
require("trouble").setup()
-- hijack other windows with trouble https://github.com/folke/trouble.nvim/issues/70#issuecomment-1528094026
local function use_trouble()
local trouble = require("trouble")
-- Check whether we deal with a quickfix or location list buffer, close the window and open the
-- corresponding Trouble window instead.
if vim.fn.getloclist(0, { filewinid = 1 }).filewinid ~= 0 then
vim.defer_fn(function()
vim.cmd.lclose()
trouble.open("loclist")
end, 0)
else
vim.defer_fn(function()
vim.cmd.cclose()
trouble.open("quickfix")
end, 0)
end
end
vim.api.nvim_create_autocmd("FileType", {
pattern = {"qf"},
callback = use_trouble
})
end,
dependencies = { "nvim-tree/nvim-web-devicons" },
},
Does anyone have any suggestions? or, an option to hijack / replace the quickfix window as suggested by @daephx is highly appreciated ❤️
Development on the main branch is EOL.
Trouble has been rewritten and will be merged in main soon.
This issue/feature either no longer exists or has been implemented on dev.
For more info, see https://github.com/folke/trouble.nvim/tree/dev
Just curious I am probably overthinking this, but how would I implement this using Trouble V3?