You will eventually need to create or edit a file and you won’t have access to a graphical editor like your IDE or Sublime Text. The most common text editor that is installed at the command line is Vi or Vim. Vim is the “improved” version of Vi and should be available to you.
Vim is an intricate beast. Here we will cover the basics to get you in and out of Vim, find something within the file, and make a couple of changes to a file.
- Command mode - allows you to execute commands on the file
- Insert mode - allows you to type into the file
When you open the editor, you will be in Command mode. To get into Insert mode hit the i
key. To get back into Command mode, hit your ESC
key. When you are in Insert mode, you will see this at the bottom of the editor:
-- INSERT --
ESC
- change back to Command modei
- insert texta
- append textx
- delete character at cursord
- delete line at cursoru
- undo last change/your_search_term
- to search for your_search_termn
- search again for your_search_termyy
- yank a line of text (copy the line)yw
- yank a word of text (copy the word)p
- paste the yanked text:q
- quit:q!
- quit without saving changes:w
- write (save):x
,:wq
- write and quit (save and quit)
Try this now:
BabyMac:~ jennapederson$ vim hello_world.sh
Type the following into the file (we will be using this later):
#!/bin/bash
echo "Hello World!"
The #!/bin/bash
is commonly referred to as “Hash bang bin bash” or even “She-bang”. The file type suffix is merely a convention. An executable can be any type of script - sh, bash, perl, php, ruby, python, etc. This line of code indicates which shell to use.
Back: Basic Commands Forward: Permissions