File Editing in Linux

Command-Line Text Editors

  1. vim

    • Powerful and Customizable

    • To open a file: vim filename

    • Enter Insert mode: i

      • Write anything once in insert mode

    • Save changes and exit: Esc, then :wq!, and Enter

[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.

 

image-20240625-164440.png
Insert Mode
image-20240625-164343.png
Saving changes


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 cursor

    • yy to copy the current line

    • y#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 words

    • P will paste before the cursor for characters and words

  • Delete characters, words, or lines:

    • dd to delete the current line

    • dw to delete a word

    • d#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 and n to move to the next occurrence and N to jump to the previous occurrence.

      • /word followed by Enter. 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

cwid@login-node ~ $ man vim