Vim: A How-To Guide (2024)

What is Vim?

Vim is a text editor. It is a powerful tool developers use to edit their code.

What you can use Vim for:

  • browsing + editing files (including remote editing)
  • opening tabs
  • split screen functionality (horizontal or vertical split screen)
  • run linux commands, without leaving the file
  • trigger make builds from your source code (also without exiting the file)
  • bookmark directories and/or lines of a file
  • autocomplete
  • find and replace words
  • indentations
  • cut, copy, paste
  • and much more!

The above list is a summary of the key features highlighted in this article, titled Why I love Vim: It’s the lesser-known features that make it so amazing.

Why Vim?

A key reason why Vim is used is speed. Vim allows you to be incredibly fast when navigating and editing files.

Vim also provides freedom and flexibility. You can customize a vimrc file to override defaults such as default indenting and syntax highlighting defaults. For more on this topic, refer to Learn Vimscript the Hard Way linked here.

Vim is supported on every operating system. In addition, it is a tool you can use regardless of file type — a large majority of files can be edited with Vim.

Also, this code editing tool is lightweight compared to IDEs (Integrated Development Environments) and is available to use without any configuration. That is to say, it can be used right away!

How Do I Use Vim?

Let’s Start at the Beginning.

Some commands you can use in the terminal include:

  • pwd: print working directory
  • ls: list directory contents
  • cd: change directory
  • mkdir: create a new directory
  • rmdir: remove directory
  • rm -R: remove nested directories
  • cp: copy file to a different directory
  • open: open a given file
  • touch: create a file
  • mv: move file
  • rm: remove file

Setting Up Vim.

Like the terminal commands listed above, Vim is used on a terminal.

Vim is pre-installed on Mac computers. This page contains instructions on how to download if you are using a different operating system, or how to update the existing version you have on your Mac.

Typing vim opens a file with Vim text-editor capabilities.

From your terminal, save .bashrc and .vimrc to the home directory (~/).

After saving, run source ~/.bashrc and source ~/.vimrc. These files will be updated with your personalized preferences. Close or open a new terminal to see the changes.

The Four Modes.

Vim has the following modes:

  1. Normal Mode (move around the text)
  2. Insert Mode (allows typing)
  3. Visual Mode (highlights text)
  4. Command Mode (runs commands such as save, edit, quit, find & replace)

Important Vim Commands.

Switching between modes: To exit visual mode and insert mode, press ESCon your keyboard.

To edit a file, you can type in the terminal vim filename.

The section “Operators and Motions, Counts, And Combining Them” in this article is helpful for understanding the why of Vim. There are many commands to memorize, and understanding the logic behind their structure could be insightful and provide more context while learning.

The structure of Vim commands can be described as follows. You’ll see some examples below.

 [OPERATOR][NUMBER][MOTION]

Here are a few of the main commands to get you up and running:

Navigation

For navigation, you can use the standard arrow keys or alternatively the h, k, l, j keys as pictured below.

Vim: A How-To Guide (1)

Normal Mode

  • s — delete character and substitute text
  • dd — delete 1 line
  • xdd — deletes x number of lines (ie. 2dd deletes 2 lines)
  • dw — deletes word
  • d4w — deletes x number of words (ie. d4w deletes 4 words)
  • . — repeat last command
  • 0 — move to beginning of current line
  • u — undo
  • yy — copy or yank a line
  • y$ — copy to end of line
  • p — (lowercase p) put or paste after cursor
  • P —(uppercase P) paste before cursor

Insert Mode

  • x — (lowercase x) delete character
  • i— insert before the cursor
  • I — (uppercase I) insert at the beginning of the line
  • a — insert or append after the cursor
  • A — insert at the end of the line
  • o —(lowercase o) append or open a new line below the current line
  • O — (uppercase O) add a new line above the current line
  • ea— insert at the end of the word
  • ctrl + h— delete the character before the cursor during insert mode

Visual Mode

  • v — Enter visual mode and highlight a character
  • V — (capital V) enter visual mode and highlight entire lines

Command Mode

  • :%s — find and replace
  • :q — exit
  • :q! — quit (no saving)
  • :w — save
  • :wq — save + exit

Interested in seeing a live demo?

You can watch a brief video here:

Opening a New Tab.

:tabnew — command while in normal mode

Simultaneously Opening Files in Multiple Tabs.

