Working with Text Files
Chris Hunt - Impressive Ruby Productivity with Vim and Tmux - Ancient City Ruby 2013
Working with VI
Vi Modes
Vi uses two modes: command mode, which is used to enter new commands and insert mode (also referred to as the
input mode), which is used to enter text.
Enter insert mode:
- Press i to insert text at the current position of the cursor.
- Press a to append text after the current position of the cursor.
- Press o to open a new line under the current position of the cursor (my favorite option).
- Press O to open a new line above the current position of the cursor.
Pressing the Esc key returns you to command mode from insert mode.
#### Saving and Quitting
The most common method is to use the :wq!
If you want to save it under a new file name, just enter the new name after the :w command:
:w newfile
Undo
Cutting, Copying, and Pasting
- Use the v command to enter visual mode.
- Use d to cut (in fact, delete) the selection. This will remove the selection and place it in
a buffer.
- Use y to copy the selection to the area designated for that purpose in your server’s
memory.
- Use p to paste the selection. This will copy the selection you have just placed in the
reserved area of your server’s memory back into your document. It will always paste
the selection at the cursor’s current position.
Deleting Text
- Use x to delete a single character. This has the same effect as using the Delete key while
in insert mode.
- Use dw to delete the rest of the word. That is, dw will delete everything from the cursor’s
current position of the end of the word.
- Use dd to delete a complete line. This is a very useful option that you will probably like
a lot.
Moving Through Text Files
- Use the g key twice to go to the beginning of a text file.
- Use G key twice, you can go directly to the end of a text file.
- To search text, you can use / followed by the text you are searching. For instance, the
command /root would find the first occurrence of the text root in the current file. This
command would search from the current position down in the text file. To repeat this
search action, use n (for next). To repeat the search in the opposite direction, use N.
- Use ? followed by text you are using to search text from the current position in the
text upward in the text file. For example, the command ?root would search for the text
“root” from the current position in the text upward. To repeat this search action, use n
for next. To repeat the search in the opposite direction, use N.
Changing All Occurrences of a String in a Text File
global substitute