This is an old revision of the document!
Create And Edit Text Files
General Information
About this page/how-to/script.
Linux Text Editors
- emacs ⇒ older text editor, not always on a system
- nano ⇒ very simple edtior, not always on a system
- vi/vim ⇒ vi always on the system, vim sometimes on a system
- For this reason, learn vi/vim.
vi/vim
vi or vim will almost always be installed on a Linux distro, so it is the best to learn.
Create new file “file1”
vi file1
- Command Mode ⇒ Cannot type into the file, used for movement/editing
- Insert Mode ⇒ Type in this mode
- Ex Mode ⇒ Searching, Saving, Quiting, and Changing Vi's options
Command Mode
Movement
- i ⇒ enter Insert Mode
- h ⇒ move left
- j ⇒ move down
- k ⇒ move up
- l ⇒ move right
- G ⇒ goto last line in file
- 1G ⇒ goto first line in file
Editing
- yy ⇒ yank line (copy)
- p ⇒ paste before current line
- P ⇒ paste after current line
- u ⇒ undo last change
- dd ⇒ delete line
- 5dd ⇒ delete 5 lines
- dw ⇒ delete current word
- cw ⇒ delete current word and enter insert mode (change word)
- cc ⇒ delete current line and enter insert mode
Insert Mode
- Esc ⇒ leave Insert mode, enter Command Mode
Ex Mode
- :q ⇒ quit
- :w ⇒ write changes (save)
- :wq ⇒ write (save) and quit
- :q! ⇒ quit without saving changes
- :%s/dog/cat ⇒ replace first occurance of “dog” on each line with “cat”
- :%s/dog/cat/g ⇒ replace all occurances of “dog” with “cat” (g = global)
- :! ⇒ run shell commands from vi
- :r oldfile ⇒ copy contents of oldfile into current file being edited
- :e file1.txt ⇒ load file1.txt into the editor (only if current file being edited has been saved)