Need to edit a file in unix cli.
Not familiar with how to achieve this goal. You’ve been using notepad and word and some other ++ version of them, having been spoon fed of the windows establishment all your life. But *Today* you will become S00p3rL33t – just by learning VI.
- Start by typing vi followed by the file(s) you want to edit.
Example: vi one.txt two.txt etc.txt
- There are two modes: command mode and insert mode. You will begin in command mode. (Unless you have a config file which changes this. Rare.)
- Hitting the Esc key always puts you in command mode.
It never takes you out of command mode.
- Commands only work in command mode. Get used to hitting Esc a lot.
- Esc is your friend. If you begin to issue a command and then change your mind, always hit Esc a few times to cancel.
- Remember: ALL COMMANDS ARE CASE-SENSITIVE!!!
Basic Editing Commands
- Hitting i or a from command mode puts you in insert mode. i starts inserting text before the cursor. a starts appending text after the cursor.
- Use h j k l to move left, down, up, and right, respectively.
- Use w e b to move forward one word, forward to the next end of a word, and back one word, respectively.
- Use x to delete one character, dw to delete one word, or dd to delete one whole line.
- Use a number before a command to repeat the command that many times.
Example: 3dw deletes three words starting from the current cursor position.
- u (lowercase) means undo the last change.
- ZZ (uppercase) saves any changes and quits.
- :q! abandons any changes and quits.
Cut and Paste
- yy yanks the current line into the buffer. This is like copying the current line to the clipboard.
Example: 3yy yanks three lines into the buffer starting with the current one.
- p (lowercase) pastes the buffer contents to the line under the current one.
- P (uppercase) Pastes the buffer contents to the line above the current one.
- Lines deleted with dd also end up in the buffer and can be pasted.
- Words deleted with dw also end up in the buffer and can be pasted.
Advanced Cut and Paste
- There are 26 lettered buffers, labeled from a to z (lowercase), in addition to the usual “clipboard” one.
- Lettered buffers are accessed with the “ double quote.
Example: “a3yy yanks three lines into the a buffer starting with the current one.
Example: “bp pastes the contents of buffer b to the line under the current one.
Going to a Line or Column
- Type a number followed by G (uppercase) to go to that line number.
Example: 24G goes to line 24.
Example: G goes to the last line of the buffer.
- Type a number followed by | (pipe) to go to that column in the current line.
Example: 40| goes to column 40.
Repeat the Last Action
- Use . (period) to repeat the last action.
Example: If you hit i to insert, then type “hello world” followed by the Esc key to enter command mode, then move to another line and hit . to repeat, the words “hello world” will appear before the current cursor position.
Search for a String
- Use / (foward slash) followed by a string to search for that string.
Example: /foobar would find the first occurrence of foobar after the cursor position. It would also find, for example, something like foobariciousness.
- After the first occurrence of the string is found, use n (lowercase) to find the next occurrence. Continue hitting n as needed.
- Use N (uppercase) to find the previous occurrence of the string.
- Use ? (question mark) followed by a string to search backwards through the file for that string.