A Beginner’s Guide to VI and VIM: Mastering Text Editing in Linux

[This article was first published on Steve's Data Tips and Tricks, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Are you new to Linux and looking to learn the basics of text editing? Look no further than VI (or VIM), the ubiquitous text editor that comes pre-installed on nearly every Linux distribution. While it may seem intimidating at first with its unique modal editing style, VI is a powerful tool that is well worth learning. In this beginner-friendly guide, we’ll walk you through the fundamentals of using VI to edit text files on Linux systems.

What is VI?

VI, which stands for “Visual Editor”, is a screen-oriented text editor originally created for the Unix operating system. Today, it is available on Linux, macOS, and other Unix-like systems. VI is known for its modal editing, where the meaning of typed keys depends on which mode the editor is in.

The original VI was developed by Bill Joy in 1976 as the visual mode for a line editor called EX. It has since been replaced by an improved version called VIM (VI Improved), which adds many useful features while maintaining backwards compatibility with the original VI.

Why Learn VI?

You may be wondering, with modern graphical text editors and IDEs available, why bother learning an old, terminal-based editor like VI? Here are a few compelling reasons:

  1. VI is installed by default on virtually all Linux and Unix-based systems. Knowing the basics will allow you to edit text files on any system you log into.

  2. VI is lightweight and fast, making it ideal for quick edits without the overhead of a graphical editor.

  3. Many common Linux tools like less and man use VI-style key bindings, so familiarity with VI will make you more proficient on the command line overall.

  4. Mastering VI will greatly improve your speed and efficiency when editing code and configuration files.

  5. VI has an extensive ecosystem of plugins and customizations that cater to specific editing needs, from syntax highlighting to version control integration.

Getting Started

To launch VI, simply open a terminal and type vi followed by the name of the file you want to edit (or create):

vi myfile.txt

If the specified file does not exist, VI will create a new blank file. If no filename is given, VI will open with an empty untitled document.

Modes in VI

One of the first things to understand about VI is its concept of modes. When you open a file in VI, you start in command mode, where typed keys are interpreted as commands that control the editor. To enter text, you must switch to insert mode. Let’s look at the three main modes:

Command Mode

When you first open VI, you are in command mode. In this mode, every key is a command that performs a specific action, such as navigating through the document, deleting text, or changing options. For example:

  • Use the arrow keys or h, j, k, l to move the cursor around
  • x deletes the character under the cursor
  • dd deletes the current line
  • :w saves the file
  • :q quits VI

Insert Mode

To enter text into the document, you need to switch to insert mode. Press i to enter insert mode at the cursor position. Now any keys you type will be inserted into the document at the cursor position. To return to command mode, press Esc.

There are a few other ways to enter insert mode:

  • a appends text after the cursor
  • o inserts a new line below the current one and enters insert mode
  • O inserts a new line above the current one and enters insert mode

Visual Mode

Visual mode allows you to visually select text in the document for manipulation. Press v to enter visual mode, then use the arrow keys or VI movement commands to select text. Once selected, you can perform operations on the highlighted text, such as:

  • d to delete the selected text
  • y to “yank” (copy) the selected text
  • > to indent the selected lines

Press Esc to exit visual mode and return to command mode.

Basic Editing

Now that you understand VI’s modal editing system, let’s look at some basic editing tasks.

Editing Text

From command mode:

  • i enters insert mode at the cursor
  • a enters insert mode after the cursor
  • x deletes the character under the cursor
  • dd deletes the current line
  • yy yanks (copies) the current line
  • p pastes the last deleted or yanked text after the cursor
  • u undoes the last change

Saving and Quitting

To save your changes, type :w in command mode and press Enter. To quit VI, type :q and press Enter. If you have unsaved changes, VI will warn you and refuse to quit. To discard your changes and quit anyway, use :q!. To save and quit in one command, type :wq.

Your Turn!

Now that you’ve learned the basics of VI, it’s time to practice! Open a new file in VI and try out the following:

  1. Enter insert mode and type a few lines of text
  2. Use the movement keys to navigate around and make some edits
  3. Yank and paste a line of text
  4. Save the file and quit VI

Here’s a sample text you can use:

The quick brown fox jumps over the lazy dog.
Pack my box with five dozen liquor jugs. 
How vexingly quick daft zebras jump!
Click Here For Solution!
  1. Open a new file in VI by typing vi test_file.txt in your terminal.

  2. Press i to enter insert mode and type the sample text:

The quick brown fox jumps over the lazy dog.
Pack my box with five dozen liquor jugs. 
How vexingly quick daft zebras jump!
  1. Press Esc to return to command mode.

  2. Use h, j, k, l or arrow keys to move the cursor around the text. Make some edits, such as changing “jumps” to “leaps” in the first line.

  3. Move the cursor to the second line and press yy to yank (copy) the line.

  4. Move the cursor to the end of the file and press p to paste the yanked line.

  5. To save the changes, type :w in command mode and press Enter.

  6. To quit VI, type :q and press Enter.

Congratulations, you’ve just completed your first VI editing session! With practice, these commands will become second nature, and you’ll be able to efficiently navigate and edit text files in any Unix-based environment.

From my terminal

Quick Takeaways

  • VI is a powerful terminal-based text editor with a modal editing system
  • Command mode is for entering commands, insert mode is for text input
  • Use h, j, k, l or arrow keys to navigate in command mode
  • Switch between modes with i, Esc, v
  • :w saves, :q quits, :wq saves and quits

Conclusion

Congratulations, you now know the basics of using the VI editor on Linux! While it takes some practice to master the key commands and modal editing style, the effort you put in will pay off in your future Linux endeavors. VI is an indispensable tool for system administrators, developers, and power users.

To further hone your skills, spend some time each day editing files in VI. You’ll be surprised how quickly the key bindings will become second nature. As you gain proficiency, you can explore VI’s more advanced features like macros, split windows, and customizing your configuration.

FAQs

Q: What is the difference between VI and VIM?

A: VIM is an enhanced version of the original VI editor, with additional features and customization options. However, VIM maintains backwards compatibility with VI, so the core functionality is the same.

Q: Can I use the mouse in VI?

A: VI was designed for a mouse-free workflow, so it relies on keyboard commands for all navigation and editing tasks. However, some modern versions of VIM do include mouse support as an optional feature.

Q: How can I customize VI to my liking?

A: VI looks for a configuration file called .vimrc in your home directory. Here you can set your preferred options, define custom key mappings, and more. See the VIM documentation for a full list of available settings.

Q: Is it worth learning VI if I already use a graphical editor?

A: Absolutely! VI is a fundamental tool that every Linux user should know. Not only is it ubiquitous across all Unix-like systems, but mastering VI will also make you more efficient in terminal-based workflows. That said, there’s nothing wrong with using a graphical editor when it makes sense.

Q: Can I use VI to edit code with syntax highlighting?

A: Yes, VIM has excellent support for syntax highlighting for hundreds of programming languages and file formats. It also has features like code folding, auto-indentation, and plugins for specific languages and frameworks.

I hope this gentle introduction to VI has piqued your interest and encouraged you to explore this classic Linux tool. Stick with it, and you’ll be editing like a pro in no time! Let me know if you have any other questions.

References

  1. “Classic Sysadmin: Vim 101 – A Beginner’s Guide to Vim” – The Linux Foundation Blog, https://www.linuxfoundation.org/blog/blog/classic-sysadmin-vim-101-a-beginners-guide-to-vim

  2. “Vi vs Vim: Choosing the First Right Text Editor” – GeeksforGeeks, https://www.geeksforgeeks.org/vi-vs-vim-choosing-the-first-right-text-editor/

  3. “8 Reasons to Learn Vi/Vim Editor in Linux” – Tecmint, https://www.tecmint.com/reasons-to-learn-vi-vim-editor-in-linux/

  4. “Learning The vi Editor” – Wikibooks, https://en.wikibooks.org/wiki/Vi

  5. “The Vim Book” – Steve Oualline, //ftp.vim.org/pub/vim/doc/book/vimbook-OPL.pdf

  6. “Bill Joy” – Wikipedia, https://en.wikipedia.org/wiki/Bill_Joy

  7. “Bram Moolenaar” – Wikipedia, https://en.wikipedia.org/wiki/Bram_Moolenaar


Happy Coding! 🚀

VIM

You can connect with me at any one of the below:

Telegram Channel here: https://t.me/steveondata

LinkedIn Network here: https://www.linkedin.com/in/spsanderson/

Mastadon Social here: https://mstdn.social/@stevensanderson

RStats Network here: https://rstats.me/@spsanderson

GitHub Network here: https://github.com/spsanderson

Bluesky Network here: https://bsky.app/profile/spsanderson.com


To leave a comment for the author, please follow the link and comment on their blog: Steve's Data Tips and Tricks.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)