Tuesday, June 2, 2009

11 - Basics of the common UNIX and Linux text editors

===============================================================================
11 - Basics of the common UNIX and Linux text editors.
===============================================================================

Here we go with the text editors vi, ed and emacs.
ed is just explained for historical reasons.

-------------------------------------------------------------------------------
Most commonly used VI commands
-------------------------------------------------------------------------------


Here we go with the vi commands, these are unlogical but still good to know
because all computers doesn't have emacs, joe, pico and so on.
Solaris / SunOS comes default with vi as only text editor.
Vi has 2 basic modes, command mode and edit mode, you change between them
by pressing the Esc button, and to start to edit a file you must have a
free line, which you get by pressing, Esc followed by `o`.
vi is bound to be the hardest and most confusing text editor to learn,
and it has LOTS of commands, I included just a few of the most used
commands.

So here we go with the vi commands:

Inserting text

esc + i insert text informant of the cursor
esc + a append text after the existing text
esc + O opens new line above the current line
esc + o opens new line under current line (insert mode)

Deleting text

esc + x deletes one character
esc + 5x deletes five charters
esc + dw deletes a word
esc + 5dw deletes five words
esc + dd deletes the whole line
esc + D deletes the line from cursor and forward
esc + d) deletes the sentence from cursor and forward
esc + d( deletes the sentence from cursor and backwards
esc + u undelete

Note: esc + d) or d( removes the sentence from cursor and forward/backwards
until it reaches a dot "."

Moving around in VI:

Make sure you are in command mode and the following letters will do:

j moves you down
k moves you up
h moves you left
l moves you right

Finding Text

Hit esc then type in a / you then go to the bottom of the screen
where you will see your / type in the text to look for.
ie. /Linux
that will find the word `Linux` in the open file.

Replacing Text

Hit esc and do: :start,stop,s/s_text/r_text/g

: indicates that this is an ex command
start is the starting line number
stop is the stopping point
s is the substitute command
s_text is the search string (the text you are looking for )
r_text is the text you are replacing with
g is global

Example:

Esc + :5,8,s/l/ll/g

This would replace all "l"'s with "ll" on lines 5 to 8.

Note to Replacing Text:
Line numbers can also be:
. current line
$ last line

Basic save & quit commands

Hit Esc and do a : where after you can type the commands.

w write (save)
q quit
! force

ie. :q! or :wq

To create control characters do:

Ctrl+V Ctrl+

Example:

Ctrl+V Ctrl+A

That will create a ^A character.

(These last 3 commands are very alike ed commands)

Another useful thing in VI is split-screen mode, so you can edit 2 files
at once, this is:

:split

Just press Esc and type ":split".
You can do this in most big editors ..... but ofcorse in another way, you'll
see when you're reading the emacs section.

-------------------------------------------------------------------------------
Most commonly used ED commands
-------------------------------------------------------------------------------

ed is a very very old line editor, and the grand father of most editors,
perhaps even the grandfather of all editors, it dates back to the time of
the old CP/M machines, and is the father of the old DOS edlin line editor.
So out of historical perspective, it can be fun to know how to operate ed.

Creating a file in ed:

alien:~$ ed newfile
newfile: No such file or directory

Don't worry, as soon as you save it it will create it.
ED is pretty simple, here's an example (the "(ed says)" and "(we type)" is
just there to make it easier to follow the editing in this tutorial and is
not there in reality):

alien:~$ echo "abcd" >> newfile; echo "efgh" >> newfile; echo "ijkl" >> newfile
alien:~$ ed newfile
15 (ed says)
1,$ n (we type)
1 abcd (ed says)
2 efgh (ed says)
3 ijkl (ed says)
1 (we type)
abcd (ed says)
s/ab/ll (we type)
llcd (ed says)
$ n (we type)
3 ijkl (ed says)
a (we type)
here we end (we type)
. (we type)
w (we type)
27 (ed says)
q (we type)
alien:~$

Not all that hard is it ?
Here's a list of the most basic commands for ed:

1,$ n displays all lines with numbers
$ n display last line, with number
2 n takes you to line 2
s/new/old replaces old with new
a takes you to editor mode
. takes you to command mode
d deletes line
w write file (save)
q quit

-------------------------------------------------------------------------------

The final thing in this tutorial is a really quick look at emacs commands:

Most commonly used Emacs commands
-------------------------------------------------------------------------------

