SSH & Terminal Access

Efficient Troubleshooting with vi: Advanced Techniques for System Administrators

By the Domain India teamPublished 8 min read
Knowledge base article
Contents (10 sections)

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.

Key takeaways

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.php or 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

ModeHow you get thereWhat it is for
NormalEsc (vi starts here)Moving, deleting, copying, pasting
Inserti, a, o or OTyping text
Visualv (characters), V (lines), Ctrl+V (a column block)Selecting text to act on
Command line: from Normal modeSaving, 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

bash
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

ActionKeys
Go to line 120:120 then Enter, or 120G
Start or end of filegg and G
Start or end of line0 and $
Next or previous wordw and b
Page down or upCtrl+F and Ctrl+B
Jump to the matching bracket%
Show the current line numberCtrl+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

  • /text searches forward and ?text backward; n repeats, N goes the other way.
  • * searches for the word under the cursor, which is handy for finding every use of a setting name.
  • :%s/old/new/g replaces every match in the file. Add c (:%s/old/new/gc) to confirm each one, which is the safer choice in configuration files.
  • :10,40s/old/new/g limits 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#gc does it without clashing with slashes in paths.
  • :g/pattern/d deletes every line matching a pattern; :v/pattern/d deletes every line that does not match. Use them on a copy first.

6. Edit, copy and undo

ActionKeys
Delete a line, or 5 linesdd, 5dd
Delete a worddw
Copy (yank) a line, or 5 linesyy, 5yy
Paste below or abovep and P
Indent or unindent a line>> and <<
Undo and redou 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.conf opens another file; :e # returns to the previous one.
  • vi a.conf b.conf opens both; :n goes to the next and :prev to the previous.
  • :split other.conf or :vsplit other.conf shows two files at once; Ctrl+W then W moves between windows.
  • :r !command inserts the output of a shell command at the cursor, for example :r !hostname.

8. Save and quit

CommandWhat it does
:wSave and keep editing
:wq or :xSave and quit
:qQuit (refused if there are unsaved changes)
:q!Quit and discard changes
:w newname.confSave 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

  1. Find the line.
    Run the service's config test, such as nginx -t, apachectl configtest or named-checkconf, and note the file and line it reports.
  2. Keep a copy.
    cp file file.bak takes a second and gives you an instant way back.
  3. Fix it.
    Open with vi +LINE file, make the change and save with :w.
  4. Test again.
    Repeat the config test, and reload the service only when it passes. Reload rather than restart where you can.
  5. Read logs with the right tool.
    Use grep -n, less or tail -f for 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.

Keep your session open

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

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.

Need root access to your own server?

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

Ready when you are

Get DirectAdmin hosting from ₹100/mo + GST

See plans

Was this article helpful?

Your answer helps us decide what to improve next.

Still need help? Open a support ticket and our team will reply.

Prefer an app? Add this site to your home screen.Get the app
vi and Vim for Troubleshooting: Advanced Techniques