VIM instead of Vi for editing text

Posted under » Linux on 9 July 2017

To be able to edit text quickly with little fuss and resources is a skill all tech people must possess.

"vi" is a text editor from the early days of Unix. As years go by, CPU power has increased so several free vi-like editors emerged. Vim ("vi improved") is one of this editors. The improvement over vi are numerous. Syntax higlighting and plugins are probably the most cited.

In ubuntu, you must install it first. To begin using VIM is quite straight forward. First you

vim /heh/lkyburninhell.txt

If document doesn't exist, it will create a new one. You may start by using the insert mode or the replace mode. Use the insert key to toggle. To exit, use the Esc key.

To exit, go to command mode or command prompt by typing : . Then you will be taken to the bottom of the screen. Type w which will save the txt like so.

:w
:wq
:q!

Lets say you want to save as the file. Just add the new name after :w

:w newfile.txt

You can undo by

u

You can scroll history commands by the usual up and down key.

There can be more than one command given. For example. wq means write and quit. Where q! means to quit without saving.

You can type vimtutor to get help or the command :h.

To find stuff you

:set ignorecase // or smartcase
:? word2find

Press "esc" then go to next or previous fine by "n" or "N".

To show line numbers, :nu or :number. To go to line 123, :123

To move around you can use the arrow keys. And the move forward word-wise, use w. To go backwords, type b.

0	Moves cursor to the start of the line
$	Moves cursor to the end of the line (inclusive of newline)
g_	Moves cursor to the end of the line (exclusive of newline)
b	Moves cursor backward through each word
e	Moves cursor to the end of the word
w	Moves cursor to the start of the next word
gg	Moves cursor to the start of the buffer
G	Moves cursor to the end of the buffer
%	Moves cursor to the next bracket (or parenthesis)
(	Moves cursor to the previous sentence
)	Moves cursor to the next sentence
{	Moves cursor to the start of a paragraph
}	Moves cursor to the end of a paragraph

Copy and paste takes a bit of getting used to

  1. First get out of insert mode and go to normal mode
  2. Press v to go to -visual mode-
  3. move the cursor to the end of what you want to cut
  4. Press c or d (short for del) to cut (not copy)
  5. or y (short for yank) to copy
  6. Press P to paste before the cursor, or p to paste after

You can toggle between 2 (or more) opened txt by

$ vim text2.txt blog.txt

To toggle type :N (next doc) or :n (prev doc)

Normally you can copy and paste while in the --insert-- mode but sometimes you cannot copy from the OS clipboard to VIM. My way to do this is by cat > tocopy.txt. Afterwhich you open 2 txt files using vim and copy as per normal.

The drawback is it will overwrite the tocopy.txt. So it is sometimes better to append (add to the end) the text to the file and then open the document using VIM to cut and copy. We do this by the echo command and the >> operator.

$ echo "copied text" >> doctoaapend.txt
$ vim doctoaapend.txt

For VIM tab usage. For VIM search and replace.

web security linux ubuntu python django git Raspberry apache mysql php drupal cake javascript css AWS data