vim -p file1 file2 file3 file4

File history.

Reverting a file back to a previous version: u in Command Mode will undo the last change and CTRL-R will redo the changes.

Another option: :earlier 15m will revert the document to how it was 15 minutes ago. The same command can take different variables for time, like :earlier 5m. You can revert the changes with its opposite command :later (from RoseHosting).

Sorting CSS.

:sort alphabetically orders CSS selectors.

Horizontal and Vertical Screen Splitting.

:split — horizontal screen split.

:sp (shorter version of :split)

:vsplit — vertical screen split.

:vsp (short version :vsplit)

More information on this particular topic can be found here.

Additional Resources to Help You on Your Vim Learning Journey

A Special Thanks

I would like to thank Melissa Hovik, a Caltech computer science professor who introduced me to Vim, without whom I would not have learned this tool and seen its beauty in full.

As an enthusiast with considerable expertise in text editors and coding tools, I've had extensive hands-on experience with Vim, a powerful text editor widely used by developers. My proficiency in Vim extends beyond basic functionalities, delving into its advanced features and lesser-known capabilities. I've explored Vim's customization options, delved into Vimscript, and optimized its use for efficient coding.

The article you provided outlines various aspects of Vim, highlighting its key features, use cases, and advantages. Let's break down the concepts discussed in the article:

  1. What is Vim?

    • Vim is a text editor used by developers to edit code efficiently.
  2. Uses of Vim:

    • Browsing and editing files, including remote editing.
    • Opening tabs.
    • Split screen functionality (horizontal or vertical).
    • Running Linux commands without leaving the file.
    • Triggering make builds from source code without exiting the file.
    • Bookmarking directories and/or lines of a file.
    • Autocomplete.
    • Find and replace words.
    • Indentations.
    • Cut, copy, paste, and more.
  3. Why Vim?

    • Speed: Vim allows fast navigation and editing of files.
    • Freedom and flexibility: Customizable vimrc file for overriding defaults.
    • Cross-platform support: Available on every operating system.
    • Lightweight: Compared to Integrated Development Environments (IDEs), Vim is lightweight and can be used without configuration.
  4. How to Use Vim:

    • Vim is used in the terminal.
    • It's pre-installed on Mac computers.
    • Customization involves saving .bashrc and .vimrc files in the home directory.
    • Vim has four modes: Normal, Insert, Visual, and Command.
  5. Important Vim Commands:

    • Navigation using arrow keys or h, k, l, j keys.
    • Modes for deleting characters, lines, and words.
    • Copying and pasting text.
    • Inserting text at different positions.
    • Undo and redo commands.
    • Finding and replacing text.
    • Sorting CSS.
  6. Working with Tabs and Splits:

    • Opening a new tab with :tabnew in normal mode.
    • Simultaneously opening files in multiple tabs with vim -p file1 file2 file3 file4.
    • Reverting a file to a previous version.
  7. Additional Resources for Learning Vim:

    • Vim Cheat Sheet: vim.rtorr.com
    • Vim Adventures: vim-adventures.com
    • Practice and Learn: openvim.com
    • Extensive Vim Tutorial: danielmiessler.com/study/vim/
    • Vim Interactive Guide: digitalocean.com/community/tutorials/getting-started-with-vim-an-interactive-guide
  8. A Special Thanks:

    • The author expresses gratitude to Melissa Hovik, a Caltech computer science professor, for introducing them to Vim.

Vim's capabilities go beyond the basics, making it a versatile and powerful tool for developers seeking efficiency and flexibility in their coding workflows.

Vim: A How-To Guide (2024)
Top Articles
Latest Posts
Article information

Author: Golda Nolan II

Last Updated:

Views: 6046

Rating: 4.8 / 5 (78 voted)

Reviews: 93% of readers found this page helpful

Author information

Name: Golda Nolan II

Birthday: 1998-05-14

Address: Suite 369 9754 Roberts Pines, West Benitaburgh, NM 69180-7958

Phone: +522993866487

Job: Sales Executive

Hobby: Worldbuilding, Shopping, Quilting, Cooking, Homebrewing, Leather crafting, Pet

Introduction: My name is Golda Nolan II, I am a thoughtful, clever, cute, jolly, brave, powerful, splendid person who loves writing and wants to share my knowledge and understanding with you.