Vim

From aldeid
Jump to navigation Jump to search

Description

Vim is a powerfull text editor for *nix. Vim (Vi Improved) is the improved version of the famous vi editor.

Installation

vim-tiny is installed by default on most *nix distributions. However, it has limited features and I recommend that you install vim complete as follows:

$ sudo aptitude install vim

Vim modes

There are several modes in Vim:

  • Interactive: This is the default mode. You can cancel the last action, copy and paste, ...
  • Insert: This is the mode that enables to add text. To enter this mode, press the i key.
  • Visual: This mode enables to select text, cut, copy, paste, ... To enter this mode, press the v key.
  • Command: In this mode, you can quit vim and save a file but also to launch commands.
Note
To leave a mode, press the ESCkey.

Open, Save, Quit

Open a file

To open a file, do as follows:

$ vim fileToOpen

Or from vim directly (from interactive mode), press :edit <file> where <file> is the file to open.

Save

  • To save modifications without leaving vim, press :w.
  • To save and quit vim, press :x.
Note
If you were editing a new file, you will have to concatenate the name of the file to your command (e.g. :w test.txt).

Quit

  • To quit without saving eventual changes, press ESC to ensure you're not in a mode (e.g. edition) and then press :q!.
  • To save and quit, press :x or :wq.
Note
If you haven't made any modification to a file and you just want to quit, press :q.

Navigate

  • To move in a file, you can use the arrow keys or use h (left), j (down), k (up), l (right).
  • To go at the beginning of a line, press 0
  • To go at the end of a line, press $
  • To jump from words to words, press w
  • To jump to the beginning of a file, press gg
  • To jump to the end of a file, press G
  • To go to a specific line, press :<n> or <n>G where <n> is the line number. For example, to go to line 134, press :134 and then ENTER.

Delete

  • To delete a character from the interactive mode, press x or 5x to delete 5 characters
  • To delete a character from the edit mode, press DEL.
  • To delete an entire line from the interactive mode, press dd. To delete 3 lines, press 3dd.
  • To delete a word from the interactive mode, place your cursor on the first letter of the word to delete and press dw. To delete 4 words, press 4dw.
  • To delete all content from the cursor to the beginning of the line, press d0.
  • To delete all content from the cursor to the end of the line, press d$.
Note
Notice that using the d key does not only deletes the content. It saves it into memory to be eventually pasted later.

Cut, Copy, Paste

From the visual mode

  • First enter the visual mode by pressing v from the interactive mode.
  • Select a text with the arrow keys.
  • Press y to copy a selection
  • Press d to cut a selection
  • Press p to paste the content

From the interactive mode

  • To cut, use the d key (see the different combination above to cut a word, a line or part of a line).
  • To copy, use the y key:
    • yy copies the entire line
    • yw copies the word where the cursor is placed
    • other possible combination (e.g. 2yw will copy 2 words)
  • To paste, use the p key. Notice that 3p will paste the content 3 times

Undo and redo

  • To cancel the last modification, press u (undo).
  • To redo, press Ctrl+R
Note
To cancel the 3 last modifications, press 3u.

Search and replace

Simple search

  • To search for a string, press /<string> where string is the text to search and then press ENTER.
  • To jump to the next occurence, press n.
  • To look backwards, press N.

Search and replace

There are several ways to search and replace. Ensure you are in the interactive mode (press ESC) and choose one of the below options.

:s/oldtext/newtext
replace oldtext with newtext only for the first occurence on the line where the cursor is placed
:s/oldtext/newtext/g
replace oldtext with newtext for all occurences on the line where the cursor is placed
:<m>,<n>s/oldtext/newtext/g
replace all occurences between line <m> and line <n>
:%s/oldtext/newtext/g
replace all occurences in the whole file