CS107 The Vim Editor (2024)

Written by Chris Gregg and Dominique Yahyavi, with modifications by Peter Johnston, Nick Troccoli, and Lisa Yan

Click here for a walkthrough video.

vim is a text editor that is a clone of the vi editor, which was created around 1976. Despite its age, vim is one of the most popular editors in the world, and it is available on virtually every computing platform (or vi is available). vim has a bit of a learning curve, meaning that you may get somewhat frustrated with it until you have used it a few times.

vim has two "modes": COMMAND mode and INSERT mode. In COMMAND mode, you execute commands (like undo, redo, find and replace, quit, etc.). In INSERT mode, you type text. There is a third mode, VISUAL mode, that is used to highlight and edit text in bulk.

  • To go into INSERT mode from COMMAND mode, you type i. To go back to COMMAND mode, you type the esc key. vim starts out in COMMAND mode. Over time, you will likely spend more time in COMMAND mode than INSERT mode.
  • Note that all commands mentioned below (except for arrow keys) only work in COMMAND mode.

To open a file in vim (or create a new one if a file with this name does not exist):

$ vim filename

If vim is already open and you would like to edit a different file, use the :e filename command (e.g. :e myOtherFile.c). Again, if this file exists in your current directory it will open it, or it will create it if it does not already exist in your current directory.

:w myprogram.c save the current changes to a file. If you don't specify a name, changes will be saved to the current file. If you would like to save the file under a different name, specify a filename.

:q quits Vim. If you have unsaved changes, you will be asked whether or not you'd like to save your changes before quitting.

:q! quit without saving unsaved changes.

:wq save and quit (or you can also use :x to do the same thing in one fewer keystroke)

Arrow keys work in both modes to navigate as you would expect. The preferred mode in vim, however, is to use the j``k``h``l keys instead in COMMAND mode (these do not work in INSERT mode), which allows users to keep their fingers on the home row to move around the screen. Here's how they work:

j: downk: uph: leftl: right

There are also several additional keyboard shortcuts to navigate within a file.

w move to the next word. B move to the previous word ("Back")

b move to the beginning of the word ("beginning"). e move to the end of the word ("end")

0 move to the beginning of the line. $ move to the end of the line

:123 jump to line number 123.123G jump to line number 123 (equivalent to :123)

Ctl-f page down ("forward"). Ctl-b page up ("back")

Ctl-u half a page up. Ctl-d half a page down.

gg jump to the first line in file. G jump to last line in file

Search is another great way to move your cursor.

/foo search the file for "foo". This will highlight all matches in the editor for you to see.

n move the cursor to the next search match

N move the cursor to the previous search match

:noh to turn off highlighting until the next search. Alternatively, search for a bogus string, like /thiswordwontexist

dd or :d deletes the current line

yy or :y or Y "Yank" (cut) the current line

p Paste the text you yanked or deleted. This will put characters after the cursor and put lines below the current line depending on what you yanked. To paste above the current line, use P.

x delete the character underneath the cursor

Nx delete the N characters under and following the cursor (e.g. 3x deletes the 3 characters under and following the cursor).

When you are ready to enter text, you should switch to INSERT mode. There are a few ways to do this:

i enter INSERT mode before the cursor

I enter INSERT mode at the start of the current line

a enter INSERT mode appending after the cursor

A enter INSERT mode appending to the end of the current line

Then, just type as normal to enter text. You can use the arrow keys to navigate, or switch back to COMMAND mode and use the shortcuts mentioned earlier.

u undo the last action

U undo all recent changes made to the current line

ctrl-r redo

If you want to highlight multiple lines or particular words in a line, you can do so in VISUAL mode. There are three different visual modes:

v enters character-wise visual mode to highlight one character at a time.

V enters line-wise visual mode to highlight entire lines at a time.

ctrl-v enters block-wise visual mode to highlight over rows and columns, etc.

Once you enter these modes by typing these in COMMAND mode, you can use the keyboard navigation (e.g., hjkl or w or b) to highlight text). Then you can batch delete (d), yank (y), or paste (p) whatever is highlighted. To exit VISUAL mode, use esc.

VISUAL mode is also useful for adjusting indent to a batch of text. For example, if you wanted to reduce the indent of a set of lines, you can enter block-wise visual mode (ctrl-v) to select a vertical column of indents (using keyboard navigation), and then batch delete with d. To increase the indent of a set of lines, enter block-wise visual mode and select a vertical column, and then batch insert at the beginning of this block with I, type in your indentation, and then use esc. You should see all highlighted lines get the same inserted space.

