Every developer has heard of 'vi' at least once in their life. That empty screen with a flashing cursos can be a little intimidating at first. After a few attempts of typing your text and nothing is echoed back to the screen, than it becomes really intimidating. :)
To help those out there who want to be able at least to quit vi editing without closing their Terminal window, here are a few tips and basic commands.
The first thing you should know about vi is that it has two modes of operation:
- Command Mode: This is the vi starting mode, and you can enter it any time by simply typing 'ESC'
- Edit Mode: To enter this mode it is necessary to type one of the editing commands while in 'command mode'
And this is a very very short list of vi's useful commands:
- vi filename: opens a file called 'filename'in vi
- ESC w: saves the file being edited
- ESC wq: saves the file being edited and quits vi
- ESC q: quits vi
- ESC q!: quits vi even if there are unsaved changes in the file
- i: insert text before cursor
- I: insert text at beginning of current line
- a: append text after cursor
- A: append text to end of current line
- o: open a new line below the cursor for editing
- O: open a new line above the cursor for editing
- r or c: replace/change single character under cursor
- cw: change current word with new text
- x: delete single character under cursor
- Nx: delete N characters, starting with the one under cursor
- dw: delete a single word
- D: delete the remainder of the line
- dd: delete entire current line
- /string: search forward for occurrence of 'string' in text
- ?string: search backward for occurrence of 'string' in text
- n: move to next occurrence of string
- N: move to next occurrence of string in the opposite direction
- CTRL G: provides line information at bottom of screen
.... or you could simply type man vi in the command line next time! ;)
Note: all these commands also work on vim (Vi IMproved).