...
Code Block |
---|
[cwid@hostname /athena/labname/scratch/cwid ]$ vim demo.txt -- opens current or will create new text file |
Insert Mode
When you first open a document, you will always start in normal mode and have to enter insert mode. To enter insert mode where the cursor is currently located in the file, press the letter i
or the Insert
key. Additionally, you can press the letter a
(for append) if you would like to enter insert mode at the character after the cursor. To exit insert mode, press the Esc
key. When in insert mode, -- INSERT --
will be visible at the bottom of the shell. Navigation in insert mode is done with the standard arrow keys.
Examples
Editing a Script with vim
...
Open terminal.
...
Type vim myscript.sh
.
...
Press i
to enter Insert mode.
...
Make your changes.
...
Press Esc
to return to Normal mode.
...
...
Editing Features
Here are some important commands to know:
Undo the previous command, even the last edit in insert mode, with the command
u
Redo the previous command (after undo) with
Ctrl-R
Copy (yank) characters, words, or lines:
yl
to copy a single character under the cursoryy
to copy the current liney#y
or#yy
where#
is replaced with the number of lines you want to copy (i.e.y25y
will copy 25 lines).
Paste (put) characters, words, or lines:
p
will paste after the cursor for characters and wordsP
will paste before the cursor for characters and words
Delete characters, words, or lines:
dd
to delete the current linedw
to delete a wordd#d
or#dd
where#
is replaced with the number of lines you want to delete (similar to copy).
Search and Replace:
Use
/
to search for text andn
to move to the next occurrence andN
to jump to the previous occurrence./word
followed byEnter
. Phrases can also be used.
Use
:%s/old/new/g
to replace all occurrences of “old” with “new” in the file
For more features on vim
Code Block |
---|
cwid@login-node ~ $ man vim |