The best way to get familiar with vim is to just start using it to edit files - type something, anything! Over time, you'll become more comfortable with the standard commands, and pick up more advanced ones. If you are looking for more references for how to use vim, check out the builtin vimtutor tutorial program on myth to help you learn vim. Run it by doing the following:

$ vimtutor

There is also a fun online vim tutorial, called Vim Adventures that can give you practice with the basic commands as well.

When I do vim filename, I get a big scary message that says something about "Found a swap file." What does this mean, and what do I do?

This most often happens when Vim quits unexpectedly, e.g. if you lose your connection to myth while editing a file. (It can also happen if you try to edit the same file in two terminal windows; you shouldn't do this.)

Here's the recommended process for resolving it: First, hit o for "open read-only". This shows you the version of the file that was last saved. Look through it. Does it have your most recent changes?

  • Yes, this is the latest version of the file: Great! To clean up the swap file, :q out, then run vim on the file again. This time, hit d to delete the swap file. (Warning: you can't undo this.)

  • No, some of my changes are missing: OK, don't panic. Quit out of vim, and start it again. This time, hit r for "recover." This will show you the version of the file from the interrupted session. If that's (closer to) the version you want to keep, save it, then follow the steps above to clean up the swap file.

  • How do I avoid the above issue coming up next time?Make sure you exit Vim cleanly. Don't just hit the "X" button on your terminal. Also, don't close your laptop lid while Vim is open (more generally, putting your laptop to sleep while connected to myth could lead to other problems, because it generally turns off your internet connection).

CS107 The Vim Editor (2024)
Top Articles
How to Deposit Cash at an ATM? - 7 Easy Steps
Major Donors: Who Are They And Where Do You Find Them?
Knoxville Tennessee White Pages
Is Sam's Club Plus worth it? What to know about the premium warehouse membership before you sign up
Cold Air Intake - High-flow, Roto-mold Tube - TOYOTA TACOMA V6-4.0
Wizard Build Season 28
Readyset Ochsner.org
Apex Rank Leaderboard
Elden Ring Dex/Int Build
Atrium Shift Select
Skip The Games Norfolk Virginia
Oppenheimer & Co. Inc. Buys Shares of 798,472 AST SpaceMobile, Inc. (NASDAQ:ASTS)
Elizabethtown Mesothelioma Legal Question
Missing 2023 Showtimes Near Landmark Cinemas Peoria
Sony E 18-200mm F3.5-6.3 OSS LE Review
Gino Jennings Live Stream Today
Munich residents spend the most online for food
Tamilrockers Movies 2023 Download
Katherine Croan Ewald
Diamond Piers Menards
The Ultimate Style Guide To Casual Dress Code For Women
Site : Storagealamogordo.com Easy Call
Is Windbound Multiplayer
Filthy Rich Boys (Rich Boys Of Burberry Prep #1) - C.M. Stunich [PDF] | Online Book Share
Integer Division Matlab
Sandals Travel Agent Login
Horn Rank
Ltg Speech Copy Paste
Random Bibleizer
Craigslist Fort Smith Ar Personals
The Clapping Song Lyrics by Belle Stars
Poe T4 Aisling
R/Sandiego
Kempsville Recreation Center Pool Schedule
Rogold Extension
Beaver Saddle Ark
Log in or sign up to view
A Man Called Otto Showtimes Near Amc Muncie 12
Powerspec G512
Saybyebugs At Walmart
2007 Jaguar XK Low Miles for sale - Palm Desert, CA - craigslist
Miami Vice turns 40: A look back at the iconic series
Love Words Starting with P (With Definition)
Tlc Africa Deaths 2021
Youravon Com Mi Cuenta
Nope 123Movies Full
Kushfly Promo Code
Diario Las Americas Rentas Hialeah
Game Akin To Bingo Nyt
Marion City Wide Garage Sale 2023
Latest Posts
Article information

Author: Ray Christiansen

Last Updated:

Views: 6811

Rating: 4.9 / 5 (69 voted)

Reviews: 84% of readers found this page helpful

Author information

Name: Ray Christiansen

Birthday: 1998-05-04

Address: Apt. 814 34339 Sauer Islands, Hirtheville, GA 02446-8771

Phone: +337636892828

Job: Lead Hospitality Designer

Hobby: Urban exploration, Tai chi, Lockpicking, Fashion, Gunsmithing, Pottery, Geocaching

Introduction: My name is Ray Christiansen, I am a fair, good, cute, gentle, vast, glamorous, excited person who loves writing and wants to share my knowledge and understanding with you.