programming and vi - department of computer science …neeraj/doc/vi-lug.pdf · programming and vi...

14
Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology Delhi LUG@IITD Semina

Upload: lenga

Post on 02-Feb-2018

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

Programming and VI

Neeraj Goel

Dept. of Computer Science and Engineering

Indian Institute of Technology Delhi

LUG@IITD Seminar

Page 2: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

Outline

Editors

VI editor

A linux compilation session

Programming and VI editor

LUG@IITD Seminar

Page 3: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

Editors

gedit : A simple and ready to use editor with inbuild spellcheck 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 Seminar

Page 4: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

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 Seminar

Page 5: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

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 Seminar

Page 6: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

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 -lAll the files are previously compiled and then linked bygiving library information

Debugger: gdb, dddUse “gdb a.out” for debugging

LUG@IITD Seminar

Page 7: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

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.cgcc -c print.cgcc -lm junk.o print.o -o run.x

LUG@IITD Seminar

Page 8: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

C/C++ Compilation and Makefiles

Makefile helps in automation of compilation process

They make it fast, need not to compile all the files again andagain

How makefile worksMakefile will have targets, prerequisite and commandsLeft of colon is target, right of colon is prerequisite, linenext to target line is commandCommand line should be tabbed‘make’ will execute target given by ‘all’ or first target,else specify your target in command lineMake will resolve the dependencies recursively

All dependencies of a target should be resolvedbefore executing its command

LUG@IITD Seminar

Page 9: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

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 Seminar

Page 10: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

Vi and C programming

Indentation:set autoindentVisual mode and ‘=’

Auto match: use ‘%’

MarkerMark set: ‘ma’Mark recall: ’∼a’

Colored keywords

Running a bash command inside Vi using !e.g., !make!! - run previous command

LUG@IITD Seminar

Page 11: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

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 Seminar

Page 12: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

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 Seminar

Page 13: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

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 Seminar

Page 14: Programming and VI - Department of Computer Science …neeraj/doc/vi-lug.pdf · Programming and VI Neeraj Goel Dept. of Computer Science and Engineering Indian Institute of Technology

Thank You

Thank You

LUG@IITD Seminar