CPL Week1
CPL Week1
WEEK 1
Objective: Getting familiar with the programming environment on the computer and writing the
first program.
Suggested Experiments/Activities:
Tutorial 1: Problem-solving using Computers.
Lab1: Familiarization with programming environment
i Basic Linux environment and its editors like Vi, Vim & Emacs etc.
ii Exposure to Turbo C, gcc
iii Writing simple programs using printf(), scanf()
Date:
i) Basic Linux environment and its editors like Vi, Vim & Emacs etc.
Emacs and Vim
Introduction
You've probably never heard of emacs or Vim, especially if you're just starting out your career
in software engineering. It is also normal that as a windows Operating System user, you've only
been working with the notepad, notepad++, sublime text, visual studio code, and all other
common text editors.In the Linux operating system, Emacs and vi are the major editors that are
used to manipulate files.
Emacs
Emacs (Editor Macros) was developed by David A Moon in 1976 making it one of the oldest
pieces of software which exists today. GNU Emacs, the most used variant(fork) of Emacs, was
released in 1985 by Richard Stallman, the founder of GNU/Linux.
Emacs is a highly advanced, extensible, and customizable text editor that also offers an
interpretation of the Lisp programming language at its core. Different extensions can be added to
support text editing functionalities.
Emacs has the following features:
User documentation and tutorials
Syntax highlighting using colors even for plain text.
Unicode supports many natural languages.
Various extensions including mail and news, debugger interface, calendar, and many
more.
Vi(m)
Vim (Vi Improved), as the name suggests is an improved clone of the text editor. The text editor
was originally developed by Billy Joy in 1976 for the proprietary Unix operating system. Bram
Moolenar later enhanced vi and released it as vim in 1991.
Vim is a powerful command-line-based text editor that has enhanced the functionalities of the
old Unix Vi text editor. It is one the most popular and widely used text editors among System
Administrators and programmers that is why many users often refer to it as a programmer’s
editor. It enables syntax highlighting when writing code or editing configuration files.
The UNIX vi editor is a full-screen editor and has two modes of operation:
1. Command mode commands which cause action to be taken on the file, and
2. Insert mode in which entered text is inserted into the file. In the command mode, every
character typed is a command that does something to the text file being edited; a
character typed in the command mode may even cause the vi editor to enter the insert
mode.
In the insert mode, every character typed is added to the text in the file; pressing
the (Escape) key turns off the Insert mode.
While there are several vi commands, just a handful of these is usually sufficient for
beginning vi users.
A good example is Vi iMproved, also known as Vim, which gradually became more
popular than Vi as it added more features and improvements that are not found in Vi.
While both Vim and Emacs support plugins that enhance their functionality, Emacs supports a
lot more customization of the editor environment. This is arguably the most important feature of
emacs and is responsible for much of emacs' devoted following.
Emacs can be extended on the fly by redefining built-in elisp functions, either by typing
the new definition into Emacs or by loading elisp files. Groups of related changes are called
"modes" and can be easily configured to be automatically used for particular types of files
(buffers). So it is easy to define modes for different programming languages or frameworks such
as "lisp mode" or "Ruby on Rails mode" or "PHP mode". These modes can directly modify even
core behaviors of Emacs, automatically format or colorize text and add standard template or
"boilerplate" text such as function declarations and closures.
Programmers find Emacs to be far more customizable to their particular requirements than
Vim. Yet some programmers may find Vim to ultimately be better suited to their programming
environment due to its equally customizable and fully modal operation.
Emacs can be extended in elisp, while Vim has its own internal scripting language and
supports the use of other programming languages for plugin development.
While Emacs can be extended in elisp, Vim has its own internal scripting language and supports
the use of other programming languages for plugin development can also be extended on the fly
by using the command mode. The command mode allows configuration options to be set,
functions to be defined, and macros to be made. Configuration files for Vim are just commands
that can be input through the command mode.
1. Graphical User Interface (GUI)
Both Vim and Emacs have graphical user interfaces. Virtually all menu items on the graphical
interface for both editors are simply ways to handle a shortcut command or quick configuration.
The GUIs for the editors do not provide virtually any additional functions beyond those available
in the CLI (command-line interface).Emacs uses XDisplay or gtk2 for its GUI. Vim can use
many other GUI libraries, such as gtk, gnome, gnome2, motif, athena, and neXtaw, in addition to
gtk2.
1. Vi is lighter and faster than Emacs.
2. Emacs is more customizable than Vi.
3. Emacs can emulate Vi but not the other way around.
4. Emacs later developed a GUI while Vi did not.
5. Emacs continued developing while Vi was succeeded by its variants.
BASIC VI EDITOR COMMANDS
To start vi:
Example: > vi letter will open a new file called letter to edit, or if letter already exits, open the
exiting file.
Command Effect
vi +/patter filename edit filename starting at the first line containing pattern
i before cursor
a after cursor
} end of paragraph
{ beginning of paragraph
To Delete Text:
The d command removes text from the Work Buffer. The amount removed depends on the
Repeat Factor and the Unit of Measure you enter after d. If you delete by mistake: give the
command u (undo) immediately after you give the delete command.
Examples: 3dd will delete 3 lines beginning with the current line.
3dw or d3w will delete 3 words
Command Action
Changing Text
Example: cw allows you to change a word. The word may be replaced by as many word as
needed. Stop the change by hitting < esc >.
c3w allows you to change 3 words.
Ending an Editing Session
Command Effect
:q quit
:w filename saves to filename (allows you to change the name of the file)
Miscellaneous commands
Command Effect
:r filename reads filename into the current file at the location of the cursor
:set showmode displays INPUT MODE at the lower right hand corner of screen
F1 For Help,F2 Save,F3 Open,F4 Go to cursor,F5 Zoom,F6 Next,F7 Trace into,F8 Step over,F9
Make,F10 Menu,Alt+X Quit,Alt+Bksp Undo,Shift+Alt+Bksp Redo,Shift+Del Cut,Ctrl+Ins
Copy,Shift+Ins Paste,Ctrl+Del Clear,Ctrl+L Search again,Alt+F7 Previous error,Alt+F8 Next
error,Ctrl+F9 Run,Ctrl+F2 Program reset,Alt+F9 Compile,Alt+F4 Inspect,
ctrl+F4 Evaluate/Modify,Ctrl+F3 Call stack,Ctrl+F8 Toggle breakpoint,Ctrl+F5
Size/Move,Alt+F3 Close,Alt+F5 User screen,Alt+0 List all,Shift+F1 Index,Ctrl+F1 Topic
search,Alt+F1 Previous topic,Ctrl+F7 Add watch
GCC: The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project
supporting various programming languages.GCC is a key component of the GNU tool chainand
the standard compilerfor most projects related to GNU and Linux, the most notable is the Linux
kernel. The Free Software Foundation (FSF) distributes GCC under theGNU General Public
License(GNUGPL). GCC has played animportant role in the growth offree software, as both a
tool and an example.When it was first released in 1987, GCC 1.0 was named the GNU C
Compiler since it onlyhandled the C programming language . It was extended tocompile C++ in
December of that year. Front ends/w were later developed for Objective-C
Compiling:Method 1:
$ gcc filename.c
If No syntactical errors it gives $ prompt. If any syntactical errors are there in the program, it
displays the errors. Correct the errors by opening the file using vi filename.c
Method 2:
If no syntactical errors are there in the program,It displays the errors.Correct the errors by
opening the file using vi filename.c
Executing:
For Methods 1:
$./a.out
For Methods 2:
$./filename