Vim – VI improved

I think it was around 1998, that I was introduced to Linux for the very first time.

I am pretty sure it was SuSE 6.2, I had a college class at the time that involved installing two operating systems and making some pretty basic system configuration changes to secure a pass for the class.

Learning about various commands and tools like YAST, and thinking how funny are these Linux guys. Yet another setup tool, still makes me chuckle to myself.

I had really only ever installed Win 95 on the old Pentium MMX 200 at home and was really pleased to be learning something new, that hopefully might give me some skills to get a job.

We only had to install Windows 3.1 and SuSE in our groups of 3 and then make some changes and record the steps.

There wasn’t the hardware for a physical machine each and the idea of  virtual machines was certainly not something we had ever discussed. Not like now, when spinning up a VM is as normal a task as sending an email.

I recall, being intrigued at the time by vi.

VIM(1) VIM(1)

NAME
vim – Vi IMproved, a programmers text editor

I was amazed that people actually worked with such a tool, having to use various keys to navigate the text and insert, copy, paste etc. I could imagine that servers over the Internet with only shell access would all be configured in such a way.

Not realizing at the time how powerful an editor it actual was. I was struggling to just do some basic tasks with it.

Some 16 years later. I am still amazed at vi, I have become more comfortable with it, the more you use it, the easier it becomes, although I do still refer at times to my cheat sheets 😉

These days vi has been improved to vim, with various excellent new features. Using vi, is actually usually using vim.

Some very basics to get you going:

create a file:

vi filename

We insert some text by leaving command mode and entering INSERT mode i:

i

~
~
~
~
~
~
— INSERT — 0,1 All

We can then type in some text such as:

Monday Tuesday Wednesday Thursday Friday

We want to save, but can not do this in insert mode, so we need to return to command mode, we press esc key to do this. I sometimes just press it a couple of times, just to be sure I am not about to start inserting colons into my text, however you do only need to press it 1 time.

We then save the changes by entering :w (don’t forget the colon)

Monday
Tuesday
Wednesday
Thursday
Friday
~
~
“filename” [New] 5L, 41C written 5,1 All

We can do things like remove lines of text by using dd.

Place your cursor at the first line of Monday. Command mode gg will take you there quickly.

Press dd to remove the first line:

Tuesday
Wednesday
Thursday
Friday
~
~
1,1 All

If you’ve  made a mistake or decided you like Mondays you can press (undo)

Monday
Tuesday
Wednesday
Thursday
Friday
~
~
~
1,1 All

We can search for strings within the file using /string

Monday
Tuesday
Wednesday
Thursday
Friday
~
~
~
/day 5,4 All

The cursor will move to the first instance of the string from were the position of the cursor is at the time you start the search. I suggest starting at the first line with gg then /day

We can replace all strings quickly that match a particular pattern, which is pretty useful feature:

Monday
Tuesday
Wednesday
Thursday
Friday
~
~
~
:%s/day/day evening/g

Changing the strings to the best time of any week day:

Monday evening
Tuesday evening
Wednesday evening
Thursday evening
Friday evening
~
~
~
5 substitutions on 5 lines 5,1 All

For more info and further examples of vim:

Monday evening
Tuesday evening
Wednesday evening
Thursday evening
Friday evening
~
~
:q!

To quit back to the command line.

And check out the vimtutor:

#man vimtutor

VIMTUTOR(1) VIMTUTOR(1)

NAME
vimtutor – the Vim tutor

SYNOPSIS
vimtutor [-g] [language]

DESCRIPTION
Vimtutor starts the Vim tutor. It copies the tutor file first, so that it can be modified without changing the original file.

The Vimtutor is useful for people that want to learn their first Vim commands.

Advertisements
Tagged

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: