Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Vim file paths in quickfix window

Tags:

vim

I made a function in Vim that compiles some stuff for me. It looks like so:

function! MyFunc(mode)
  lcd ./build
  pwd
  let &makeprg='the_command some_script_file'
  let &errorformat='some format'.','
  let &errorformat.='%-G%.%#'
  silent make
  lcd ..
  cwindow
endfunction

I usually open vim in a project directory, then I can run this function, which cd's into build, builds it, and cd's back so I stay in my project directory.

However, sometimes, when the build fails and brings up the quickfix window, it'll show filenames relative to my project dir (yay), but other times in the same file, it shows the absolute path. The output from the build script always shows relative paths, and vim handles both correctly, i.e. it finds the correct file.

I suspect the path handling is responsible. My question is, what's the problem, and is there a better way to handle switching into the build directory and back? I always want relative paths shown.

Thanks!!

like image 491
Christoph Avatar asked May 24 '12 18:05

Christoph


People also ask

What is quickfix list Vim?

The quickfix and location lists provide a powerful way to navigate in Vim especially in the context of searching a file or project, and when inspecting errors. Essentially, they are lists of file locations with a built-in set of commands for navigating between them, and they can be populated in a variety of ways.

What is location list in Vim?

*location-list* *E776* A location list is similar to a quickfix list and contains a list of positions in files. A location list is associated with a window and each window can have a separate location list. A location list can be associated with only one window.


2 Answers

I think this has to do with the lcd. When the quickfix window is opened from your function, it will inherit the local-directory from the current window. But when the quickfix window has been opened from anywhere else (or before running your function), it will keep the global working directory. When there's a mismatch, Vim has to display absolute paths.

If you really need to use :lcd, it would be better to use a location list (:lmake), because that will be local to the window, too. But I would first try to use a global :cd, since the switch is only temporary, anyway, and you don't care about restoring the actual original value so far.

like image 61
Ingo Karkat Avatar answered Sep 27 '22 19:09

Ingo Karkat


As Ingo pointed out, the paths are relative to the current directory of quickfix window, which is inherited from the previous window.

You can use autocmd QuickFixCmdPre to set the correct path before executing the make.

If you use Project Root plugin add the following to your .vimrc:

augroup changeQfCmdDir
   au!
   autocmd QuickFixCmdPre make ProjectRootCD
augroup END

, then the path of quickfix window will be corrected before it is filled by a :make (you can also specify a number of other commands that uses quickfix windows: grep,lgrep,cscope).

like image 32
mMontu Avatar answered Sep 27 '22 19:09

mMontu