vi is on every Linux and Unix server, including minimal and rescue systems where nothing else is installed. On most modern systems the vi command actually starts Vim, which adds undo history, visual mode and split windows. This guide covers the vi and Vim techniques that matter when you are fixing a configuration file or reading an error log under pressure.
vi starts in Normal mode: press i to type, Esc to stop, :w to save and :q to quit (:q! quits without saving). Jump to a line with :120 or open the file there with vi +120 file, search with /text, and replace across the file with :%s/old/new/gc. Use vi -R file to look without editing. Server configuration editing applies to your own VPS; on Domain India shared hosting you edit only your own files, over jailed SSH if it is enabled on your account.
1. Where this applies
- Your own VPS or server: you have root access, so you can edit web server, firewall, DNS and service configuration. Domain India VPS plans are self-managed.
- Domain India shared hosting: you cannot change server configuration. Jailed SSH access is available on every shared hosting plan (cPanel, DirectAdmin, Webuzo). It is off by default; ask support to enable it for your account. You can then edit your own files, such as
.htaccess,wp-config.phpor a script.
Measured in September 2026, the vi command in the jailed shell on our cPanel and DirectAdmin servers is Vim 8.0 in a small build: split windows work, but syntax highlighting, block insert and diff mode do not. The cPanel jail also has the full vim command, which has them. Check what you have with vi --version: a feature listed with + is available, one with - is not.
2. Modes: the one idea you need
| Mode | How you get there | What it is for |
|---|---|---|
| Normal | Esc (vi starts here) | Moving, deleting, copying, pasting |
| Insert | i, a, o or O | Typing text |
| Visual | v (characters), V (lines), Ctrl+V (a column block) | Selecting text to act on |
| Command line | : from Normal mode | Saving, quitting, search and replace, going to a line |
If you are ever unsure what mode you are in, press Esc twice. You are then in Normal mode and nothing you type goes into the file until you press i again.
3. Open files safely
vi +120 /etc/nginx/nginx.conf # open at line 120
vi +/Listen /etc/httpd/conf/httpd.conf # open at the first match of "Listen"
vi -R /etc/ssh/sshd_config # read-only
vim -d old.conf new.conf # compare two files side by side (full Vim only)For a protected system file on your own server, sudoedit /etc/hosts is safer than running the editor as root: it opens a temporary copy and writes it back only when you save.
If vi warns that a swap file already exists, another session is editing the file or a previous session crashed. Choose (O)pen Read-Only to look, or (R)ecover if you know the earlier session died; never have two editors writing the same file.
4. Move to the problem quickly
| Action | Keys |
|---|---|
| Go to line 120 | :120 then Enter, or 120G |
| Start or end of file | gg and G |
| Start or end of line | 0 and $ |
| Next or previous word | w and b |
| Page down or up | Ctrl+F and Ctrl+B |
| Jump to the matching bracket | % |
| Show the current line number | Ctrl+G, or :set number for all lines |
When a config test reports "error in line 120", :120 puts you on it straight away.
5. Search and replace
/textsearches forward and?textbackward;nrepeats,Ngoes the other way.*searches for the word under the cursor, which is handy for finding every use of a setting name.:%s/old/new/greplaces every match in the file. Addc(:%s/old/new/gc) to confirm each one, which is the safer choice in configuration files.:10,40s/old/new/glimits the change to lines 10 to 40.- Dots and slashes are special: to change an IP address, escape the dots, or pick another separator.
:%s#192\.168\.1\.10#10\.0\.0\.5#gcdoes it without clashing with slashes in paths. :g/pattern/ddeletes every line matching a pattern;:v/pattern/ddeletes every line that does not match. Use them on a copy first.
6. Edit, copy and undo
| Action | Keys |
|---|---|
| Delete a line, or 5 lines | dd, 5dd |
| Delete a word | dw |
| Copy (yank) a line, or 5 lines | yy, 5yy |
| Paste below or above | p and P |
| Indent or unindent a line | >> and << |
| Undo and redo | u and Ctrl+R |
| Repeat the last change | . |
Comment out a block quickly. :10,20s/^/# / comments out lines 10 to 20 in one command, and :10,20s/^# // removes the comments again. This works in every vi. In full Vim you can also press Ctrl+V, move down to select the lines, press I, type # and a space, then press Esc; the small vi build does not support this block insert.
7. Work with more than one file
:e other.confopens another file;:e #returns to the previous one.vi a.conf b.confopens both;:ngoes to the next and:prevto the previous.:split other.confor:vsplit other.confshows two files at once; Ctrl+W then W moves between windows.:r !commandinserts the output of a shell command at the cursor, for example:r !hostname.
8. Save and quit
| Command | What it does |
|---|---|
| :w | Save and keep editing |
| :wq or :x | Save and quit |
| :q | Quit (refused if there are unsaved changes) |
| :q! | Quit and discard changes |
| :w newname.conf | Save a copy under another name |
| :w !sudo tee % | Save a root-owned file you opened without sudo (your own server only) |
9. A troubleshooting workflow on your own server
- Find the line.Run the service's config test, such as
nginx -t,apachectl configtestornamed-checkconf, and note the file and line it reports. - Keep a copy.
cp file file.baktakes a second and gives you an instant way back. - Fix it.Open with
vi +LINE file, make the change and save with:w. - Test again.Repeat the config test, and reload the service only when it passes. Reload rather than restart where you can.
- Read logs with the right tool.Use
grep -n,lessortail -ffor logs. Never edit a log that a service is writing to.
On systemd servers, change a service with systemctl edit myservice, which creates an override file instead of editing the packaged unit that an update would overwrite. If you run your own DNS, remember to raise the zone's serial number after editing a zone file.
A mistake in SSH, firewall or network configuration can lock you out of your own server. Keep your current session open and test the change from a second session before you close it.
10. vi on Domain India
- VPS: self-managed with root access, so everything in this guide applies. See how to access your Domain India VPS using SSH.
- Shared hosting: jailed SSH on request, with key login only. See enabling and accessing jailed SSH. If you would rather not use a terminal, the control panel's File Manager has a built-in editor.
Prefer a simpler editor? Read advanced troubleshooting with nano.
How do I exit vi?
Press Esc to make sure you are in Normal mode, then type :q and Enter. If you have unsaved changes, type :wq to save and quit or :q! to quit without saving.
How do I go to a specific line in vi?
Type a colon, the line number and Enter, for example :120. You can also open the file at that line with vi +120 filename, or type 120G in Normal mode.
How do I search and replace in vi?
Use :%s/old/new/g to replace every match in the file, or :%s/old/new/gc to confirm each one. Escape special characters such as dots with a backslash.
How do I undo a change in vi?
Press u in Normal mode to undo and Ctrl+R to redo. In Vim you can undo many steps; the original vi could undo only the last change.
What is the difference between vi and Vim?
Vim is an improved version of vi with multi-level undo, visual mode, split windows and syntax highlighting. On most Linux systems the vi command starts Vim, sometimes in a small build with fewer features.
Can I use vi on Domain India shared hosting?
Yes, if jailed SSH is enabled on your account; it is available on every shared hosting plan and off by default, so ask support to enable it. The vi command in the jail is Vim 8.0, and you can edit only your own files, not server configuration.
Ready to work on the command line? Request jailed SSH for your shared hosting with a new support ticket, or get root access on a self-managed VPS.
A self-managed VPS gives you full root access to edit any configuration file with vi or the editor of your choice.
See VPS plans