Vim errorformat string to show message in QuickFix removing part of it - vim

I'm writing an errorformat string, and it works for the most part. My problem is that I have lines like this as the makeprg output:
Some text I want to show in the QuickFix window^M
Yes, the line ends with an spurious ^M character I want to remove. So, what I want in my QuickFix window is this, without the ^M character:
|| Some text I want to show in the QuickFix window
but I have this instead:
|| Some text I want to show in the QuickFix window^M
So far, this is the relevant part of my errorformat:
set errorformat=%+GSome text%m
I've tested, without success, something like this:
set errorformat=%+GSome text%m%-G^M%.%#
but it throws an error (not from the ^M which is a literal control-M char, not a caret followed by an M).
Obviously the solution is not using %G but I am at a loss here.
How can I remove the line ending character from the line here? And also, removing the initial || would be a plus, but I think it's impossible to do in Vim.
Thanks in advance!
Edited to make clearer how the input text looks

Well, turns out I found a solution, probably not very good but it works, using trial and error.
set errorformat=%\\(Some Text%*[^.]).%\\)%\\#=%m
That is, the solution is using the Vim pattern (regex) expressions within errorformat, which has a quite arcane look but works, together with %* to match unknown text on the rest of the line
The solution uses \#=, a zero-width match, and requires some kind of terminator for the line, which appears before the ^M character I want to ignore, and some kind of text appearing somewhere on the line to match that line and not others.
Probably there is a much better solution but this is the best I could do myself.

Related

What's the meaning of some advanced patterns in vim errorformat? (%s, %+, %\\#=)

I tried reading :help errorformat and googling (mostly stackoverflow), but can't understand some of the patterns mentioned there:
%s - "specifies the text to search for to locate the error line. [...]"
um, first of all, trying to understand the sentence at all, where do I put the "text to search", after the %s? before it? or, I don't know, does it maybe taint the whole pattern? WTF?
secondly, what does this pattern actually do, how does it differ from regular text in a pattern, like some kinda set efm+=,foobar? the "foobar" here is for me also "text to search for"... :/
%+ - e.g. I I've seen something like that used in one question: %+C%.%#
does it mean the whole line will be appended to a %m used in an earlier/later multiline pattern? if yes, then what if there was not %.%# (== regexp .*), but, let's say, %+Ccont.: %.%# - would something like that work to capture only stuff after a cont.: string into the %m?
also, what's the difference between %C%.%# and %+C%.%# and %+G?
also, what's the difference between %A and %+A, or %E vs. %+E?
finally, an example for Python in :help errorformat-multi-line ends with the following characters: %\\#=%m -- WTF does the %\\#= mean?
I'd be very grateful for some help understanding this stuff.
Ah, errorformat, the feature everybody loves to hate. :)
Some meta first.
Some Vim commands (such as :make and :cgetexpr) take the output of a compiler and parse it into a quickfix list. errorformat is a string that describes how this parsing is done. It's a list of patterns, each pattern being a sort of hybrid between a regexp and a scanf(3) format. Some of these patterns match single lines in the compiler's output, others try to match multiple lines (%E, %A, %C etc.), others keep various states (%D, %X), others change the way parsing proceeds (%>), while yet others simply produce messages in the qflist (%G), or ignore lines in the input (%-G). Not all combinations make sense, and it's quite likely you won't figure out all details until you look at Vim' sources. shrug
You probably want to write errorformats using let &erf='...' rather than set erf=.... The syntax is much more human-friendly.
You can experiment with errorformat using cgetexpr. cgetexpr expects a list, which it interprets as the lines in the compiler's output. The result is a qflist (or a syntax error).
qflists are lists of errors, each error being a Vim "dictionary". See :help getqflist() for the (simplified) format.
Errors can identify a place in a file, they can be simple messages (if essential data that identifies a place is missing), and they can be valid or invalid (the invalid ones are essentially the leftovers from parsing).
You can display the current qflist with something like :echomsg string(getqflist()), or you can see it in a nice window with :copen (some important details are not shown in the window though). :cc will take you to the place of the first error (assuming the first error in qflist actually refers to an error in a file).
Now to answer your questions.
um, first of all, trying to understand the sentence at all, where do I put the "text to search", after the %s? before it?
You don't. %s reads a line from the compiler's output and translates it to pattern in the qflist. That's all it does. To see it at work, create a file efm.vim with this content:
let &errorformat ='%f:%s:%m'
cgetexpr ['efm.vim:" bar:baz']
echomsg string(getqflist())
copen
cc
" bar baz
" bar
" foo bar
Then run :so%, and try to understand what's going on. %f:%s:%m looks for three fields: a filename, the %s thing, and the message. The input line is efm.vim:" bar:baz, which is parsed into filename efm.vim (that is, current file), pattern ^\V" bar\$, and message baz. When you run :cc Vim tries to find a line matching ^\V" bar\$, and sends you there. That's the next-to-last line in the current file.
secondly, what does this pattern actually do, how does it differ from regular text in a pattern, like some kinda set efm+=,foobar?
set efm+=foobar %m will look for a line in the compiler's output starting with foobar, then assign the rest of the line to the message field in the corresponding error.
%s reads a line from the compiler's output and translates it to a pattern field in the corresponding error.
%+ - e.g. I I've seen something like that used in one question: %+C%.%#
does it mean the whole line will be appended to a %m used in an earlier/later multiline pattern?
Yes, it appends the content of the line matched by %+C to the message produced by an earlier (not later) multiline pattern (%A, %E, %W, or %I).
if yes, then what if there was not %.%# (== regexp .*), but, let's say, %+Ccont.: %.%# - would something like that work to capture only stuff after a cont.: string into the %m?
No. With %+Ccont.: %.%# only the lines matching the regexp ^cont\.: .*$ are considered, the lines not matching it are ignored. Then the entire line is appended to the previous %m, not just the part that follows cont.:.
also, what's the difference between %C%.%# and %+C%.%# and %+G?
%Chead %m trail matches ^head .* trail$, then appends only the middle part to the previous %m (it discards head and trail).
%+Chead %m trail matches ^head .* trail$, then appends the entire line to the previous %m (including head and trail).
%+Gfoo matches a line starting with foo and simply adds the entire line as a message in the qflist (that is, an error that only has a message field).
also, what's the difference between %A and %+A, or %E vs. %+E?
%A and %E start multiline patterns. %+ seems to mean "add the entire line being parsed to message, regardless of the position of %m".
finally, an example for Python in :help errorformat-multi-line ends with the following characters: %\\#=%m -- WTF does the %\\#= mean?
%\\#= translates to the regexp qualifier \#=, "matches preceding atom with zero width".

