• There's no question that at some point you will have to work with text files. You'll have to edit them, change their contents, create new ones, etc. And for that, you need an editor.


  • The two most common ones used in Linux are vi and Emacs.


  • But there are alternatives that are very easy to use, you can just start using right away, such as nano and Gedit.


  • Whether you're a system administrator or a developer, you will have to edit text files, as well as to create them.


  • Graphical system administration applications can help you avoid working directly with text files, as [they] can integrate the development environments, however, it's far more flexible and often easier to just use a simple text editor.


  • For one thing, distributions differ in terms of what graphical administration utilities they have, but they all have the same basic editors and many of the same basic configuration files.


  • As with everything else, Linux has a lot of choices for how you do this.


  • Many of these will befamiliar if you've ever worked in any kind of UNIX type of system. The usual editors, that are used most often, are vi and emacs, and they are completely compatible with versions used on other operating systems.


  • These both have very long histories, they are always available on every Linux installation, though one will often not find emacs in a default installation.


  • This is a little bit of product of the silly, holy wars that have existed between emacs and vi lovers.


  • Each of these editors, vi and emacs, have a pure text-based form that runs perfectly well in a non-graphical environment, and they also have X-based graphical forms that would extend their capabilities.


  • One editor you can use from the command line is nano. It's very simple to use, it has virtually no learning curve, and it's much easier to get started with than either vi or emacs if you haven't used them before.


  • All you want to do is make a small change in a file and create a file. nano with a simple enough choice.


  • gedit, on GNOME-type systems and KWrite, on KDE desktop environments are also very simple graphical editors that require little or no learning curve, they're both extremely capable, and configurable, and can produce very pretty displays.


  • If you just want to create a file, without even doing an editor, there are two standard ways to just create one from the command line and fill it with content.


  • The first is to just use echo repeatedly:


  •  
    $ echo line one > myfile
    $ echo line two >> myfile
    $ echo line three >> myfile
    
    
  • This sort of game is often played from scripts.


  • A second way is to use cat combined with redirection:


  •  
    $ cat  << EOF > myfile
    > line one
    > line two
    > line three
    > EOF $
    
    
  • either of which produces a file which has in it:


  •  
    line one
    line two
    line three
    
    
  • It is the most common text editor you will find on any Linux system.


  • vi is short for view.


  • The actual name of the program was probably vim, to which vi has been the alias to as its substitute name.


  • Even if you don't really want to use vi, you should gain familiarity with it.


  • There will be times when you have no other choice, it is on every system, and all your colleagues will probably be familiar with it.


  • There are graphical interfaces such as gvim and kvim, for new users, these may be easier to use at first.


  • Everything you do with vi, you do through the keyboard.


  • You do not have to use a mouse unless you're using a graphical version.


  • There's one confusing part about vi, is that there are two fundamental modes, command mode and insert mode, and you toggle between each with the escape key.


  • The same letter typed in either mode does very different functions, so it can be confusing to learn at first.


Command Description
vi myfile Start vi and edit myfile
vi -r myfile Start vi and edit myfile in recovery mode from a system crash
:r file2<RET> Read in file2 and insert at current position
:w<RET> Write out the file
:w myfile<RET> Write out the file to myfile
:w! file2<RET> Overwrite file2
:x<RET> or :wq<RET> Exit vi and write out modified file
:q<RET> Quit vi
:q!<RET> Quit vi even though modifications have not been saved
Command Description
arrow keys Use the arrow keys for up, down, left and right; or:
j or <RET> One line down
k One line up
h or Backspace One character left
l or Space One character right
0 Move to beginning of line
$ Move to end of line
w Move to beginning of next word
b Move back to beginning of preceding word
:0 <RET> or 1G Move to beginning of file
:n <RET> or nG Move to line n
:$ <RET> or G Move to last line in file
^f or PageDown Move forward one page
^b or PageUp Move backward one page
^l Refresh and center screen
Command Description
/pattern<RET> Search forward for pattern
n Move to next occurrence of search pattern
string<RET> Search backward for pattern
N Move to previous occurrence of search pattern
Command Description
a Append text after cursor; stop upon Escape key
A Append text at end of current line; stop upon Escape key
i Insert text before cursor; stop upon Escape key
I Insert text at beginning of current line; stop upon Escape key
o Start a new line below current line, insert text there; stop upon Escape key
O Start a new line above current line, insert text there; stop upon Escape key
r Replace character at current position
R Replace text starting with current position; stop upon Escape key
x Delete character at current position
Nx Delete N characters, starting at current position
dw Delete the word at the current position
D Delete the rest of the current line
dd Delete the current line
Ndd or dNd Delete N lines
u Undo the previous operation
yy Yank (cut) the current line and put it in buffer
Nyy or yNy Yank (cut) N lines and put it in buffer
p Paste at the current position the yanked line or lines from the buffer
Command Description
emacs myfile Start emacs and edit myfile
Ctl-x i Insert prompted for file at current position
Ctl-x s Write out the file keeping current name
Ctl-x Ctl-w Write out the file giving a new name when prompted
Ctl-x Ctl-s Write out all files currently being worked on and exit
Ctl-x Ctl-c Exit after being prompted if there any unwritten modified files
Command Description
arrow keys Use the arrow keys for up, down, left and right; or:
Ctl-n One line down
Ctl-p One line up
Ctl-f One character left
Ctl-b One character right
Ctl-a Move to beginning of line
Ctl-e Move to end of line
M-f Move to beginning of next word
M-b Move back to beginning of preceding word
M-< Move to beginning of file
M-x goto-line n Move to line n
M-> Move to end of file
Ctl-v or PageDown Move forward one page
M-v or PageUp Move backward one page
Ctl-l Refresh and center screen
Command Description
Ctl-s Search forward for prompted for pattern, or for next pattern
Ctl-r Search backwards for prompted for pattern, or for next pattern
Command Description
Ctl-o Insert a blank line
Ctl-d Delete character at current position
Ctl-k Delete the rest of the current line
Ctl-_ or Ctl-x u Undo the previous operation
Ctl-space Mark the beginning of the selected region; the end will be at the cursor position
Ctl-w Yank (cut) the current marked region and put it in buffer
Ctl-y Paste at the current position the yanked line or lines from the buffer