Posted in scripting, Unix

VI – How to exit VIM

Tenha uma

Hello Boys and Girls

As I promised, I would share all knowledge I gain, as it might by useful for any of you (either tester or not).

Last time we were talking about basic concept of Unix. It would be great to be able to talk to it, wouldn’t it? 🙂 In Unix and Unix-like systems all commands are run from command line in some script languages. There is few of them, but bash seem to be most recommended. But it is not language that we will be talking about today.

Today we will be editing files.

From command line.

In Unix.

Like… really.

What is VI

There are some editors that enables you to manage files in Unix.

Editing files using the screen-oriented text editor vi is one of the best ways. This editor enables you to edit lines in context with other lines in the file.An improved version of the vi editor which is called the VIM.

Let’s start from basics.

The name “vi” is derived from the shortest unambiguous abbreviation for the ex command visual, which switches the ex line editor to visual mode. The name vi is pronounced /ˈvˈ/.

You have a file – let’s assume it is some shell script (sh) – a file that was written to run some process. You are able to see (edit) it in your Notepad ++ (I need to write about it too), but it’s not the point. You can be a smart-ass and edit it directly in Unix – using VIM. How to do it?

Open your Unix console -> Find the file location -> type vi NameOfTheFile.sh -> Click Enter.

1Tadaaaaam 🙂

Piece of cake.

Vimtutor

For those of you, who are really interested in getting familiar with vim – there is an awesome built-in solution. It’s called vimtutor. Type vimtutor in your console, click Enter, and you’ll have access to set of easy lessons which give you some basic abilities.

VIM is a bit like Agile for V-lovers – You have to unlearn what you have learned. Nothing there is as you assume it would be – for instance moving up and down in the console bases on 4 keys:

k

h                                                                                                                                    l

j

 

It is so straight-forward to remember that l is a shortcut for right 🙂 Right?

First thing to remember is how to get out of vim

:q!

Those 3 keys may save your life.

Hints

To be honest, vim is useful, especially, when you are working with number of files, have to edit them and basically move around. It helps. It might be colorful, if you wish and it does it’s job.

Useful hint: VI is case-sensitive. It means that you have to be careful what you are writing. :Q! won’t get you out of there 🙂

Vim works in two modes:

  • command
  • insert

To proceed any operations – you have to be in command mode. In order to do so – press Esc twice.

To edit your file and change text inside them – you have to be in insert mode – when you want to switch to it –  i (insert) or a (append).

Inside vi you don’t have to press Enter to confirm your operation (that is also a useful hint).

938f75ace142cfa0b22b4dbe5cc558a3

Commands and descriptions

 

What might be useful for you at the beginning of playing with insert/append mode:

  1. Editing files

i – Inserts text before the current cursor location

a – Inserts text after the current cursor location

A – Inserts text at the end of the current line

p – Pastes a new line

 

       2. Changing files

x – Deletes the character under the cursor location

X – Deletes the character before the cursor location

dd – Deletes the line

dd<10>– Deletes ten lines at once , starting from the place where cursor points

u – Undo of the last command

>Moves text to the right

< – Moves text to the left

yy – Copies the current line

 

What might be useful for you at the beginning of playing with command mode:

:q! – Leaves the editor without saving

:w – Saves the file

😡 – Leaves the editor with saving

:help – Helps you

:e name – Opens file for editing

 

And couple of my favorites:

:set nu – Sets the number for each line in the file (eases searching through logs a lot,                            believe me)

/              – searches through pattern – for example /Setting

:10,30s/^/#/ – an example of how to comment lines from 10 to 30 (you can try any other                             combination on your own)

 

The vi has the capability to run commands from within the editor. To run a command, you only need to go to the command mode and type :! command.

VI runs on every Unix environment without any additional packages or libraries. It gives you the opportunity to work with your files even the the connection and editing environment are rather poor.

I hope that set of hint will be useful for you. There is plenty of online guides and examples how to play with vi. Don’t be afraid and remember – if I was able to do it – you are capable of it to.

Enjoy!

Advertisements