File Editing on the Omega

The projects are based on code, which live in files, so it stands to reason that we’ll be doing some file editing for each of our projects.

On the Omega, there’s one text editor by default - Vim. It’s different from more familiar, visual editors in how it works, so we’ll cover the basics super quickly to get to making cool things faster!

Creating or Opening a File

To work with a file, simply call vim <filename>. It will work if you do vi <filename> as well.

If the file already exists, it will open it in vim. If it doesn’t, it will open an empty temporary file with the given name.

The temporary file won’t be permanent until you save it!

If you type things immediately when vim starts, nothing will come out. So how do we get text in?

Writing Text

Hit i to get into ‘insert mode’, now we can start entering text!

The controls of insert mode should be quite familiar if you’ve used notepad before. The keys will insert characters, the arrow keys provide nagivation, and home/end/pgup/pgdn will behave accordingly.

Vim is based on different modes. Entering characters and pasting with ctrl-shift-v will only work in insert mode.

Vim begins in normal mode, where all keystrokes are interpreted as commands. So all the text you want to paste will be interpreted as commands, causing lots of unpredicatble changes to the file.

If mistakes were made, what can we do?

Undo and Redo

Undo is a normal mode function, hit ESC and press u to undo the last bit of changes made. To redo hit ctrl-r in normal mode.

The reason we hit ESC first is to return to normal mode. So instead of inserting u, vim will undo the last changes we made.

Once the code is fixed, it’s time to save.

Saving Changes

Hit ESC to enter normal mode, then :w and enter.

Entering : calls the ‘command line’ of vim, where we can run vim’s less often used functions. The command we give it is w which stands for ‘write to file’. Since : works like a command like, we have to enter to send our command to it.

The more commonly used commands in vim are related to navigating, copy/pasting, and editing chunks of text - like u for undo.

Now that we’ve saved, we’ll have to exit vim to test the script.

Quitting and Saving changes

ESC, then :wq enter.

The quitting process is very similar to saving - return to normal mode with ESC, type : to enter the q command to quit vim.

Vim also has the ability to accept multiple commands and execute them in order. So we can save and quit by typing :wq.

Quitting without Saving Changes

:q!

Forcing vim to quit can be done by adding ! to the end of the :q command.

Normally when trying to exit vim with unsaved changes, vim will deny the attempt as a safe measure. But quitting without saving is also useful to revert really big changes.

More on Vi

There’s a lot more than meets the eye with vim. It’s actually a very powerful editor under the hood and can be installed in all major operating systems. If you want to learn how to make the most out of it, Open Vim has a fantastic tutorial that can get you started.