Skip to content

Text Editors

  • How to create and edit files using the available Linux text editors.
  • nano, a simple text-based editor.
  • gedit, a simple graphical editor.
  • vi and emacs, two advanced editors with both text-based and graphical interfaces.

  • nano is easy to use, and requires very little effort to learn.

  • To open a file, type nano <filename> and press Enter. If the file does not exist, it will be created.

  • nano provides a two line shortcut bar at the bottom of the screen that lists the available commands. Some of these commands are:

    CommandUsage
    CTRL-GDisplay the help screen.
    CTRL-OWrite to a file.
    CTRL-XExit a file.
    CTRL-RInsert contents from another file to the current buffer.
    CTRL-CShow cursor position.
  • can only be run within a Graphical Desktop environment. It is visually quite similar to the Notepad text editor in Windows
  • To open a new file find the program in your desktop’s menu system, or from the command line type gedit <filename>. If the file does not exist, it will be created.

Modes in vi

ModeFeature
Command- By default, vi starts in Command mode. Each key is an editor command. Keyboard strokes are interpreted as commands that can modify file contents.
Insert- Type i to switch to Insert mode from Command mode. Insert mode is used to enter (insert) text into a file. Insert mode is indicated by an “? INSERT ?” indicator at the bottom of the screen. Press Esc to exit Insert mode and return to Command mode.
Line- Type : to switch to the Line mode from Command mode. Each key is an external command, including operations such as writing the file contents to disk or exiting. Press Esc to exit Line mode and return to Command mode.

[[vimtutor]]

Most important vi commands

CommandUsage
vi myfileStart the editor and edit myfile
vi -r myfileStart and edit myfile in recovery mode from a system crash
:r file2Read in file2 and insert at current position
:wWrite to the file
:w myfileWrite out to myfile
:w! file2Overwrite file2
:x or :wqExit and write out modified file
:qQuit
:q!Quit even though modifications have not been saved

Searching for Text in vi

**ENTER** key should be pressed after typing the search pattern.

CommandUsage
/patternSearch forward for pattern
?patternSearch backward for pattern

most important keystrokes used when searching for text in vi.

KeyUsage
nMove to next occurrence of search pattern
NMove to previous occurrence of search pattern

Changing Cursor Position

KeyUsage
arrow keysTo move up, down, left and right
j or <ret>To move one line down
kTo move one line up
h or BackspaceTo move one character left
l or SpaceTo move one character right
0To move to beginning of line
$To move to end of line
wTo move to beginning of next word
:0 or 1GTo move to beginning of file
:n or nGTo move to line n
:$ or GTo move to last line in file
CTRL-F or Page DownTo move forward one page
CTRL-B or Page UpTo move backward one page
^lTo refresh and center screen

Using External Commands in vi

  • Typing **sh** command opens an external command shell. When you exit the shell, you will resume your editing session.
  • Typing **!** executes a command from within vi. The command follows the exclamation point.
  • This technique is best suited for non-interactive commands, such as : **! wc %**. Typing this will run the **wc** (word count) command on the file; the character % represents the file currently being edited.
  • Rather than having different modes for command and insert, like vi, emacs  uses the **CTRL** and Meta (**Alt** or **Esc**) keys for special commands.

Working with emacs

KeyUsage
emacs myfileStart emacs and edit myfile
CTRL-x iInsert prompted for file at current position
CTRL-x sSave all files
CTRL-x CTRL-wWrite to the file giving a new name when prompted
CTRL-x CTRL-sSaves the current file
CTRL-x CTRL-cExit after being prompted to save any modified files