December 28, 2013 · coding cream debian editor howto linux programming raspberrypi tricks vim windows

Using VIM Text Editor

Text editors are programs used to create or edit files. One of the most popular editors on Linux/Unix systems (also available for other platforms) is vim. Though it does take some time getting use to, this easy to follow HowTo will walk you through the essentials of using vim.


Starting Vim | Modes of operation | Exiting vim | Inserting | Deleting, Copying and Pasting | Navigating | Search and Replace** | ****Cream/GVim | vimtutor**


For those who are wondering why should they learn vi and it's brethren, it really boils down to this:

Not every *nix system has nano, ed, joe, leafpad, etc. However every *nix system has vi. So, if you ever decide to venture into a career in IT, it is best to learn vi now!

by Richard-TX

Starting the vim text editor

The command to start the vim editor is simply

vim

followed by the filename. For example, to edit a file named test, you would type

vim test

and hit [Return].

New files will be empty, as with every new file in any editor. When creating a new file, you will see a screen filled with tilde characters (~) down the left side of the screen. Those represent blank lines beyond the end of file. At the bottom of your screen, the file name will be shown, along with a line and character count. These come in handy for referencing:

vim

TOP

Modes of Operation

Vim has two modes of operation, "command mode" and "insert-text mode". You're always in command mode when vim first starts. This command mode allows you to issue a desired action. While insert-text mode simply enters anything you type into the file. Through-out this document we're going to provided lists of frequently used commands, be sure to pay attention to case sensitivity as it does matter and can have different results.

KeystrokeDescription
**Esc**Hit the "Esc" (Escape) key at any point in time to enter command mode. If you were already in the command mode when you hit "Esc", don't worry. It might beep, but you will still be in the command mode.
>
**Note:** When in insert-text mode, you should see "” INSERT "” at the bottom of the window:

vim2

TOP

Properly exiting vim

In order to exit vim, you must first be in command mode. Type colon, and 'q', followed by return. If your file has been modified in any way, the editor will warn you of this, and not let you quit. To ignore this message, you may force quit command without saving. This allows you to view a file without actually saving any changes.
KeystrokeDescription
**:q**Quits the session.
**:q!**Forces a quit the session and does not save changes.
**:w**Writes (saves) changes to current file.
**:wq**Writes changes to current file and then quits.
**:wq!**Forces a write to current file and then quits.
>
**Note:** Files are not saved unless you issue the command to do so, thus closing the terminal to exit will result in the lost of any changes made to the file.

TOP

Inserting

KeystrokeDescription
**i**Inserts text to the left of the cursor.
**I**Inserts text at the beginning of the line, no matter where the cursor is positioned on the current line.
**a**Begins inserting after the character (append) on which the cursor is positioned.
**A**Begins inserting at the end of the current line, no matter where the cursor is positioned on that line.
**o**Begins inserting text on a new, empty line, below the current line. This is the only command that will allow you to insert text BELOW the LAST line of the file.
**O**Begins inserting text on a new, empty line, above the current line. This is the only command that will allow you to insert text ABOVE the FIRST line of the file.
**r**Replace single character under cursor position.
**R**Replace all characters, starting under cursor position, until Esc is pressed.
[TOP](#top)

Deleting, Copying and Pasting

KeystrokeDescription
**D**Delete to end of the line.
**dd**Delete line at cursor position.
**x**Delete character right of the cursor position.
**X**Delete the character before the cursor.
**d$**Delete to the end of the line, including the current character.
**ndw**Delete to the beginning of the line, excluding the current character. (n is a number)
**ndW**Delete a word(s), stopping at punctuation.
**nde**Delete to the end of next word.
**ndd**Delete a line(s).
**y**Yank (copy) the text at cursor position.
**yy**Yank (copy) the line at cursor position.
**p**Paste after the current cursor position.
**P**Paste before the current cursor position.
[TOP](#top) ### Navigating
KeystrokeDescription
**h**Move cursor to the left one character.
**l**Move cursor to the right one character.
**j**Move cursor down one line.
**k**Move cursor up one line.
**^**Move cursor to the beginning of the line.
**$**Move cursor to the end of the current line.
**G**Move cursor to the last line of your file.
**1G**Move cursor to the first line of your file.
**w**Move cursor forward to the next word, stopping at punctuation.
**W**Move cursor forward to the next word, ignoring punctuation.
**e**Move cursor forward to the end of the word, stopping at punctuation.
**E**Move cursor forward to the end of the word, ignoring punctuation.
**:n**n is the line number you'd like to jump to.
**:set number**Show line numbers down the left side of the window.
> **Note:** The arrow keys can be used for basic navigation.

TOP

Search and Replace

KeystrokeDescription
**f**Search the current line for the character specified after the 'f' command. If found, move the cursor to the position.
**F**Search the current line backwards for the character specified after the 'F' command. If found, move the cursor to the position.
****Repeat the last f or F command (see above).
**,**Repeat the last f, F, t or T command in the reverse direction.
**t**Search the current line for the character specified after the 't' command, and move to the column before the character if found.
**T**Search the current line backward for the character specified after the 't' command, and move to the column before the character if found.
**/**Search the file downwards for the string specified after the /.
**?**Search the file upwards for the string specified after the ?.
**n**Repeat last search given by '/' or '?'.
**:s/old/new**This will find the first occurrence of "old" and replace it with "new".
**:s/old/new/g**This will find globally (all) occurrences of "old" on the current line and replace them with "new".
**%s/old/new/g**This will find every occurrence of "old" in the file and replace them with "new".
**%s/old/new/gc**Same as above but asks for confirmation before changing.
[TOP](#top)

Cream

If you prefer to use GUI instead of CLI, there exists a GUI version - Cream. I highly recommend Cream if you are looking for a simple text editor, or want to learn Vim the easy way.

You can easily install Cream, it's already there in the repositories. Just

sudo apt-get install cream

To install the graphical version of Vim (Gvim), type

sudo apt-get install vim-gnome

If you are using Vim in a terminal and want to switch to Gvim just go into normal mode (i.e., ESCCtrl-c, or Ctrl-[) and type

:gui

Gvim will launch and the terminal will pass the file to Gvim.

gvim

TOP

vimtutor

A good tool to learn vim is there, installed alongside with vim.  The Vimtutor is useful for people that want to learn their first **Vim **commands. Just type

vimtutor

in your command line, and you'll be greeted with first lesson on using vim:

vimtutor

TOP

  • LinkedIn
  • Tumblr
  • Reddit
  • Google+
  • Pinterest
  • Pocket

Contact