Shell scripts are plain text files. They cannot contain font styles, rich formatting, or hidden document metadata the way word processors do. Any editor that saves plain text can create a script, including modern editors such as Notepad++ or VS Code.
In Unix and Linux environments, however, you often work on remote systems where a graphical editor is not available. For that reason, this module teaches vi (and its common modern variants) so you can create and edit scripts directly from a terminal session.
The most widely available terminal editor on Unix-like systems is
vi.
Most systems include either
vi itself or a compatible implementation such as
vim (Vi IMproved) or
nvim (Neovim).
Even if you prefer another editor day-to-day, vi is a valuable baseline skill because it works in minimal environments (including rescue shells, single-user mode, and remote SSH sessions).
vi is mode-based. The two modes you will use most are:
- Command mode: keystrokes are interpreted as editing commands.
- Insert mode: keystrokes are inserted into the file as text.
vi starts in command mode. You can always return to command mode by pressing
Esc.
Unix systems often include multiple text editors. If you already know one well, you can use it to create scripts in this course.
Common editors include:
- vi/vim/nvim — ubiquitous terminal editors used on nearly every Unix-like system.
- emacs — powerful and highly configurable; often available on developer-focused systems.
- nano (or pico) — beginner-friendly editors with on-screen help.
- ed — a classic line editor; useful historically and in constrained environments.
- joe — a straightforward terminal editor with familiar key bindings.
If you are working on a system with a graphical desktop, you may also have GUI editors. In server environments, terminal editors are often the default.
A shell script is a text file that contains one or more commands. Typically, each line is treated as a command (or part of a command).
For example, this is valid script content:
df -k
ls
A critical detail for modern systems: your script file must use Unix line endings.
If you create a script on Windows and upload it to Linux without converting line endings, the shell may report errors such as a “bad interpreter” message.
Using vi/vim on the target system avoids this problem because it naturally writes Unix-style text files.
The first thing most users learn about vi is its modes:
Command mode is used for navigation and editing actions (delete, copy, paste, search).
Insert mode is used to type text into the file.
vi starts in command mode. Common ways to enter insert mode include:
i — insert text before the cursor
a — append text after the cursor
o — open a new line below the current line and begin inserting
To leave insert mode and return to command mode, press
Esc. If you are unsure what mode you are in, press
Esc twice to ensure you return to command mode.
The following commands are enough to comfortably edit scripts in a Unix terminal session.
Practice them in the exercise that follows.
| Task |
Command |
What it does |
| Enter insert mode |
i, a, o |
Insert/append text (leave with Esc) |
| Save |
:w |
Write (save) the file |
| Quit |
:q |
Quit (fails if there are unsaved changes) |
| Save and quit |
:wq |
Write changes and quit |
| Quit without saving |
:q! |
Exit and discard changes |
| Move cursor |
h j k l |
Left, down, up, right |
| Jump |
0, $, gg, G |
Line start, line end, top of file, bottom of file |
| Delete |
x, dd |
Delete character, delete line |
| Copy and paste |
yy, p |
Yank (copy) line, paste below cursor |
| Undo |
u |
Undo last change |
| Search |
/text, n |
Search forward; repeat match |
Quick script workflow tip: A common pattern is “edit → save → execute.”
For example, you might create a script file and run it like this:
$ vi hello.sh
$ chmod +x hello.sh
$ ./hello.sh
(In the next lessons, you’ll build scripts that print output and accept user input.)
Click on the Exercise link below to practice using vi in the UNIX Lab.
Unix Text Editor - Exercise
The next lesson introduces a command to write information to the screen from a script.