Viewing files with cat, more, less and vim
There are 3 main tools available on almost every Linux systems to view files, and a couple that allow you to see partial files.
They all have the general format of
command followed by the file you want to access.
For more details on any command line argument that may be available, if not referenced here, check the man page for that tool by typing
man followed by the command name, for example:
cat (from concatenate) is a tool that is used to take the file name provided as an argument, and then print it to standard output (the screen), but can be redirected to a file. This can be useful in showing the contents of small files on the screen.
It can also be used to concatenate (which is it's primary design use) multiple small files into one larger one, for example, compiling multiple logfiles into one file.
The output of
cat can be piped into
more, to allow the data to be paused page by page, with each page advancing (and only advancing) by pressing any key. This allows you to have a chance to read data on a file that would otherwise scroll by on the screen faster than you can read.
The opposite of
more. More allows you to display the contents of a file, but also allows you scroll back and forth, and run searches, using the same syntax as you would with
vim - or
sed - but you cannot edit anything - it's read-only only!
Visual editor - improved! -
vim for short (in some basic distros, you may only have
vi - it works the same way globally) This will allow you to view and edit files, but it has a particular way of working, where there is no menu per-se but you have to switch between command mode, where you can instruct vi to save, quit, search, replace etc, and insert mode where you can enter text and navigate using the arrow keys on the keyboard.
This is not an extensive doc on vim, it's a lot more complex than what is presented here!
Get into insert mode
When you open a file, you are in command mode. You can possibly navigate around the file with the arrow and page up/down keys and you can run searches, save, exit or quit without saving, but not enter text.
To get into insert mode and be able to type in the document, you will press the
i key followed by
enter. You will now switch to insert mode and you can edit your document.
Get to command mode
You will need to get out of insert mode to save and quit vim - you need to remember the command combination as there is no visual menu!
To switch back from insert mode to command mode, press the
From here, you can enter commands.
Basic commands will start with a full-colon
: followed by a command key code - then press
enter to validate:
:w: Write the current document document
:x: Save and exit
:q!: Quit directly, dont save, and don't ask for confirmation!
Searching and replacing
Both of these operations are carried out from command mode.
This is not started with a full-colon but a slash, followed by your search term:
/somethingwill search the open file for the string
- Remember that you may need to escape certain control characters. If you don't find what you expect, add a backslash
\before any symbols that
vimmay process unexpectedly.
As for searching, the same comment on escaping symbols as noted above applies.
= One occurrence in a file =
:s/search/replace/g: Replace the first occurrence of the word
searchand replace it with the word
= All occurrences in a file =
:%s/search/replace/g: Replace every occurrence of the word
searchand replace them with the word
replacein the document.