sczdavos
sczdavos

Reputation: 2063

Compile error or warning will close project in VIM

I'm using VIM for writing some basic C algorithms. I have some key binded to SCCompile plugin and when I press it it will compile my program. If there is no problem it will show me press any key, and then I'm back in VIM with opened file. But if there is some problem or warning, it will just show me that error and I don't know how can I get back to my file.

VIM compile error

Is there some command how can I get back to project? Or do I need reopen it?

There is my .vimrc file:

if v:lang =~ "utf8$" || v:lang =~ "UTF-8$"
   set fileencodings=ucs-bom,utf-8,latin1
endif

set nocompatible    " Use Vim defaults (much better!)



set bs=indent,eol,start     " allow backspacing over everything in insert mode
filetype off                  " required

" set the runtime path to include Vundle and initialize
set rtp+=~/.vim/bundle/Vundle.vim
call vundle#begin()
" alternatively, pass a path where Vundle should install plugins
"call vundle#begin('~/some/path/here')


" All of your Plugins must be added before the following line
call vundle#end()            " required
"set ai         " always set autoindenting on
"set backup     " keep a backup file


set viminfo='20,\"50    " read/write a .viminfo file, don't store more
            " than 50 lines of registers
set history=50      " keep 50 lines of command line history
set ruler       " show the cursor position all the time

set nocompatible
filetype plugin indent on
syntax on
silent! runtime macros/matchit.vim
set autochdir
set backspace=indent,eol,start
set foldenable
set hidden
set incsearch
set laststatus=2
set ruler
set switchbuf=useopen,usetab
set tags=./tags,tags;/
set wildmenu
nnoremap gb :buffers<CR>:sb<SPACE>
set number
set tabstop=4
set shiftwidth=4
set softtabstop=4
set fo=cqt

" Only do this part when compiled with support for autocommands
if has("autocmd")
  augroup redhat
  autocmd!
  " In text files, always limit the width of text to 78 characters
  autocmd BufRead *.txt set tw=78
  " When editing a file, always jump to the last cursor position
  autocmd BufReadPost *
  \ if line("'\"") > 0 && line ("'\"") <= line("$") |
  \   exe "normal! g'\"" |
  \ endif
  " don't write swapfile on most commonly used directories for NFS mounts or USB sticks
  autocmd BufNewFile,BufReadPre /media/*,/mnt/* set directory=~/tmp,/var/tmp,/tmp
  " start with spec file template
  autocmd BufNewFile *.spec 0r /usr/share/vim/vimfiles/template.spec
  augroup END
endif

if has("cscope") && filereadable("/usr/bin/cscope")
   set csprg=/usr/bin/cscope
   set csto=0
   set cst
   set nocsverb
   " add any database in current directory
   if filereadable("cscope.out")
      cs add cscope.out
   " else add database pointed to by environment
   elseif $CSCOPE_DB != ""
      cs add $CSCOPE_DB
   endif
   set csverb
endif

" Switch syntax highlighting on, when the terminal has colors
" Also switch on highlighting the last used search pattern.
if &t_Co > 2 || has("gui_running")
  syntax on
  set hlsearch
endif

filetype plugin on

if &term=="xterm"
     set t_Co=8
     set t_Sb=[4%dm
     set t_Sf=[3%dm
endif

" Don't wake up system with blinking cursor:
" http://www.linuxpowertop.org/known.php
let &guicursor = &guicursor . ",a:blinkon0"

autocmd Filetype c nmap <buffer> <F5> :SCCompileAF -std=c99 -Wall -Wextra -pedantic <CR>

execute pathogen#infect()

Upvotes: 0

Views: 169

Answers (1)

Peter Rincker
Peter Rincker

Reputation: 45177

From what I can tell the SCCompile plugin is supposed to make it easier to compile thing in Vim. I cannot vouch for this claim but the plugin's documentation states that if an error during compiling then you should use the quickfix list.

To understand this process it is probably easier to first understand Vim's native :make / quickfix workflow.

:make

Running :make will execute the make program, 'makeprg'. Then parse the output with 'errorformat' and put each entry into the quickfix list.

Typically 'makeprg' defaults to the make command which runs a Makefile. However you can change your 'makeprg' via the :compiler command or set it directly.

You can also pass extra arguments to :make. e.g. :make clean

Note: running :make will often show the output of the command at the bottom of the screen with a prompt asking to press enter. Just press enter as all the output will be available in the quickfix list.

quickfix list

After you run :make your quickfix list will hold the output of compiler.

Use the following command to navigate the quickfix list.

  • :copen to open the quickfix window
  • :cclose to close the quickfix window
  • :cnext/:cprev to move to the next/previous item
  • :cc to print out at the bottom the current error or :cc 3 to show a specific error in this example 3

Personally I use Tim Pope's unimpaired plugin to navigate the quickfix list.

Back to using SCCompile

From what I can tell SCCompile is wrapper around :make that handles setting up 'makeprg' and/or 'errorformat'. Therefore SCCompile is just a drop in replacement for :make. To compile just execute your SCCompile mapping and then use the quickfix list to check your errors.

Please take a look at SCCompile's documentation: :h SingleCompile-overview and :h SingleCompile-contents.

Conclusion

As you are a newbie then I would suggest creating a Makefile and using :make because that will keep things very simple and as well as have a nice record of how you compiled your program.

I would also suggest you look into create a mapping to run :make. Example:

nnoremap <f5> :make<cr>

For more information see

:h :make
:h 'makeprg'
:h 'errorformat'
:h :compiler
:h quickfix
:h :cc
:h :cope
:h :cnext

Upvotes: 1

Related Questions