Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

Vim G++

Download as pdf or txt
Download as pdf or txt
You are on page 1of 3

lab.

md 2024-02-12

Lab Guide for writing C++ using Vim and G++


Writing C++ Code with Vim
Vim is a highly configurable text editor built to enable efficient text editing. It's an improvement over the
original vi editor distributed with UNIX and is available by default on most UNIX systems.

Getting Started with Vim

Opening Vim: Open your terminal and type vim filename.cpp to start writing a new C++
program. Replace filename.cpp with your desired file name.
Entering Insert Mode: Press i to enter insert mode. In insert mode, you can type your code as you
would in any text editor.
Saving and Exiting: Press Esc to return to normal mode.Then enter :wq or press ZZ to save your file
and quit Vim simultaneously.

Basic Vim Commands:

i: Enter insert mode.


esc: Return to normal mode, where you can enter commands.
:w: Save your file but remain in Vim.
:q: Quit Vim.
:wq or ZZ: Save your file and quit Vim.
:q!: Quit Vim without saving changes.

If you have Vim installed, you can use the vimtutor program to learn and get comfortable with Vim.
vimtutor is a 30-minute interactive tutorial for beginners that teaches the fundamentals of Vim, such as
inserting text, deleting text, and searching within files. To start it, simply open your terminal and type
vimtutor.

Navigating Vim More Effectively


Vim operates in several modes, with the primary ones being Normal Mode, Insert Mode, and Command
Mode. Mastering the basics of navigating in Vim can significantly enhance your coding efficiency.

Basic Navigation in Normal Mode

h: Move left
j: Move down
k: Move up
l: Move right

Jumping Around

0: Jump to the beginning of the line


^: Jump to the first non-blank character of the line
$: Jump to the end of the line

/
lab.md 2024-02-12

gg: Jump to the start of the file


G: Jump to the end of the file
:{line_number}: Jump to a specific line number

Searching

/{pattern}: Search forward for a pattern


?{pattern}: Search backward for a pattern
n: Repeat the search in the same direction
N: Repeat the search in the opposite direction

Editing Commands

i: Enter insert mode


o: Open a new line below and enter insert mode
O: Open a new line above and enter insert mode
x: Delete the character under the cursor
dd: Delete the current line
yy: Yank (copy) the current line
p: Paste below the cursor
P: Paste above the cursor

Basic Configuration with .vimrc


The .vimrc file allows you to customize Vim settings. Create or modify this file in your home directory
(~/.vimrc) to personalize Vim.

Editing .vimrc: Open .vimrc with Vim by running vim ~/.vimrc from your terminal.
Reloading .vimrc: After saving your changes, you can apply them without restarting Vim by running
:source ~/.vimrc.

Basic .vimrc Configuration for C++

" Enable syntax highlighting


syntax on

" Set tabs to 4 spaces


set tabstop=4
set shiftwidth=4
set expandtab

" Enable line numbering


set number

" Set indentation rules for C++ code


set cindent
set smartindent

" Enable auto-indentation


set autoindent
/
lab.md 2024-02-12

" Highlight search results


set hlsearch

" Enable mouse support


set mouse=a

Compiling C++ Code with g++


g++ is a part of the GNU Compiler Collection (GCC) and is used to compile C++ programs.

Compiling Your Code

Basic Compile Command: In the terminal, type g++ filename.cpp -o outputname where
filename.cpp is the name of your C++ file, and outputname is the name of the executable to be
created.
Running Your Program: After compiling, run your program by typing ./outputname in the
terminal.

Debugging C++ Code with gdb

The GNU Debugger (gdb) allows you to see what is going on inside another program while it executes or
what another program was doing at the moment it crashed.

Basic gdb Commands:

Compiling for gdb: To use gdb, you need to compile your program with the -g option. This includes
debugging information in the executable. Example: g++ -g filename.cpp -o outputname.
Starting gdb: Type gdb outputname in the terminal.
Common gdb Commands:
run: Start your program.
break main: Set a breakpoint at the beginning of the program (you can replace main with any
function name or line number).
next: Execute the next program line (after stopping at a breakpoint).
print variable_name: Print the current value of a variable.
continue: Continue running the program until the next breakpoint.
quit: Exit gdb.

Practice for Lab Exam

Please try out and get comfortable with using these tools on your own computers as the lab exam will be
held on computers that have only these tools installed on them.

1. Write your code in Vim: Open Vim and write your C++ code.
2. Compile your code with g++: Use the g++ compiler to turn your code into an executable.
3. Debug your code with gdb: If your program isn't working as expected, use gdb to step through your
code and find the issue.

You might also like