How can I get vim find to ignore whitespace?

I have XML entries that span two or more lines sometimes, and so I can look for something like this:
something on one line
But if I try the vim command /something on one line and the line is like this:
something on one
line
then it doesn't find it, because the second text block is actually seen as
something on one^J line
Which might have something to do with the fact that I'm using a DOS formatted file.
How can I get vim search to ignore whitespace and newlines?
In vim search, _s means a new line, a space or a tab. So you can try something such as:
/something on one\_s*line
to match the example string you used as example.
Replacing the spaces with \_s\+ manually is cumbersome. The Search for visually selected text topic on the Vim Tips Wiki has a mapping that does this for you for the current visual selection. You can use it like the built-in * mapping, to search (ignoring spaces) for the current selection. Very handy!
While I like the existing answers, I was looking for a way to do the substitution of spaces to \_s* "automagically" for every search that I type.
Luckily, such a solution is possible, as explained in this reddit post. I ended up using the following command:
cnoremap <expr><space> '/?' =~ getcmdtype() ? '\_s*' : ' '
This makes it so that whenever you type a space in a search command (either / or ?), all spaces that you type are automatically replaced by \_s*.
In fact, I only wanted this behavior for LaTeX files, so I added the following to my .vimrc:
autocmd FileType tex cnoremap <expr><space> '/?' =~ getcmdtype() ? '\_s*' : ' '

vim Jump to the next non-identical line

I am looking at files that may have several consecutive identical lines.
Is there a easy way of jumping to the next non-identical line?
Alternatively I would like to be able to fold all the lines that are equal to the initial one showing just the number of linees that are folded.
You could define your own fold-expr:
first set fdm:
:set fdm=expr
then
:set foldexpr=getline(v:lnum)==#getline(v:lnum-1)?1:0
now you can test by typing zM, to close all fold, if you are lucky ^_^ all duplicated lines are folded.
you could type zR to open all folds.
if it works and you open those kind of file very often, you could put the above lines in your .vimrc.(au with ft) if only one time job, you can write mode line into that file.
Try this:
:nmap <F2> "1y$<CR>/^\(<C-R>1$\)\#!<CR>
It maps F2 to:
copy the current line into register 1
search for (and move to) the first line that does not match the contents of register 1
This seems to work well, unless the text of your copied line has escaped characters that will confuse the search regexp. This is because register 1 is just dropped into the search expression without escaping. This would be tricky to fix reliably, but for normal log files, it shouldn't be much of a problem.
Also: if you're not married to vim and just need to read the non-consecutively-duplicated lines of a file, the canonical UNIX way is:
uniq filename
If you want to be in vim but won't need to make changes to the file, try:
:%!uniq
(If you try the latter, be sure to exit without saving)

