Viewing & editing files
This page concentrates on doing things on the command line. That knowledge makes you more flexible and able to work when no desktop environment is available, such as when logging into a Linux machine remotely with PuTTY.
Viewing files with cat
The basic command to show the contents of a file is cat, e.g.:
cat readme.txt
Viewing long text with less
If the file is long, cat won't be convenient, because it means you need to scroll back to see the start. Viewing a long text file conveniently can be done with less, e.g.:
less readme.txt
It can also be used to scroll through the text output of a previous command by piping it through, e.g.:
ls -lR | less
The basic usage of less is intuitive: the arrows keys and page up/down work to scroll through the text. You only need to learn that the q key is for quitting. It has more tricks up its sleeve though:
Key | What it does |
---|---|
Q | quit |
H | show help (about even more keys) |
/foo then ⏎ | search for “foo” in the text |
History: Less is a better version of the older more command. Less allows scrolling back and it starts up faster because it doesn't load the entire input before showing it.
Line breaks
For historical reasons different computer systems represent a line break in text with different characters. It is good to be aware of this as it can cause some incompatibilities to this day. All systems use either the Carriage Return (CR) and/or Line Feed (LF).
History: Both concepts come from early printers that needed to let the print head (the carriage) return to the beginning of the line (CR) and then scroll the paper down (LF) to start printing text on the next line.
These different methods exist:
Operating system | Line break |
---|---|
DOS, Windows | CR and then LF |
Unix, Linux, modern Mac | LF |
Legacy Mac (before OS X) | CR |
A convenient utility to convert between the Windows and Linux format is dos2unix, which can be given a text file to modify as argument: dos2unix readme.txt
Nano
Nano is a nice and simple text editor that even has syntax highlighting. A text file can be opened with:
nano readme.txt
The keys for Nano are shown at the bottom the screen (such as Ctrl+X for exit):
Vim
Vim (VIsual editor iMproved) is what the name suggests: an improved version of the older Vi editor. It takes a bit longer to learn this editor, but it has loads of features that can make you really productive. It is my editor of choice. A text file can be opened with:
vim readme.txt
Vim has two modes: the command mode and insert mode. By default it is in command mode and most keys on the keyboards make the editor do something. The x key means delete for example. To enter so-called Ex commands you need to first type : and then the command. Opening a file and quitting are done with Ex commands. You need to use the i key to switch to insert mode. Then you can add/remove/change text as usual. Returning back to command mode is done with the Esc key or Ctrl+[.
The basic keys in command mode are:
Key | What it does |
---|---|
F1 | show help |
:help | show help |
i | switch to insert mode, then Esc to stop |
:q | quit |
:q! | quit without saving changes |
:w | write/save |
:wq | write/save and quit |
Shift+Z,Shift+Z | write/save and quit |
You can put settings for Vim in ~/.vimrc. I advice the following:
syntax on " Enable syntax highlighting
set background=dark " Assume dark background for syntax highlighting
set nowrap " Don't wrap long lines
set ic " Ignore case when searching
A screenshot of Vim with syntax highlighting enabled:
Emacs
Emacs is the other famous advanced editor. A text file can be opened with:
emacs readme.txt
It doesn't have two modes like Vim. You can directly edit text when opened. Commands are given with the Ctrl key. Basic keys are:
Key | What it does |
---|---|
Ctrl+X, Ctrl+S | save |
Ctrl+X, Ctrl+C | quit |
F10 | open menu bar |
Like Vim the colors won't be right be default in case you have a dark background. To fix that put this in ~/.emacs:
;; dark mode
(when (display-graphic-p)
(invert-face 'default)
)
Screenshot of Emacs:
Default editor
This is not essential, but a few programs (such as less) make use of either environment variable VISUAL or EDITOR to determine which editor to open. You can customize this to your preferred editor by including this in ~/.bashrc:
export VISUAL=nano
export EDITOR=nano