LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-16-2004, 05:25 AM   #1
Dartelin
LQ Newbie
 
Registered: May 2004
Posts: 19

Rep: Reputation: Disabled
Vim and C++


I have a question about vim: how can i configure it for C++ writing ? I mean i would like to write the C++ code and then test it without getting out of vim. Thx.
 
Old 09-16-2004, 07:43 AM   #2
Boudewijn
Member
 
Registered: Nov 2003
Location: The Netherlands
Distribution: MDK: 10,10.1,10_amd64,9.2,9.1 . Debian: sarge,woody, Gentoo (X86 amd64 Sparc)
Posts: 219

Rep: Reputation: 30
Afaik vi(m) does colour the texts if you open a .cpp file. There's AFAIK no integrated compiler in vi(m).

To open a cpp file using vi type:

vi XXX.cpp
 
Old 09-16-2004, 07:46 AM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392Reputation: 2392
A little vim voodoo:

File number one, call it: compile.vim
Code:
" MAPS:
"
"  ,;   Compile current project file.
"  ,,;  Execute the compiled file. 
"
" NOTE:
"
"  The project file is the currently loaded file, unless
"  the global variable 'project' is defined:
" 
"    let project="test.c"
"
"  in which case that is the file to be compiled.
" -----------------------------------------------------------------------------
let project=""
let compile_file=""

augroup compiletex
  au!
  au bufenter *.c, nm ,; :call CompileC()<cr>
  au bufenter *.cc,*.cpp,*.c++ nm ,; :call CompileCC()<cr>
  au bufenter *.c nm ,,; :!./a.out<cr>
  au bufenter *.cc,*.cpp,*.c++ nm ,,; :!./a.out<cr>
  au bufenter *.vim,*vimrc* nm ,; :so % <cr>
augroup END

fu! CompileFile()
  if g:project==""
    let g:compile_file=expand("%")
  else
    let g:compile_file=g:project
  end
endf

fu! CompileC()
  w!
  call CompileFile()
  set makeprg=gcc
  set errorformat=%f:%l:\ %m
  exe "make ".g:compile_file
endf

fu! CompileCC()
  w!
  call CompileFile()
  set makeprg=g++
  set errorformat=%f:%l:\ %m
  exe "make ".g:compile_file
endf
The above file does all the actual work:
,; will compile your code,
,,; will execute your code.

The above file needs to be loaded in oder for it to work, place the following line in your .vimrc:

so /path/to/compile.vim

You might want to play with the following line (the execute line):

au bufenter *.cc,*.cpp,*.c++ nm ,,; :!./a.out<cr>

The above line will just execute the code, if it needs input oyu are able to give it and at the end you need to press return/enter to go back to your vi session.

You can also redirect it to a file, in this example it is redirected to out

note: if any input is required by your code, you need to give it 'blind':

au bufenter *.cc,*.cpp,*.c++ nm ,,; :!./a.out > out<cr>:e out<cr>

You might have noticed that this will not only compile/execute C++ (*.cpp,*.c++,*.cc) files, but C (*.c)files as well.

Hope this is what you wanted.

edit
PS: Hate those smileys....... Everywhere there's a smiley in the code it should say: : p (without the space in between).

Last edited by druuna; 09-16-2004 at 09:30 AM.
 
Old 09-16-2004, 08:23 AM   #4
deiussum
Member
 
Registered: Aug 2003
Location: Santa Clara, CA
Distribution: Slackware
Posts: 895

Rep: Reputation: 32
If you want to know the standard vim commands for compiling and running your code you can also use the following:

To compile your code with make, use:
:make

This works best if you have a Makefile, but if you just have a single .cpp file and no non-standard libraries you need to add to the command line, you can also do the following to compile a file called foo.cpp.

:make foo
(Note, no .cpp)

If there are compile errors it will bring you to the first error. You can then use the following to navigate the errors:

Next error
:cn

Previous error
:cp

List errors
:cl

View the compile log
:cope

To execute a program, just use something like:
:!./foo
 
Old 09-16-2004, 09:30 AM   #5
Dartelin
LQ Newbie
 
Registered: May 2004
Posts: 19

Original Poster
Rep: Reputation: Disabled
Ok thank u guys for the answer. Im just a beginner in C++ and would like to learn it. The first contact with C i had in Turbo C so i just wanted a similar program. Thx again.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
encryption with Vim (vim -x) mikshaw Linux - Software 2 03-07-2007 09:00 AM
vim bluetwist Debian 2 08-01-2005 08:43 PM
^M and vim jrdioko Linux - Software 5 08-04-2004 11:36 PM
VIM help bender_pondie Linux - Software 3 08-01-2003 10:58 AM
Better looking VIM? the_styler Linux - Newbie 5 04-04-2003 07:29 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:31 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration