pctechguide.com

  • Home
  • Guides
  • Tutorials
  • Articles
  • Reviews
  • Glossary
  • Contact

Using vi to Edit Text Files on A Linux Dedicated Server

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

Filed Under: Moving from Shared to Dedicated Web Hosting Tagged With: editing text with vi, how to edit text files with vi, how to use the vi text editor

Latest Articles

Intel Core – 8th generation CPU architecture

It was at the Intel Development Forum in March 2006 that Intel released details of its new Intel Core microarchitecture, the successor to the NetBurst and mobile Pentium M architectures and foundation for the company's forthcoming multi-core server, desktop and mobile … [Read More...]

Stellar Cost-Saving Benefits of Free VPN Services

A recent study from PC Magazine showed that the global VPN market will reach $36 billion by 2022. There are tremendous benefits of VPNs that are driving the market. Numerous people already installed a VPN as it is a really necessary tool in the modern world. It allows the user to feel safe on … [Read More...]

Windows Virtual Security

Windows Virtual Security Welcome to another of our virus removal guides. This article will go over how to remove Windows Virtual Security. Infections of this type aren't generally dangerous, but they can make using your computer unnecessarily difficult. Usually, infections of this type are simply … [Read More...]

Gaming Laptop Security Guide: Protecting Your High-End Hardware Investment in 2025

Since Jacob took over PC Tech Guide, we’ve looked at how tech intersects with personal well-being and digital safety. Gaming laptops are now … [Read More...]

20 Cool Creative Commons Photographs About the Future of AI

AI technology is starting to have a huge impact on our lives. The market value for AI is estimated to have been worth $279.22 billion in 2024 and it … [Read More...]

13 Impressive Stats on the Future of AI

AI technology is starting to become much more important in our everyday lives. Many businesses are using it as well. While he has created a lot of … [Read More...]

Graphic Designers on Reddit Share their Views of AI

There are clearly a lot of positive things about AI. However, it is not a good thing for everyone. One of the things that many people are worried … [Read More...]

Redditors Talk About the Impact of AI on Freelance Writers

AI technology has had a huge impact on our lives. A 2023 survey by Pew Research found that 56% of people use AI at least once a day or once a week. … [Read More...]

11 Most Popular Books on Perl Programming

Perl is not the most popular programming language. It has only one million users, compared to 12 million that use Python. However, it has a lot of … [Read More...]

Guides

  • Computer Communications
  • Mobile Computing
  • PC Components
  • PC Data Storage
  • PC Input-Output
  • PC Multimedia
  • Processors (CPUs)

Recent Posts

Dot-Matrix Printers

Dot matrix was the dominant print technology in the home computing market in the days before the inkjet. Dot matrix printers … [Read More...]

Resolve Omiga-Plus Redirect Issue

Alternating homepage or start page is one to capabilities of Omiga-Plus, which is the reason it is popularly known to be one of the browser hijackers. … [Read More...]

What Are the Best eSports Games to Watch in 2020?

The esports industry is growing at a remarkable rate.  According to Statistica, the global market for esports is expected to reach $1.79 billion … [Read More...]

[footer_backtotop]

Copyright © 2025 About | Privacy | Contact Information | Wrtie For Us | Disclaimer | Copyright License | Authors