0% found this document useful (0 votes)
85 views14 pages

Programming and VI: Neeraj Goel

This document discusses the VI text editor and its use for programming. It provides an overview of VI basics like modes, commands, searching and replacing text. It also describes how to use VI for C/C++ programming tasks like indentation, running makefiles, debugging code. The document encourages customizing VI through environment settings to enhance productivity.

Uploaded by

Sachin Panwar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
85 views14 pages

Programming and VI: Neeraj Goel

This document discusses the VI text editor and its use for programming. It provides an overview of VI basics like modes, commands, searching and replacing text. It also describes how to use VI for C/C++ programming tasks like indentation, running makefiles, debugging code. The document encourages customizing VI through environment settings to enhance productivity.

Uploaded by

Sachin Panwar
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 14

Programming and VI

Neeraj Goel

Dept. of Computer Science and Engineering


Indian Institute of Technology Delhi

LUG@IITD Semina
Outline

Editors
VI editor
A linux compilation session
Programming and VI editor

LUG@IITD Semina
Editors

gedit : A simple and ready to use editor with inbuild spell


check and other tools.
emacs : A simple, powerful and extendibles editor
vi : A lightweight and fast editor for programmers
pico : Used as default in pine (a text based mail client)

LUG@IITD Semina
Vi editor

Why Vi? Fast and easy

Various versions and clones : Vi, Vim, Gvim

In and Out
$ >vi filename.extension
Out :q, :wq :q! (quit, write and quit, quit without save, respectively)

Basic modes- edit and command,


Vi will open in command mode
‘i’, ‘a’, ‘o’ are used to enter in edit mode (‘i’ for insert, ‘a’ for append, ‘o’ for open a
new line)
“esc” can be used to return to command mode from edit mode
Command mode
‘d’ ‘x’ for deletion, ‘y’ for yank, ‘p’ for paste
Commands starting from ‘:’, e.g., :w for write, :e open another file for editing, :1, :$
for page surfing
Commands starting from ‘/’ - search and replace

LUG@IITD Semina
Vi continues

Searching using ‘/’


In command mode use ‘/’ then write the word you want to search
‘n’ for forward search, ‘N’ for backward search
Tip Keep the cursor on the word you want to search and press ‘∗’
Search and replace
:s/ram/mohan - will search string “ram” and replace with “mohan”
:%s/ram/mohan - will replace first ouccarnaces of string “ram” with “mohan” in all
lines
:%s/ram/mohan/g - will replace all ouccarnaces of string “ram” with “mohan”

Visual mode - start with pressing v in command mode; up and down keys can be used to
select area/region
gq can be used for fixing line length
left shifting (using «), right shifting (using ») can be done

LUG@IITD Semina
C/C++ Compilation

Unix made by programmers for programming


Gcc compiler :gcc for ‘c’, g++ for ‘c++’
Various options, -O,-c,-g,-I
‘-O’ sets optimization level
‘-c’ only compile not link
‘-g’ for debug
‘-I’ for pre-processing only
Linking with -l
All the files are previously compiled and then linked by
giving library information
Debugger: gdb, ddd
Use “gdb a.out” for debugging

LUG@IITD Semina
C example files

Two input files—junk.c and print.c


Compilation method 1:
gcc junk.c print.c -lm -o run.x
Compilation method 2:
gcc -c junk.c
gcc -c print.c
gcc -lm junk.o print.o -o run.x

LUG@IITD Semina
C/C++ Compilation and Makefiles

Makefile helps in automation of compilation process


They make it fast, need not to compile all the files again and
again
How makefile works
Makefile will have targets, prerequisite and commands
Left of colon is target, right of colon is prerequisite, line
next to target line is command
Command line should be tabbed
‘make’ will execute target given by ‘all’ or first target,
else specify your target in command line
Make will resolve the dependencies recursively
All dependencies of a target should be resolved
before executing its command

LUG@IITD Semina
Example of Makefile

CC = gcc
CFLAGS = -g -Wall -O4
#CFLAGS += -I.
#LDFLAGS = -L/usr/local/admxrc2/library -ladmxrc2
LDFLAGS = -lm

TARGET = run.x
SRC = junk.c print.c
OBJ = $(SRC:%.c=%.o)

all: $(TARGET)

$(TARGET):$(OBJ)
$(CC) $(CFLAGS) $(OBJ) $(LDFLAGS) -o $(TARGET)
$(%.o) : %.c
$(CC) -c $(CFLAGS) $(SRC) -o $@ $<

clean:
rm -f core *.o $(TARGET) *˜ LUG@IITD Semina
Vi and C programming

Indentation
:set autoindent
Visual mode and ‘=’
Auto match: use ‘%’
Marker
Mark set: ‘ma’
Mark recall: ’∼a’
Colored keywords
Running a bash command inside Vi using !
e.g., !make
!! - run previous command

LUG@IITD Semina
Vi environment setting

dot exrc or dot vimrc


My .vimrc
"This is a comment
set autoindent
set smartindent
set showmatch
set tabstop=4 " number of spaces in a tab
set softtabstop=4 " as above
set shiftwidth=4 " as above
"set expandtab " always turn tabs into spaces. (you might want smarttab)
set pastetoggle=<F8>

LUG@IITD Semina
Other interesting stuff about Vi

Vi in read only mode (vi -R)


It remember previous commands used (: and up down keys)
It keeps the recent data yanked or deleted
Vi can undo (and then redo) lot of previous steps
‘u’ for undo, ‘ctr r’ for redo
Vi can recover file from swap file (using vi -r filename)
but always delete swap file after recovery
Can write macros for auto completion, auto correction etc.

LUG@IITD Semina
To leave you with

Vi is effective, easy and fast


Best suitable for small editing jobs and programming
Easily extendable!
There is lot more you can do with Vi - explore and enjoy!

LUG@IITD Semina
Thank You

Thank You

LUG@IITD Semina

You might also like