^# symbol in vim

The following symbol shows up when i view my file in vim.
---<snip>----
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#
^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#^#fstalone
---<snip>-----
The file that I create is by redirecting stdout and stderr of my utility, like this: #./my_util > util.log 2>&1. This file tend to grow quite huge ( ~4 MB )
What is this symbol?
How to get rid of it?
That is the null character, in a format (which Vim uses a lot, as you've probably noticed) called caret notation. Basically, somehow you're getting bytes full of zeros into your file.
Since we don't know what your utility is doing, if it's the culprit, you'll need to show us some code if you want us to help. Otherwise, if you just want to remove the characters from your file, use a substitution:
%s/<Ctrl-V><Ctrl-J>//g
Ctrl-V marks the beginning of an escape sequence. After pressing Ctrl-J as well, you should see ^# appear in your command. Thus, as you guessed, Ctrl-V, Ctrl-J is one escape sequence for the null character.
None of the above worked for me. I had a file with '^#' at the end of some lines that I wanted to replace. I managed the substitute it by searching for '[\x0]' using:
%s/[\x0]//g
I hope it saves someone an hour of their life.
There's an explanation here that I will go back to read when I'm not so busy:
A discussion with a better explanation
^# shows up when you try to open a non text file in vim. For example if you open a exe file or an image file ^# is shown which is a non-readable character. Try opening the file in some other editor and see the result

VIM: How to change the Showbreak Highlight color without using the NonText Color-element

I noted that the 'showbreak' symbol is highlighted with the highlight "NonText" color-element. NonText is also used for the EOL Characters.
I would like to keep the highlight-color for the EOL characters but want to change it for the showbreak symbol is that possible?
Another problem is that my showbreak symbol is not displayed.
I would like to use this symbol "↳" and put it in the linenumbers column (using set cpoptions+=n). I can't find out how to display the symbol and how to put a space after the showbreak symbol (between the text and the symbol).
Can anyone help me?
I don't think you're going to get highlighting to be different than the EOL character, at least I am not aware of a way to do that.
For the second part I can help with. I was able to get "↳ " to show up in my line number column with the following settings:
let &showbreak = '↳ '
set wrap
set cpo=n
Note that there is a space after the ↳. This lines up nice until you have > 9 lines in the file. If you wanted it to line up with the last character of the number column regardless of the number of lines I'm not sure what you're going to have to do.
Edit: I've recently written a proof-of-concept function for someone on IRC that highlights the first character on a line that has been wrapped with a different highlight group. It hasn't been tested much but it seems to work. Not exactly what you're looking for but maybe it's worth a look.
:help hl-NonText makes it pretty clear that you cannot have different colors for the 'showbreak' string and other non-text strings, of which eol is a member (see :help 'listchars'):
NonText
'~' and '#' at the end of the window, characters from 'showbreak' and
other characters that do not really exist in the text (e.g., ">"
displayed when a double-wide character doesn't fit at the end of the
line).
If you're willing to accept this limitation (#elliottcable) hi! link NonText LineNr will match the 'showbreak' string to the line number colors.
If you really wanted to get clever, as a compromise you could create a mapping or command to toggle between ':set list' and ':set nolist' that would also adjust the NonText highlight setting simultaneously.
If you use :set relativenumber (added in vim 7.3), :set showbreak=↳\ \ \ will reliably keep your 'showbreak' neatly lined up since the number width will not change as you navigate through the file. (This in addition to the :set cpo+=n and :set wrap #Randy Morris mentioned in his answer.)
You'll definitely need UTF-8 for the ↳ character, since it does not appear in other encodings. I'd strongly recommend you carefully document your encoding problems, with details about how to reproduce them along with your OS, its version, and the :version output of vim, and post them as separate questions. UTF-8 should be helping you wrangle multiple languages rather than being an impediment.

Resources