Editors

Certify and Increase Opportunity.
Be
Govt. Certified Linux Administrator

Back Tutorial

Linux editors for plain text can be divided into two categories, graphical GUI editors and console text editors. The advantage of the GUI editor is intuitive user friendly interface while the benefit of the console text editor is the suitability over long distance network connections which may or may not provide suitable bandwidth or reliability which would both be required by the GUI editors for remote operation.

A text editor is just like a word processor without a lot of features. All operating systems come with a basic text editor. Linux comes with several. The main use of a text editor is for writing something in plain text with no formatting so that another program can read it. Based on the information it gets from that file, the program will run one way or another.

The text editor “vi”

The most popular text editor for Linux is called ‘vi’. This is a program that comes from UNIX. There is a more recent version called ‘vim’ which means ‘vi improved’. You have to remember a lot of key combinations to do stuff that other text editors will do for you more easily. Let’s make a text file, type – vi try_vi

You’ll see a line of tildes down the left side and the name ‘tryvi’ at the bottom and [new file]. To write something, you have to press ESC and the ‘i’ key (i for insert). Even if you don’t press ‘ESC-i’ it usually gets the idea that you want to type something and lets you do it after a few keystrokes. You should get used to the ‘ESC-i’ keys so you don’t end up writing ‘ar John’ instead of ‘Dear John’.

Press ESC + ‘i’ then type: hello vi

If you wrote jello vi or jello bi or something I don’t want to know about, you can always erase your mistakes with the backspace key. To save this file, you would press ESC then the colon key ‘:’ then ‘w’ (write). To save the file and quit vi, you would press ESC, ESC the colon key ‘:’ then wq (write, quit). To quit without saving, press ESC, ‘:’ then ‘q’. Vi may protest if you’ve written something and you don’t want to save it. If you press ESC ‘:’ ‘q!’ with an exclamation point, vi will accept it and not save your changes. There are a lot of commands in vi.

Common vi commands are

General Startup

vi filename To use vi
ZZ or :wq To exit vi and save changes
:q! To exit vi without saving changes
[esc] To enter vi command mode

Cursor Movement

h move left (backspace)
j move down
k move up
l move right (spacebar)
[return] move to the beginning of the next line
$ last column on the current line
0 move cursor to the first column on the current line
^ move cursor to first nonblank column on the current line
w move to the beginning of the next word or punctuation mark
W move past the next space
b move to the beginning of the previous word or punctuation mark
B move to the beginning of the previous word, ignores punctuation
e end of next word or punctuation mark
E end of next word, ignoring punctuation
H move cursor to the top of the screen
M move cursor to the middle of the screen
L move cursor to the bottom of the screen

Inserting

r replace character under cursor with next

Character typed

R keep replacing character until [esc] is hit
i insert before cursor
a append after cursor
A append at end of line
O open line above cursor and enter append mode

Deleting

x delete character under cursor
dd delete line under cursor
dw delete word under cursor
db delete word before cursor

Copying Code

yy (yank)’copies’ line which may then be put by the p(put) command. Precede with a count for multiple lines.

Miscellaneous Commands

. repeat last command
u undoes last command issued
U undoes all commands on one line
xp deletes first character and inserts after second (swap)
J join current line with the next line
^ Gdisplay current line number
% if at one parenthesis, will jump to its mate
mx mark current line with character x
‘x find line marked with character x

Emac

Emacs is one of the oldest and most versatile text editors available for Linux and UNIX-based systems. It’s been around for a long time (more than twenty years for GNU emacs) and is well known for its powerful and rich editing features. Emacs is also more than just a text editor; it can be customized and extended with different “modes”, enabling it to be used like an Integrated Development Environment (IDE) for programming languages like Java, C or Python.

Emacs uses control and escape characters to distinguish editor commands from text to be inserted in the buffer. When we say “ CTRL-X ” it means to hold down the control key, and type the letter x. “ESC-X ” means to press the escape key down, release it, and then type x.

