Editor and Learn Basic Commands

Vim (Vi Improved) is a very powerful and configurable text editor that is frequently used for system administration, text editing, and programming tasks. Here’s how to install Vim on Ubuntu and learn some basic commands to get you going.

Step 1: Set up Ubuntu with Vim

Vim can be installed on Ubuntu by using the apt package manager.

1. Revise the Package List

Update your package list first by opening a terminal:

sudo apt update

2. Set up Vim

Use the following command to install Vim after the package list has been updated:

sudo apt install vim

3. Check the Installation

After installation, check the version of Vim to make sure it is installed:

vim-version

Step 2: Fundamental Vim Instructions

There are two main modes in Vim:

1. Normal mode: This is the standard mode in which you can copy, paste, delete, and navigate text.
2. Insert mode: This is where you can actually change or add text.

To get you started, here are some fundamental Vim commands:

1. Using Vim to Open and Close Files:

• Open a file:

vim filename.txt

• Close Vim:

  • If you have not made any changes and are in Normal mode:
:q
  • if you have unsaved changes:
:wq  # Save and exit
  • To exit without saving modifications:
:q!

2. Changing Modes

• Normal mode: To make sure you’re in normal mode, press Esc.
• Insert mode: To enter insert mode and begin typing, press I.

As an alternative, you can start adding text at the beginning of the current line by pressing I.

• Command-line mode: To save, exit, search, and other operations, press :

3. Navigating a File

The following commands can be used to navigate in Normal mode:

  • To move the cursor, press the arrow keys or h, j, k, l (left, down, up, and right).
  • w: Advance the cursor one word.
  • b: Advance the pointer one word backward.
  • 0: Go to the line’s beginning.
  • $: Proceed to the line’s end.
  • gg: Open the document at the start.
  • G: Go to the document’s conclusion.
  • Ctrl + f To advance one page.
  • Ctrl + b To go one page back, press.

4. Using Vim to Edit Text

• To enter text, press I to enter insert mode and begin typing.
• To remove a character, move the cursor over it in Normal mode and hit x.
• To remove a word, move the cursor to the start of the word and hit dw.
• To remove a line, press dd.
• Undo changes: Press u to reverse the most recent modification.
• To redo the undone changes, press Ctrl + r.

5. Text Copying, Cutting, and Pasting: Copy (yank) a line:

  • To copy the current line in Normal mode, press yy.
  • To copy (yank) a word, move the cursor over it and hit yw.
  • To cut (delete) a line, press dd while in Normal mode. Paste the text that has been cut or copied: To paste, move the cursor to the desired location, then press P to paste before or after the cursor.

6. Text Searching and Replacing:

• Look up a word: Enter the word you’re looking for after pressing /.
/text

o Determine the following event:

To move on to the next instance, press n.

o Locate the prior instance:

To access the prior instance, press N.

o Change the text:

 Type- :%s/oldword/newword/g in Normal mode.

                          ‘This will change every oldword instance in the entire file to a newword.’

7. Preserving Modifications:

• Save the document:

Press Esc, then type :w to save the file in Normal mode.

• Save and close:

Use :wq to save and exit Vim.
Alternatively, in Normal mode, press ZZ (uppercase).

8. Accessing Several Files

Vim allows you to open multiple files at once:

• Launch several files at once:

vim files 1 and 2.

• Change between files: To access the next file, press :n; to access the previous file, press :prev.

• End a file: When you are in the file you wish to close, press :q.

In conclusion

Here is a brief rundown of the most important Vim commands:

• Normal mode: The editing and navigational default mode.
• Insert mode: To begin editing text, press I.
• Navigation: To move, use h, j, k, l, w, b, and $.
• Editing: To remove words, lines, or characters, use x, dw, and dd.
• Undo/Redo: Use u to undo and Ctrl + r to redo.
• Search and Replace: Use / to search and :%s to replace text.
• To save and exit: type :w, :wq, :q!.

You can experiment with Vim’s more sophisticated features and customizations as you become more accustomed to it.