^A Cursor to the beginning of line
^E Cursor to the end of line
^K Deletes rest of line forward
^D Deletes current character
^L Horizontally center the current line
^S Search for a word forward in the file
^R Search for a word backwards in file

^Q Followed by Ctrl+, gives the real control character in a text file

^X ^F Open file
^X ^- ^- Undo
^C+Shift+- Undo
^X ^C ! Quit without saving
^X 2 split screen (horizontaly)
^X 3 split screen (verticaly)
^X O move to other screen (if in splitscreen mode)
^X ^W Save As
^X ^S Save

(^X 1 to get back a single window from splitscreen mode)

Shift+Esc Shift+5: Replace query (press y to replace words)
Meta+backspace: Deletes rest of word backwards (note "Meta" == "Alt")
Home: Takes cursor to the top of the file (Or equal to ^A)
End: Takes cursor to the end of the file (Or equal to ^E)
Delete: Deletes current character
Page Up / Page Down: Does what they say
Meta+X: Will load any emacs plugin, you may type any plugin name after pressing
the Meta+X (Alt+X), if you press TAB here once you will get a list of the
commands, if you type 'a' followed by a tab you will get all commands
starting with a and so on .... try: Meta+X doctor to try the
interactive eliza bot, or try telnet, ftp, webjump or shell.

To reach the menus "Buffers Files ..." etc. press F10
and if you wanna get out of the menus press ^G.

You may think that all this is weird, but know that emacs use to work
as a VERY primitive window manager, before the times of X.

Backspace and the arrow key's works as normal.

A tip is tp press: ^X then press 2 then press ^X and then O, now press
Meta+X and type 'shell', and you should have a split window, with a
shell in the lower one, so you can code or write in the upper one
at the same time as you have a shell in the lower one.
To change between the windows simply press: Ctrl+X and then press: o
A note is that if you want to run BitchX in the shell part you need
to start it with: BitchX -d, to get it in dumb terminal mode.

Usless or obsolete commands:

^I TAB
^O Move text forward
^P same as UpArrow
^F same as RightArrow
^J Enter/Return
^B same as LeftArrow
^N same as DownArrow
^M Enter/Return

^U Del deletes 4 characterss backwards
^U ^U Del deletes 16 characterss backwards
^U ^U ^U Del deletes 64 characterss backwards

All you really need to know to start using emacs is how to save and quit.

( ^X ^S ^X ^C will save and quit, a tip is: hold down ^ (Ctrl) and
press X S X C )

===============================================================================
-------------------------------------------------------------------------------
===============================================================================

This should be enough for you to start to script in bash, and make useful
scripts.
The only thing that limits what you can do is your imagination (well almost).

Go over this tutorial several times so you really understand everything.
If you accomplish that, you have a really good chance of learning UNIX well.

And that's what it's all about, to learn new things and explore new ways.
As long as you learn you live, not the contrary.

This tutorial turned out rather large, but I hope that those of you out there
that have the determination to learn shell scripting, also have the patinace
to read it all, and if not, you can always use it as a small dictionary.

I've got the question many times, which Linux distribution is the best ....
The question in it self is pointless and as ilitterat as asking
what version linux is up to....
The later question can only be answerd with a kernel version number,
and that is what Linux is, Linux is the kenrel and all distributions
use the same kernel, everything else in the system is just "stuff around
the kernel", to this point I've found that Mandrake is the distibution
that is most compleat for my needs, and it's suitable for beginners
aswell as for proffessionals, and it has nice configuration tools
that have been written especially for Mandrake.
But as I said, a Linux is a Linux, and the main difference between different
distributions is the package manager, where of rpm is the most stanard
and accepted, though I find Debians dpkg good aswell.
This is to the difference of distributions that have no indiginous package
manager like Slackware, that emulates a package manager with it's .tgz
package format (note that .tar.gz is not .tgz since .tgz should have it's
packages compressed with there path beginning from / )
Now there is nothing wrong with that if you like to compile most stuff on the
system your self, and many people preffer to do that.

My conclution is that the best distrinution is the one you personally
like the best, the one that fits _your_ needs.

So anyway, when you know bash scripting well enough, my suggestion is to
learn C programming, which, if you look at it with bash behind you,
isn't that hard.

So, I better go to bed and stop this nonses now.

Happy scripting all of you out there.

===============================================================================
--------------- Written by Billy Wideling <-> alien@koping.net ----------------
===============================================================================

No comments:

Post a Comment