Buffers – The best things about programming in Emacs is ,when you edit a file in emacs, you’re not really editing the file itself, as it sits out on a disk somewhere. Instead, emacs makes a copy of the file, and stores the copy in a part of RAM memory called a buffer. All the changes you make to the file are applied to the buffer. When you save the file, emacs writes the contents of the buffer to the disk.

Because the buffer exists in RAM memory, it disappears if the power is turned off, or if the system crashes. Thus, you should use the save command often, flushing your current buffer to disk. Once the file is on disk, a power outage or system crash shouldn’t harm it. The buffer files are usually represented as the Original file name ending with ‘ ~ ’ (tilda).

<machine>% ls

Makefile     test1.c     test.c     test1.c~

Here are some of the fundamental things you’ll need to do when you edit a document in emacs.

Starting emacs – To start emacs, just type the command at the command prompt.

<machine>% emacs

to the UNIX shell. If you want emacs to start with a file already loaded into a buffer, type

<machine>% emacs filename

Quitting emacs – To exit emacs and return to the UNIX shell, type CTRL-X-CTRL-C. Press control key and press ‘X’ and then again with the control key pressed , hit ‘c’. If you have made changes to the buffer since the last time you saved it to disk, emacs will ask you if you want to save. Type y for yes or n for no.

Getting help – Emacs has an on-line help system that can be invoked by typing CTRL-H. If you type the question mark (?), emacs will present a list of help topics you can choose.

Aborting a command – You can abort an emacs control or escape sequence by typing the command CTRL-G.

Working with files – To read a disk file into an emacs buffer, type the command CTRL-X-CTRL-F. Emacs will ask you for the name of the file. When you have entered the file name, press the return key, and emacs will load the file into a buffer, and display it in the text window.

The command to save the contents of the buffer to a disk file is CTRL-X-CTRL-S. The save command overwrites the old version of the file. You may also write the contents of the buffer to a different file with the command CTRL-X-CTRL-W. Emacs will prompt you for the name of the file you want to create.

To create a new file, use CTRL-X-CTRL-F, just as if the file already existed. When emacs asks you for the file name, type in the name you want your new file to have, and emacs will create the file, and display an empty buffer for you to type in. Emacs will perform file name completion for you. Type part of the name of the file you want, and press the spacebar or tab key to get emacs to complete a file name. If the partial name you’ve given matches more than one file, emacs will display a list of all potential matches. You can continue typing in more of the file’s name, and pressing either file completion key, until you zero in on the file you want.

Cursor motion – On well-configured systems, you will find that the keyboard arrow keys will function correctly in emacs, moving you forward or backward one character at a time, and up or down one line at a time. If the arrow keys do not work, here’s how to accomplish the same functions:

  • CTRL-F moves the cursor forward to the next character.
  • CTRL-B moves the cursor back to the previous character.
  • CTRL-N moves the cursor to the next line.
  • CTRL-P moves the cursor to the previous line.
  • In addition to basic cursor motion, emacs provides some other handy cursor motion functions:
  • CTRL-A moves the cursor to the start of the current line.
  • CTRL-E moves the cursor to the end of the current line.
  • ESC-F moves the cursor forward to the next word.
  • ESC-B moves the cursor back to the previous word.
  • ESC-< moves the cursor to the start of the buffer.
  • ESC-> moves the cursor to the end of the buffer.

Inserting and deleting text – To insert text into a buffer, place the cursor where you want to start inserting text, and start typing away. If you want to insert the contents of another file into the current buffer, place the cursor at the desired insertion point, and type Control-X-I. Emacs will ask you for the name of the file you wish to insert. You may also insert text by cutting it from one place, and pasting it at the insertion point.

Deleting text is easy. As you’d expect, the delete key deletes backward one character. Here are some other ways to delete text:

  • CTRL-D deletes forward one letter.
  • CTRL-K deletes from the point to the end of the line.
  • ESC-D deletes forward one word.
  • ESC-delete deletes backward one word.

