Created
April 9, 2025 09:27
-
-
Save AndrewRadev/8322e46fb7c5a26b7fa1fdac627fba98 to your computer and use it in GitHub Desktop.
Save/load a quickfix list to a file
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
command! -nargs=1 -complete=file SaveQf call s:SaveQf(<f-args>) | |
command! -nargs=1 -complete=file LoadQf call s:LoadQf(<f-args>) | |
function! s:SaveQf(filename) abort | |
let list = getqflist() | |
for entry in list | |
" Resolve each buffer to a filename, modify to take the absolute path | |
let entry.filename = fnamemodify(bufname(entry.bufnr), ':p') | |
" Remove bufnr to make sure Vim will deserialize the filename instead | |
unlet entry.bufnr | |
endfor | |
let serialized_list = map(list, {_, entry -> json_encode(entry) }) | |
call writefile(serialized_list, a:filename) | |
endfunction | |
function! s:LoadQf(filename) abort | |
if !filereadable(a:filename) | |
echoerr "File not readable: " .. a:filename | |
return | |
endif | |
let file_contents = readfile(a:filename) | |
let quickfix_entries = map(file_contents, {_, line -> json_decode(line) }) | |
call setqflist(quickfix_entries) | |
copen | |
endfunction |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment