Quick Links

Vi is a powerful text editor included with most Linux systems, even embedded ones. Sometimes you’ll have to edit a text file on a system that doesn’t include a friendlier text editor, so knowing Vi is essential.

Unlike Nano, an easy-to-use terminal text editor, Vi doesn’t hold your hand and provide a list of keyboard shortcuts on the screen. It’s a modal text editor, and it has both an insert and command mode.

Getting Started With Vi

Vi is a terminal application, so you’ll have to start it from a terminal window. Use the vi /path/to/file command to open an existing file with Vi. The vi /path/to/file command also works if the file doesn’t exist yet; Vi will create a new file and write it to the specified location when you save.

Remove Ads
Ubuntu terminal with the vi command being used to open a new file.

Remember to use sudo if you want to edit a system file. So, for example, you’d type sudo vi /etc/fstab if you wanted to edit your fstab file.

Command Mode

This is what you’ll see when you open a file in vi. It looks like you can just start typing, but you can’t. Vi is a modal text editor, and it opens in command mode. Trying to type at this screen will result in unexpected behavior.

A file open in the vi text editor in an Ubuntu terminal window.

While in command mode, you can move the cursor around with the arrow keys. Press the x key to delete the character under the cursor. There are a variety of other delete commands—f.or example, typing dd (press the d key twice) deletes an entire line of text.

Remove Ads

You can select, copy, cut, and paste text in command mode. Position the cursor at the left or right side of the text you want to copy and press the v key. Move your cursor to select text, and then press y to copy the selected text or x to cut it. Position your cursor at the desired location and press the p key to paste the text you copied or cut.

Text selected at the end of a file open in the vi text editor.

Insert Mode

Aside from command mode, the other mode you need to know about is insert mode, which allows you to insert text in Vi. Entering insert mode is easy once you know it exists—just press the i key once after you’ve positioned the cursor in command mode. Start typing and Vi will insert the characters you type into the file rather than trying to interpret them as commands.

Remove Ads
Vi text editor with text stating that the text was added by pressing the "i" key first.

Once you’re done in insert mode, press the escape key to return to command mode.

Saving and Quitting

You can save and quit vi from command mode. First, ensure you’re in command mode by pressing the Escape key. (Pressing the Escape key again does nothing if you’re already in command mode.)

Type :wq and press enter to write the file to disk and quit vi. You can also split this command up—for example, type :w and press enter to write the file to disk without quitting or type :q to quit vi without saving the file.

Vi text editor with the "wq" commmand typed at the bottom of the window.

Vi won’t let you quit if you’ve modified the file since you last saved, but you can type :q! and press enter to ignore this warning.

Remove Ads
Vi text editor with the error stating "No write since last change" at the bottom of the window.

Check out Nano if you’re looking for an easier-to-use terminal text editor. Most Linux distributions come with Nano installed, but embedded systems and other stripped-down environments often only include Vi.

Linux Commands

Files

tar·pv·cat·tac·chmod·grep ·diff·sed·ar·man·pushd·popd·fsck·testdisk·seq·fd·pandoc·cd·$PATH·awk·join·jq·fold·uniq·journalctl·tail·stat·ls·fstab·echo·less·chgrp·chown·rev·look·strings·type·rename·zip·unzip·mount·umount·install·fdisk·mkfs·rm·rmdir·rsync·df·gpg·vi·nano·mkdir·du·ln·patch·convert·rclone·shred·srm·scp·gzip·chattr·cut·find·umask·wc· tr

Processes

alias·screen·top·nice·renice·progress·strace·systemd·tmux·chsh·history·at·batch·free·which·dmesg·chfn·usermod·ps·chroot·xargs·tty·pinky·lsof·vmstat·timeout·wall·yes·kill·sleep·sudo·su·time·groupadd·usermod·groups·lshw·shutdown·reboot·halt·poweroff·passwd·lscpu·crontab·date·bg·fg·pidof·nohup·pmap

Networking

netstat·ping·traceroute·ip·ss·whois·fail2ban·bmon·dig·finger·nmap·ftp·curl·wget·who·whoami·w·iptables·ssh-keygen·ufw·arping·firewalld

RELATED:Best Linux Laptops for Developers and Enthusiasts

Remove Ads