Cutting and pasting text regions – Emacs allows you to select a region of text, and perform cut and paste operations on the region. It uses a temporary storage area called the “kill buffer” to allow you to store and retrieve blocks of text. There is only one kill buffer in emacs, which means that you can cut text from one document, and paste it into another.

To define a region of text, place the cursor at one end of the region and press Control-spacebar. That sets the mark. Then, move the cursor to the other end of the region. The text between the mark and the cursor defines the region. To cut a region of text, and place it in the kill buffer, use the command Control-W (think of Wipe). The paste command is Control-Y. It Yanks the block of text from the kill buffer, and places it where the cursor rests. The Control-Y command only retrieves the most recently-cut block of text.

You can paste in earlier cuts by pressing ESCAPE-Y. The ESCAPE-Y command, used repeatedly, will take you back through several previous text blocks that were cut. The ESCAPE-Y command does not work unless you type Control-Y first. You may copy a region of text into the kill buffer without cutting it. Define the text block by setting the mark at one end, and moving the cursor to the other end. Then type ESCAPE-W.

Undoing changes – It is possible to undo the changes you have made to a file by entering the command Control-_. (That’s Control-underscore.)

editors

Nano

nano is a text editor for Unix-like computing systems or operating environments using a command line interface. It emulates the Pico text editor, part of the Pine email client, and also provides additional functionality. In contrast to Pico, nano is licensed under the GNU General Public License (GPL). Though not as powerful as Emacs or Vim, it is easy to learn and use. Nano is ideal for making small changes to existing configuration files or for writing short plain text files.

Most nano commands are invoked by holding down the Ctrl key (that is, the control key), and pressing one of the other keys. In this text, the control key is referred to using ^. For example, ^X means “hold down the CTRL key and press the x key”. Most of the important commands are listed at the bottom of your screen.

^G       nano help

Starting nano – To edit a file called filename, type nano filename.

In nano, you can insert another file:

^R        read an existing file into nano (inserted at the current cursor position)

^T       opens a browser that allows you to select a file name from a list of files and directories

Navigation – The usual mouse-based point-and-click method is not supported by nano. Use the arrow keys to move around the page in nano. Other navigation commands:

^A       move to beginning of line

^E        move to end of line

^Y       move down a page

^V       move up a page

^_        move to a specific line (^_^V moves to the top of the file, ^_^Y to the bottom)

^C       find out what line the cursor is currently on

^W      search for some text.

When searching, you will be prompted for the text to search for. It searches from the current cursor position, wrapping back up to the top if necessary.

Editing – Insert new text at the current cursor position just by typing the text in.

Delete commands:

^D       delete character currently under the cursor

BackSpace       delete character currently in front of the cursor

^K       delete entire line

^\         search for (and replace) a string of characters

Cut and paste – ^K does not delete lines permanently; the most recent set of deletions are stored in a buffer. These lines may be re-inserted at the current cursor location using ^U. This may be used to simulate cut and paste:

  • Repeatedly use ^K until all of the text you want to move has been deleted.
  • Move to the line that you want to insert the text at, and use ^U.

Pressing ^U more than once will cause multiple copies to be inserted. This is particularly useful if you want to copy text:

  • Repeatedly use ^K until all of the text you want to copy has been deleted.
  • Press ^U immediately to put a copy back in its original location.
  • Move to the line that you want to copy the text to, and use ^U.

Saving and Exiting

^O       save contents without exiting (you will be prompted for a file to save to)

^X       exit nano (you will be prompted to save your file if you haven’t)

^T       when saving a file, opens a browser that allows you to select a file name from a list of files and directories

editors-01

Apply for Linux Administration Certification Now!!

http://www.vskills.in/certification/Certified-Linux-Administrator

Share this post
[social_warfare]
BASH shell variables, commands and scripting
Users

Get industry recognized certification – Contact us

keyboard_arrow_up