2

Context

Say in vim, after :grep text *, then :copen, then cursor in a quickfix window, which has contents like below

some-very-very-very-long-path/file1.txt|1 col 46| some text
some-very-very-very-long-path/file2.txt|84 col 34| another text
some-very-very-very-long-path/file3.txt|243 col 29| yetanother text

Problem

How to conceal the filename and line column number at the beginning of each line?

9

You can use the conceal feature for this.

Add the following to the file ~/.vim/after/syntax/qf.vim (first creating the file if necessary):

syntax match ConcealedDetails /\v^[^|]*\|[^|]*\| / conceal
setlocal conceallevel=2
setlocal concealcursor=nvic

The first line sets up a syntax item to conceal anything that matches the regular expression.

The second line informs Vim that you want to hide any concealed items that don't have a replacement cchar set.

The third line makes it so that the text is always concealed, whatever mode you are in.

The regular expression I used presumes that your file names/paths won't ever contain a | character. If they do, you'll need to adjust your regular expression to account for this.

Further details:

4

While the other answers here have all the necessary pointers ever needed, I think there is lacking a comprehensive solution IMHO.

Below is a ready-to-paste in your .vimrc (or equivalent)

au BufReadPost quickfix :call ConcealPath() 

function ConcealPath()
       syntax match ConcealedPath /\v^\/[^|]*\// conceal cchar=&
       setlocal conceallevel=2
       setlocal concealcursor=nvic
       setlocal nowrap 
endfunction

function UnconcealPath()
       setlocal conceallevel=0
       setlocal wrap
endfunction

More than being a comprehensive answer, it is a more general solution.

You are now equipped to hide similar "long paths" without any file-edits, any where in vim (not just in a quickfix window), using

:call ConcealPath()

and revert/unhide using

:call UnconcealPath()

NOTE-1: The use of set wrap and set nowrap is only for aesthetics that I liked in my usage

NOTE-2: You can also conveniently map your favourite keys to reduce typing in case you need frequently using the function calls

NOTE-3: All this works only when you notice +conceal is present in your :version (or vim --version from terminal). So -conceal will knock you off this deal. Neovim (nvim) on the other hand, will not suffer from this issue due to its philosophy of all-possible features always-on.

3
  • 1
    Thanks for the summary! A small nitpick that you don't need the :s inside autocmd and functions, only when using Ex commands interactively... So personally I'd recommend removing those. – filbranden May 20 at 23:54
  • 1
    @filbranden Done! – Loves Probability May 21 at 0:44
  • I updated my answer to include complete instructions. Not sure why I didn't do so in the first place, as it's actually more concise this way, so thanks for nudging me into doing so! I like your idea of adding functions so you can toggle the behaviour. +1 – Rich May 21 at 9:40
0

according to quickfix.txt and the answer by Rich, following works:

au BufReadPost quickfix  setlocal modifiable
            \ | silent exe 'g/^/s/\v^[^|]*\|[^|]*\| //'
            \ | setlocal nomodifiable
1
  • 2
    Welcome to Vi and Vim! I'm not sure this uses conceal so much as actually removing the relevant characters? – D. Ben Knoble Mar 20 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.