Getting started with vim

Vim is an in terminal text editor. The improved version of vi. One of these variants is often preinstalled on every linux host.

How to use vim

Vim allows you edit files, it'll also create any files that don't exist and open them for editing. Typing vim [filename] is the same as typing touch [filename] followed by vim [filename] you still open an empty file for editing. Let's start by creating and editing a file.

vim hello.sh

We'll be writing a simple script using vim. Vim has two basic modes - a command mode and an Insert mode. Insert mode allows us to write code and make changes to a file. While command mode allows us to change things about our vim session, issue Unix and Linux commands as well as save and or quit the vim session.

There's also a visual mode but we won't be using that here.

Now that we're in our file we need to enter insert mode this is simple within vim but not intuitive. Simply press i . This will change us from command mode to insert mode we'll type two things: our shebang line to inform our OS how to execute our script #!/bin/bash and our code underneath it echo "Hello World"

hello.sh
#!/bin/bash # This is a shebang it tells our OS how to execute our script
echo "Hello World" # echo outputs data to stdout aka prints to the shell

So now that we've written our first script. How do we save it? You'll first need to enter command mode to do this press the [escape] key on your keyboard. If you do not have one for some reason you can hold the [control] + [ key at the same time.

Once you've entered command mode you won't be able to edit text in the same way. There are several keybindings so don't start pressing random keys.

Once you've entered command mode you'll need to type the colon key : this should show up at the bottom of the editor.

If you do not see the colon continue attempting to enter the command mode and enter the colon until it appears

Once the colon is present you can type wq to save your changes and exit vim.

Last updated