3.3. Conventional Text Editors.
Section quotes:
- "I'd crawl over an acre of 'Visual This++' and 'Integrated Development
That' to get to gcc, Emacs, and gdb. Thank you."
-- By Vance Petree, Virginia Power
- "[VI] It is very powerful, which means it can be hard to do simple things,
BUT it also means that complicated thing are equally hard to do."
-- Romain Lerallut, The VI nano-HOWTO for the NewbieDoc Project.
Section contents:
3.3.1. Introduction.
Conventional text editors are part of Unix customs,
the first part of GNU project was EMACS - a text editor,
those text editor are very powerful, they are far from having an overloaded look,
but it's lovely and common between experts, they like them more than
nice looking GUI text editors.
Most configuration files are text files, and even those text editors
could be used by developers to replace IDEs. We will talk
about text editors that can work even without X graphical interface,
we have GUI text editors like 'gedit', 'kate' and more,
that are full of features like spell checking, syntax coloring
(useful for programming) and many more, but we won't talk about them,
the idea behind this section is that X is not always available
for example when X server has bad configuration and you are editing it,
or when you don't want to bother the server with drawing icons.
To be a good Unix user you have to know them (it's something you put in your CV),
We will talk about Vi, ViM, Emacs and Nano full-screen editors, don't ask LUG members what
do they prefer, there is a flame war between ViM and Emacs, and we will talk
about 'ed' line-oriented text editor.
Tip
'mcedit' is a very simple console text editor comes with 'mc' package,
make sure you install it (while you are not expert on otther console text editors),
to open it's main menu press F9.
There are many more reasons, conventional editors like VIM and Emacs has
unconventional features, they could be used to automate many borning hard works
on the fly at your finger tips, you could record a macro as a compound
of entry processes and operations (moveing to the begining/ending, searching,...),
then apply it on any other part of the text.
Tip
If you are familiar with 'WordStar' or 'Borland (Turbo) C for DOS',
then you may like 'joe' which has the same key binding.
3.3.2. VI text editor.
The first full-screen text editor in history! it's usually
installed not just as part GNU/Linux but also with many
rescue disks or minized base systems. With master distributions
'vi' is just a link to the new improved VIM, but it could
be provided by other (less featured packages) like 'nvi' or 'elvis'.
At first look, you will not be impressed, it has a voodoo like
behavior , the key to understand all that is to know that it works modes,
the first default mode (called normal mode) takes commands and operations,
and the other is for text entry (called insert mode) to activate it press 'i',
to go back to normal/command mode press 'ESC', at your terminal command line(bash)
type 'vi myfile', before your start typing press 'i',
now you can do normal editing, to exit press 'ESC' (to start normal mode) then
type ':q', if the file is modified you have to save it before exit
with typing ':wq', or to force edit without saving
type ':q!' with any of those don't forget to press '[ENTER]'
(we said type not press), to search press '/' (in normal mode) then enter
the text you searching for, to save the file type ':w'
at normal mode, to save it as another file, type ':w FILENAME',
you could open/edit another file ':e FILENAME',
yes 'vi' is multiple document editor, to move between files type
':prev' or ':next', also you could split the screen
with typing ':split', to move between the splited windows
use 'CTRL+w j' and 'CTRL+w k'.
VIM is similar, don't miss it.
3.3.3. VIM text editor.
|
Vi Improved (VIM), is very powerful text editor with millions of features,
type 'vim' at command line, run graphical versions like 'gvim' or 'kvim'
(option could be used for some features like '-A' for Arabic support if compiled),
use VIM the same way as vi, if you are not yet familiar with VIM you
could use 'evim' which is a link to vim that case it to run easy featureless mode.
|
|
Tip
To learn VIM first could run 'kvim' or 'gvim' twice then
press on one F1 for help and double click some section using the mouse
or use arrows and CTRL+']', read them and text them on the other VIM.
As in Vi it start in normal mode (operations and commands),
in which you could move with arrows to specify the position
where you apply some operation. We did not learn any so let's move
insert mode, press 'i' and use it as any text editor, move with arrows and type.
One of VIM best features is auto-completion for example type 'internationalization'
some where move other where and you want to type it, just type part of like 'inter'
then press 'CTRL+X' to enter completion mode then press
'CTRL+P' or 'CTRL+N' to complete it (if there
s many words start with 'inter' press it many times),
if you are typing a file name , just type part of it then press
'CTRL+X' then 'CTRL+F' and if there are many files use
'CTRL+P' and 'CTRL+N' to move between them,
another completion mode is dictionary use with CTRL+X followed by
CTRL+K, and you could make the dictionary yourself
as any file where each word on a line by itself.
Writing C++ files with VIM is now more easy, for example at 'gvim'
select 'syntax->CD->C++', while you are typing
C++ codes VIM indent blocks to have very readable file,
if you are editing an HTML file, select 'syntax->HIJK->HTML'
(submenus of syntax menu are alphabet letters initials of syntax name)
|
|
Let's have a real world experiment, open VIM and press 'i' then type:
Hello, world! I'm using VIM.
It's cool.
Look this is the 1st statement. And this is the 2nd one. And
there is more.
This is the second paragraph.
press ESC to back to normal mode, try cursor movement commands:
| k | one line upward |
j | one line downward |
l | next character |
h | previous character |
I know, you say to yourself that you will never usethem, well we'll see,
let's now take more useful commands:
| 0 | goto line begining |
^ | goto first non blank char on the line. |
$ | goto end of the line |
| |
| w | next word (at the first character) |
e | next word end(last character of the word) |
b | prevois word |
| |
| ( | next statement |
) | prevois statement |
{ | next paragraph |
} | previos paragraph |
altogether we have the following diagram:
k (up line) w/b word forward/backward
/\ e end of word
(prev letter)h <- -> l(next letter) 0/^/$ 1st/1st-nonblank/end of line
\/ '(' ')' prev/next statement
j (down line) '{' '}' prev/next paragraph
you could specify number (before) operation like '3w'
which moves to next 3 words forward, and so on.
Let's talk about operations that modify the text, for example 'x'
will delete character at the cursor, the operation 'd'
followed by a movement operation will delete all characters between the curent
and after move positions, for example put the cursor over the third line,
just before the 'A' of 'And' in the second statement then press 'd('
and the whole statement will be removed.
And this is the 2nd one. And
this is very useful if you are writing a long line
with many statements and you want to remove the last one, just press ESC
then (at the end of last statement) 'd' and '(' then press 'i' and continue typing.
Usually when you make a mistake, you delete it then type the correct words,
se VIM offer them in one operations called change 'c' which works
the sameway as d but it enter insert mode after it deletes,
if you undo last 'd' and replace it with 'c(foobar [ESC]'
it will give:
foobar And this is the 2nd one. And
Unlimited multi level undo, is one of VIM features, also VIM has a clipboard
where it save deleted text(like cut in GUI), move at any place you want to
paste deleted text then press 'p' (short for put), you could
do that many times, for example 'ppp' will paste the deleted
text three times, to copy (to clipboard) text without deleting it
use 'y' the same way as d, for exampe y3w will store
next 3 words on VIM clipboard, move the cursor any where then press 'p'
to put a copy of them there. You could repeate last change operation with '.'
but on the current version (moving does not count here), for example put
the cursor on the first of 'Hello', then press
'cwWelcome[ESC]' which will change this word to 'Welcome',
now move tthe cursor on 'world' (using 'w','l' or arrows), then press
'.' and it will become 'Welcome'. VIM could save 26 actions
(sequence of operations) and give it a name from lower case English alphabet,
those are called macros. First we record a macro by doing the action,
then stop recoding, move to other locations and apply it, to start
recoding, move to normal mode with '[ESC]' then press
'q' followed by it's a letter (to be it's name, only a-z),
do the action (type operation,...) then press 'q' to stop
recoding and store it, to apply this macro (any where) while in normal more
press '@' folloed by it's name, for example to record a macro called 'a',
that change the third word from the current line to 'Hello'
(while in normal mode) press 'qa02wcwHello[ESC]q',
to test our macro move to any line and press '@a'
(while in normal mode).
To move the cursor to first occurence of some letter (only one character)
use 'f' or 'F' (forward or backword repectively) followed by the letter,
to repeate last search press ';', for real advanced searching,VIM offers
support for Regular Expressions (RE for short), it's a very powerful
method to represent what you are searching for, some characters has a special
meaning, for example '|' which mean 'or', see
'4.2. Back to command line.' section.
If you are searching for an email address that the user is the same as the
service provider (like ahmad@ahmad.com)
then the corresponding RE is '\([A-Za-z0-9]\+\)@\1.com',
to search for this RE forward type it after '/',
or backwork with '?', and to repeate last search press 'n'.
To search and replace a RE match with a text, use ':substitute'
or for short ':s', also you could specify lines to
search for example from 2 to 15 using ':2,15s',
to replace over all the text use '%' as range to be ':%s',
any way follow them with RE then replacement then options all separated by '/',
for example ':%s/Ali/Ahmad/gc', those options 'g' (global)
to replace all matches (many in the same line), option 'c' is used
to confirm replacement, you could use all RE features for example
to match whole word only use '<>'
for example ':%s/\<Ali\>/Ahmad/gc',
or this example (from VIM manual page) that will swap two statement
separated with a comma (to replace 'foo bar' with 'bar foo')
type ':%s/\([^,]*\), \(.*\)/\2 \1/'.
There are another VIM mode called 'visual mode', in this mode you type
moving operations then VIM will shade a region, then you press action
operations (unlike normal mode where you press action then movement).
To enter this mode, there are three methods, first press 'v',
look at this line:
This is a very long long line with meaningless english text.
|----------------------->
let's assume you want to delete the underline words (in normal mode you press 'd25l')
move to first letter of 'very', then press 'v' then move the cursor
with arrows (or 'l','w',...etc) now press 'd' and we are done.
the selected area has two end points, to move between them press 'o'
while you are not yet apply the operation. The next method is with 'V'
this is called 'linewise visual mode', in which you select the whole line
if part of it was hit. The third method is entered with 'CTRL+V',
it's called 'blockwise visual mode' in this mode you select
a rectangular block, it's useful when you have columns and you want
to edit a column, like the following table and you want to remove
Ahmad's column:
Ali Ahmad Belal
exam1 10 8 6
exam2 8 8 7
exam3 9.5 7 6
put the cursor over Ahmad's A then press 'CTRL+V',
move to this word end with 'e', move for lines down with '4j'
or using arrows, then press 'd' to remove the column.
The selected area has 4 corners to move between them use
'o' or 'O'.
Another example, type:
#include <gl.h>
#include <glu.h>
#include <glut.h>
#include <glui.h>
let's assume that you want to change files name between '<>'
to be 'gl/gl.h', 'gl/glu.h', 'gl/glut.h'
and 'gl/glui.h'put the cursor on 'g' of 'gl.h'
then press 'CTRL+V' then move down with arrows then press
'Igl/[ESC]' where 'I' is insert operation (not mode, it keeps in visual mode)
this will insert 'gl/', before pressing
'[ESC]' you will see change on one line, but
after it, changes will apply to the block.
3.3.4. Emacs text editor.
There are many jokes found on it's package tells you where EMACS
name came from. This program is now a 'lisp' interpreter (an AI language),
on which Emacs were written at it's first stages, it's core now rewritten
on C and can have modules loaded at run time written on 'lisp'.
Emacs has many features like syntax coloring and indention for many
programming languages, games, completion, browsing the web, reading emails and
even helping the blinds (this is not an exaggeration). Unlike VIM, Emacs
works in single mode (or to be more specific Emacs modes does not mean
you can't type text as usual), to do some task you press a sequence of
keys that usually starts with 'CTRL' which is written in
documentation as 'C' for example 'C-x'
mean you hold CTRL and press 'x', another common key is Meta written 'M',
for example 'M-x' means 'META+x',
you ask where on earth this Meta key come from! it's a key
was common on computers that Emacs was designed on (long long ago, it was first GNU part),
in recent keyboards it could be 'ALT', for example 'M-x' means
'ALT-x'.
To find your first steps on Emacs see the tutorial with 'C-h t',
to read it's GNU information pages press 'C-h i', to see FAQs
press 'C-h F'. Each Emacs operation or action has a long name
(there exist actions that has no hot keys)m for example 'C-h i'
full name is info, to run an action using name press 'M-x' followed
by action name, for example 'M-x info' look at the buttom line
while you are typing commands, command names could be complicated and long like
'help-with-tutorial', that's why Emacs offer completion,
you could press '[TAB]', like GNU BASH which actually
borrow this feature from Emacs.
Emacs offers modes that in each it offer more features related to the mode,
for example 'cc-mode' for C,C++ and Java,
you will find a 'compile' on menus, and other related tools,
another mode is 'text-mode' which will be activated once
you entered a '.txt' to make a human readable file.
There are many tools (modes) like calender, choose it from tools menu
then you also get a new goto menu which could be used to move through it
even using other calenders, to know the first of Ramadan select Islamic
calender from goto menu, then enter the year,month name (eg. 'ramadan')
then the day (eg. 1), in this mode you could also display moon phases from
moon menu select Lunar phases, othter modes offers some games like AI asychiatrist (doctor) that
you could "talk" to (using your keyboard), type any thing then press ENTER
twice, see 'tools' menu.
To open a file press 'C-x C-f', to wait for a dialog box, look
down there, you see a place to type it's name (you could use TAB),
to save 'C-x C-s', to save as 'C-x C-w',
and to exit 'C-x C-c'.
3.3.5. Nano text editor.
A very simple small yet easy to use text editor that
depends on very few libraries, it look and feel as 'pico' with
more features. Debian selected it as it's default editor while installing.
Type 'nano' optionally followed by a file name to open,
you will see a screen with sections, big section on top (editing area),small
one on buttom (help and status), this help syntax like
'^G' which mean 'CTRL+G',
to get help press 'CTRL+G', to open (read) a file 'CTRL+R',
to save (write out) it 'CTRL+O', to justify text
'CTRL+J', to search (where is) 'CTRL+W', to check spelling
'CTRL+T' and to exit 'CTRL+X'.
3.3.6. Ed text editor.
Ed line-oriented text editor unlike full-screen text editors
it has ugly prompt where you could enter commands to select a line
then edit it by typing it's contenet again! this needs less memory
to hold the program and the text buffer being edited, this what old computers
at that time could do, there are many jokes about GNU Ed and GNU EMACS
found at GNU humor, one tells you why 'ed' is better than VIM and Emacs.
Type 'ed' and you will get... well, nothing!! this is a prompt to type commands,
you could pass an argument to 'ed' the file you want to edit, for example
'ed readme.txt'. Like Vi we have two modes the defualt command
mode and input mode, at the prompt type a command for example 'a' to add
text, then press ENTER (you should press ENTER after each command),
then you enter input mode and type what ever you like (more than one line
if you wish), then when you finish type period '.' on a line by itself
to stop input and go back to command mode, 'p' command will print
part of the buffer by default the current line:
bash$ ed myfile.txt
I'm using Ed the best text editor,
not only because it comes alphabetically first
before Vi and EMACS.
.
p
before Vi and EMACS.
commands could take a range, for example to display from line 1 to line 3
type '1,3p', and to display the second line type '2p',
special line numbers could used like (where N and M are integer numbers):
| . | The current line. |
| $ | The Last line. |
| - | The previous line. |
| + | The next line. |
| N | The N-th line, 0 is before the first line. |
| -N | The line N steps upward. |
| +N | The line N steps downward. |
| N,M | From line N to line M |
| , | as '1,$' - all from first line to last line. |
| ; | as '.,$' - all from the current line to last line. |
| any lower case letter | Line marked with that letter using 'k' command. |
so to display all lines press ',p', to save use write command
by typing 'w' followed by the filename (if you like), to quit type 'q'
(another 'q' if the buffer is modified). Basic command found on the next table:
| Na | start input to add just after N, by default it's '.' |
| Rd | remove the region R, by defalt '.,.' |
| Rc | remove region R ('.,.' by default) then start input to add and replace it. |
| Rp | print region R by default '.,.' |
| N | let line N be the current. |
| RmN | move region R just after N |
| Rs/RE/STR/g | search for RE and replace it with STR |
| Rg/RE/COMMANDS | search for RE and apply COMMANDS on matches |
| NkI | mark line N with name I that is [a-z]. |
| ! COMMAND | run COMMAND using shell and display it's output. |
| e !COMMAND | drop current buffer and run shell COMMAND and edit it's output. |
| e FILE | drop current buffer and open FILE. |
| w FILE | save the buffer as FILE. |
| q | quit |
where N is a line specifier (need not be a number, it could be '-'),
and R is a range specifier in the form N,M or just N.
Let's continue on our prevois session
,p
I'm using Ed the best text editor,
not only because it comes alphabetically first
before Vi and EMACS.
p
before Vi and EMACS.
1a
ignore next lines
.
,p
I'm using Ed the best text editor,
ignore next lines
not only because it comes alphabetically first
before Vi and EMACS.
g/EMACS/d
s/first/first./
,p
I'm using Ed the best text editor,
ignore next lines
not only because it comes alphabetically first.
2m-2
2m+1
,p
ignore next lines
not only because it comes alphabetically first.
I'm using Ed the best text editor,
w delme.txt
q
make sure you don't forget to use the period '.' to end 'a' or 'c' input,
and notice that 'm-' is meaningless.
|
Best viewed with free web browsers
You may get more high quality software
from here for free

Generously Hosted by www.JadMadi.net
|