Introduction
Basic Editors: nano and gedit
nano
gedit
More Advanced Editors: vi and emacs
vi (Vi IMproved)
Introduction to emacs
Knowledge Check (Verified Certificate track only)
Summary
Introduction
- 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.
![[/Untitled 73.png|Untitled 73.png]]
Basic Editors: nano and gedit
==nano==
- 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:
Command Usage **CTRL-G**Display the help screen. **CTRL-O**Write to a file. **CTRL-X**Exit a file. **CTRL-R**Insert contents from another file to the current buffer. **CTRL-C**Show cursor position.
==gedit==
- 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.
More Advanced Editors: vi and emacs
==vi (V====i== ==IM====proved====)==
Modes in vi
| ==Mode== | ==Feature== |
|---|---|
| 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
| ==Command== | ==Usage== |
|---|---|
**vi myfile** |
Start the editor and edit myfile |
**vi -r myfile** |
Start and edit myfile in recovery mode from a system crash |
**:r file2** |
Read in file2 and insert at current position |
**:w** |
Write to the file |
**:w myfile** |
Write out to myfile |
**:w! file2** |
Overwrite file2 |
:x or :wq |
Exit and write out modified file |
:q |
Quit |
:q! |
Quit even though modifications have not been saved |
Searching for Text in vi
**ENTER** key should be pressed after typing the search pattern.
| ==Command== | ==Usage== |
**/pattern** |
Search forward for pattern |
**?pattern** |
Search backward for pattern |
most important keystrokes used when searching for text in vi.
| ==Key== | ==Usage== |
**n** |
Move to next occurrence of search pattern |
**N** |
Move to previous occurrence of search pattern |
Changing Cursor Position
| ==Key== | ==Usage== |
| arrow keys | To move up, down, left and right |
**j** or **<ret>** |
To move one line down |
**k** |
To move one line up |
**h** or **Backspace** |
To move one character left |
**l** or **Space** |
To move one character right |
**0** |
To move to beginning of line |
**$** |
To move to end of line |
**w** |
To move to beginning of next word |
**:0** or **1G** |
To move to beginning of file |
**:n** or **nG** |
To move to line n |
**:$** or **G** |
To move to last line in file |
**CTRL-F** or **Page Down** |
To move forward one page |
**CTRL-B** or **Page Up** |
To move backward one page |
**^l** |
To refresh and center screen |
![[Commands_for_viLinuxFoundationX.pdf]]
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.
[[vi Practice]]
==Introduction to emacs==
- 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
| ==Key== | ==Usage== |
**emacs myfile** |
Start emacs and edit myfile |
**CTRL-x i** |
Insert prompted for file at current position |
**CTRL-x s** |
Save all files |
**CTRL-x CTRL-w** |
Write to the file giving a new name when prompted |
**CTRL-x CTRL-s** |
Saves the current file |
**CTRL-x CTRL-c** |
Exit after being prompted to save any modified files |
![[Commands_for_emacsLinuxFoundationX.pdf]]
Knowledge Check (Verified Certificate track only)
![[Screenshot_from_2022-06-21_13-30-54.png]]
![[Screenshot_from_2022-06-21_13-31-48.png]]
Summary
- Text editors (rather than word processing programs) are used quite often in Linux, for tasks such as creating or modifying system configuration files, writing scripts, developing source code, etc.
- nano is an easy-to-use text-based editor that utilizes on-screen prompts.
- gedit is a graphical editor, very similar to Notepad in Windows.
- The vi editor is available on all Linux systems and is very widely used. Graphical extension versions of vi are widely available as well.
- emacs is available on all Linux systems as a popular alternative to vi. emacs can support both a graphical user interface and a text mode interface.
- To access the vi tutorial, type vimtutor at a command line window.
- To access the emacs tutorial type Ctl-h and then t from within emacs**.**
- vi has three modes: Command, Insert, and Line. emacs has only one, but requires use of special keys, such as Control and Escape.
- Both editors use various combinations of keystrokes to accomplish tasks. The learning curve to master these can be long, but once mastered using either editor is extremely efficient.