A fact of life with Linux administration is that you will have to edit text files, simply because the operating system is configured using text files. Although modern versions of Linux (try Knoppix) do provide friendly graphical desktops and GUI (graphical user interface) aids for most configuration tasks, a dedicated web server will not have these facilities available, so you simply have to get into the text.
The two most common programs used for editing text files from a command line interface are vi and emacs. We’ll look at vi in this article, as it is available on just about all default Linux installations. However, the fact is that vi is not an intuitive program to use, and will be completely alien to those used to editing files from Windows applications like Notepad. You will have to learn a completely new way of working, because there is no point and click operation at all, everything is achieved by entering keyboard commands.
Here we’ll cover the basic steps involved in editing text files with vi, so that you will have a reference to work from when it comes to editing the important administration files on your server. Don’t think that this is going to be a complex, exhaustive coverage of the operation of vi. Keeping it simple, we’re just covering the barebones essentials that will enable you to do what needs to be done.
Creating a file with vi
The steps to create a file with vi are actually very simple. Enter the following at the command line.
$ vi newfile.txt
(Please refer back to the conventions used here.) You’ll see something along the lines of this:
~
~
~
~
~
/home/[username]/newfile.txt [New File]
Now, anyone who’s never come across vi before will at this point be confused. If they start to type, then any number of weird things start to happen, but no text appears. There’s no icons or drop down menus, simply nothing to click on, and all those tildes (~) don’t make any sense. But relax, here’s what’s going on.
The vi editor has two modes, command or input. Whenever vi starts, it is in command mode, and is waiting to be told what you want to do. When you have a sparse or empty file, vi uses the tilde character as filler. To edit text, you have two options.
- a – for add, allows you to input text that starts to the right of the cursor
- i – for insert, allows you to input text that starts to the left of the cursor, which will be more familiar to most Windows users
Once you have hit either of these commands you are in the input mode and can edit the file. At any time you can get back to command mode by hitting the escape key. Go ahead and enter some text into the file. You’ll find you can type pretty much as you would normally type. You can:
- hit return for a new line
- use backspace and delete to delete characters
- use the arrow keys to move around the text
- use Home, End, Page up and Page Down
This is all pretty common stuff, but you might be a little frustrated if you’re used to using the Crtl to help you navigate a text file, as you’ll find it doesn’t work. However, when you’ve done editing, hit escape, and you’re back in command mode.
As an aside, a useful feature of vi is that when in command mode you can enter regular Linux shell commands, as long as you precede the command with :!. For instance, type:
:!pwd
This instruction will reveal your current directory location. Hit enter to return to vi. In this way you can navigate the operating system, list directory contents, move files, create directories, or whatever you need to do in the shell environment, without exiting vi. (See more on Linux directory structure and directory navigation.)
As for working with the text file, the table below outlines a useful subset of the vi instructions when operating in command mode. Try them out on your newly created file, and experiment. A few minutes now may help you to feel more comfortable when it comes to editing more important files later on.
Notice the differing cases for the letters, because a CAPITALISED letter does not mean the same thing as a lower case letter! And one more useful tip, if at any time you need to know the name of the file you’re editing and the line number you’re on, hit Ctrl-g to have a summary placed across the bottom of the screen.
VI COMMAND MODE INSTRUCTIONS | |
---|---|
File Saving | |
ZZ or :wq | Save the file and exit vi |
:w | Save the file |
:q | This quits the current file, but only if there are no unsaved changes |
:q! | The get-me-out-of-here! command, this quits vi without saving, so all changes are lost |
Navigating Text | |
Arrow keys | Move the cursor around the document |
h(left), l(right), j(down), k(up) | Alternatives to the arrow keys |
w | Moves the cursor to the beginning of the next word |
b | Moves the cursor to the beginning of the previous word |
0 (zero) | Moves the cursor to the beginning of the current line |
$ | Moves the cursor to the end of the current line |
H | Moves the cursor to the beginning of the first line currently on the screen |
M | Moves the cursor to the beginning of the middle line currently on the screen |
L | Moves the cursor to the beginning of the last line currently on the screen |
Ctrl+f | Go forward one page |
Ctrl+b | Go back one page |
Ctrl+d | Go forward half a page |
Ctrl+u | Go back half a page |
G | Go to the last line of the file |
#G | Move the cursor to the line number specified by # |
Deleting text | |
x | Deletes the character under the cursor |
X | Deletes the character before the cursor |
dw | Deletes from the current character to the end of the current word |
d$ | Deletes from the current character to the end of the current line |
d0 | Deletes from the current character to the beginning of the current line |
Search and Replace | |
/text | Search the file forward from the current cursor position for the text specified |
?text | Search the file backward from the current cursor position for the text specified |
/some.*text | Using regular expression-like wildcards, this will search for a line that contains the character string some followed by, after any intervening characters, the character string text |
?[tT]ext | Searches backwards for text or Text – remember that searches are case sensitive |
:g/text | Prints every line in the file that contains text |
:s/text/replacement | Replaces text with replacement on the current line |
:g/text/s//replacement/g | Replaces text with replacement in the entire file |
:g/text/s//replacement/gp | Replaces text with replacement in the entire file and displays the changes made onscreen |
- Advantage to Shared Web Hosting
- Shared Hosting Issues – Shared Bandwidth and Server Resources
- The Ins and Outs of Dedicated Web Hosting
- When To Move To A Dedicated Server
- Choosing a Dedicated Server for your Website
- Managing and Operating a Dedicated Server Over the Internet Using Online Control Panels
- Accessing the Linux Operating System on Dedicated Servers with PuTTY or SSH
- How to Create a Secure Password
- Using Sudo for Super User Access to Root Privileges in Linux
- The Linux Directory Structure
- Linux Commands for Navigating and Viewing Directories
- Creating, Moving, Renaming and Copying Files and Directories in Linux
- Find, View and Delete Files and Directories Using Linux Commands
- Using vi to Edit Text Files on A Linux Dedicated Server