How do I make :copen use a file instead of makeprg? - vim

I want to run make | tee errors.err and then have :copen use errors.err to open the quickfix list and jump to any compilation errors.

You're looking for :cfile [errorfile] instead of :copen.
Read the error file and jump to the first error.

Related

Execute vim pipe only when system() reports an error?

I have the following vim auto command:
autocmd BufWritePost *.go :cex system('revive '..expand('%:p')) | copen
It executes a binary called revive, passing it the current file path, and then opens up the quickfix window with the results from the revive binary.
But if the revive binary returns empty I'd prefer not to have the quickfix window opened.
Wondering if there's a better way to handle this? Is there a way to check for an error status or checking the length of the quickfix results before opening?
Thanks!
#romainl solved this for me by commenting about :cwindow.
The use of :cwindow prevents the quickfix window opening unless there are error items in the list.
Thanks Romainl

Automatically go to next compile error in Vim

Using Vim, is there a way to automatically go the next line you had a compile error? Is there a way to bind that to a key? I believe this feature exists in emacs.
Let's say you compiled on the command line and now know which lines of your code have errors so you go back to editing your code using Vim. You remember the first line that had an error, so you jump to that line and fix your typo. How do you go to the next error line without exiting vim and looking at the command line again?
I understand you could use a terminal multiplexer like tmux to aid this process, but that's not really what I'm going for here.
If your compiler generates a file containing a list of errors (let's call it errorfile) you can simply do:
$ vim -q errorfile
and then use the following commands:
:cn[ext] " jump to next error
:cp[revious] " jump to previous error
:cc 12 " jump to error number 12
:copen " open the quickfix window
See :help quickfix for the complete list of quickfix commands.
Alternatively, you can compile directly from Vim:
:make
and use the quickfix commands above.
See :help :make for setup/usage instructions.

How to use tidy with vim without unix linebreak in quickfix window and how to correct only the errors?

After a lot of searching and trying I found this to make my tidy work with vim:
:set makeprg=tidy\ -e\ --gnu-emacs\ yes
:set shellpipe=2>
:set errorformat=%f:%l:%c:\ %m
:make %
:copen
But why does the output in the quickfix window has an ^M unix line-break at the end of every line?
I tried to remove it but the content in quickfix window is not modifiable.
I tried also to make tidy correct the errors, but only the errors.
I created this:
let errorf = "d:\\error.txt"
let currentf = expand("%:p")
let writef = "d:\\".expand("%:t:r")."_tidy.".expand("%:e")
exe a:type."!tidy -w 0 -f ".errorf." -o ".writef." ".currentf
exe ":bot split ".writef
exe ":bot split ".errorf
But this changes the complete output of my file.
I want to correct only the errors.
I have read the manual of tidy but can't find a simple option to correct only the errors without changing the rest of the file.
p.e.
<h1>test</h2> --> <h1>test</h1>
Are there tidy users who know how to change only the errors in tidy?
You can pipe the output of tidy to sed to create a filter to remove carriage returns before vim puts it in the quickfix window.
set makeprg=tidy\ -e\ --gnu-emacs\ yes\ $*\ \\\|\ sed\ 's/\\r$//'
The pipe needs to be escaped twice one by set and once for the interpretation of the command.
The sed command I used was
sed 's/\r$//
Which removes carriage returns that appear at the end of the line.
Have a look at the syntastic plugin. It supports tidy as one of the syntax checkers for HTML.

How do I make vim open all files matching a pattern in different tabs?

In a given working directory, if I do
:tabe **/test*.py
vim complains with E77: Too many file names. What if I want it to open every matching file in a separate tab? There must be a way to do it, but I can't find it.
You could use the args list and argdo like so:
:args **/test*.py
:argdo tabe %
However, the syntax event is turned off by argdo (to speed up the normal use case), so the files will be loaded without syntax at first. You could follow it up with a :syntax on to force the syntax event on all loaded buffers. Compressed into one line (need to wrap argdo in execute so it doesn't absorb the following |):
:args **/test*.py | execute 'argdo tabe %' | syntax on
Alternately, you can open vim from the command line via:
vim -p **/test*.py
But that will max out at 10 tabs.
You can use the following:
:next **/test*.py
It opens all the files.
To map it
nmap <c-d> :args **/*.tpl<bar>execute 'argdo tabe %'<bar>syntax on<cr>
But still it displays list of files, you have to press enter few times (depending of number of files).
This functionality can be included as a command in your .vimrc file:
"open all files in seperate tabs
command -nargs=1 OpenAll call <SID>openAll(<f-args>)
function! s:openAll(dir)
execute 'args ' . a:dir
silent argdo tabe %
syntax on
endfunction
With this function running :OpenAll **/*.py from vim will quickly open all files into new tabs
None of the other answers works for me, but this is fine:
find <path> -iname <pattrn> | xargs -o vim -p
all files are visible in different tabs
file lookup is recursive
Note, vim can limit tabs - to be changed by set tabpagemax=42.
Also, if you wonder how to close all tabs at once, use :qa

Format code with VIM using external commands

I know that using VIM I can format C++ code just using
gg=G
Now I have to format 30 files, so doing it by hand becomes tedious. I had a look how to do it passing external commands to VIM, so I tried
vim -c gg=G -c wq file.cpp
but it does not work.
Can you give me a hint?
Thanks
Why not load all the files up in buffers and use bufdo to execute the command on all of them at one time?
:bufdo "execute normal gg=G"
Change -c gg=G to -c 'normal! gg=G'. -c switch accepts only ex mode commands, gg=G are two normal mode commands.
I prefer a slight change on the :bufdo answer. I prefer the arg list instead of the buffer list, so I don't need to worry about closing current buffers or opening up new vim session. For example:
:args ~/src/myproject/**/*.cpp | argdo execute "normal gg=G" | update
args sets the arglist, using wildcards (** will match the current directory as well as subdirectories)
| lets us run multiple commands on one line
argdo runs the following commands on each arg (it will swallow up the second |)
execute prevents normal from swallowing up the next pipe.
normal runs the following normal mode commands (what you were working with in the first place)
update is like :w, but only saves when the buffer is modified.
This :args ... | argdo ... | update pattern is very useful for any sort of project wide file manipulation (e.g. search and replace via '%s/foo/bar/ge' or setting uniform fileformat or fileencoding).

Resources