LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-12-2015, 12:18 PM   #1
Elearnaca1
LQ Newbie
 
Registered: Jan 2015
Location: san jose
Distribution: redhat, linux
Posts: 1

Rep: Reputation: Disabled
Talking Good: Vi Editor- Writing your own Cheat sheet in 10 days


History:

[img] https://scontent-b-sjc.xx.fbcdn.net/...56054850_o.jpg [\img]

The name vi is derived from the shortest unambiguous abbreviation for the command*visual*in ex;

But vi was created in 1976, by the co-founder of SUN ie Bill Joy .
They had only one editor called ed. They had to use uppercase for it.

What happened is that Ken Thompson came to Berkeley and brought this broken Pascal system, and the team of 3 got this summer job to fix it. While we were fixing it, they got frustrated with the editor they were using which was named ed.

So they had a terminal at home and a 300 baud modem so the cursor could move around and they just stayed up all night for a few months and wrote vi.


How to invoke
Go to the terminal and type vi or gvim.
Vi is the older version, Your mouse will not work over this screen.
So use gvim, This is a newer version of gvim and is easy to use.

Basic Editing
The Vim editor is on of the most powerful text editors around. It is also extremely efficient, enabling the user to edit files with a minimum of keystrokes.

You may face problems in getting adjusted to vi. But hang on, every minute is worth it.


Vim starts editing a file

go to the editor and type vi <file name> or gvim <file_name> and hit enter. you will get a new window to start entering data. Initially you will be in the normal mode, to enter the data you have to enter to the insert mode where the vi start accepting data. so press 'I' and enter the required data.

Modes

The Vim editor is a modal editor. That means that the editor behaves
differently, depending on which mode you are in. If the bottom of the screen
displays the filename or is blank, you are in normal mode. If you are in insert
mode, the indicator displays --INSERT--; and if you are in visual mode, the
indicator shows --VISUAL--.

Editing for the First Time

The next few sections show you how to edit your first file. During this process, you learn the basic commands that you have to know to use Vim. At the end you will know how to edit--not fast, not efficiently, but enough
to get the job done.


Inserting Text
To enter text, you need to be in insert mode. Type i, and notice that the
lower left of the screen changes to --INSERT-- (meaning that you are in insert
mode).
Now type some text. It will be inserted into the file. Do not worry if you make mistakes; you can correct them later.


After you have finished inserting, press the <Esc> key. The --INSERT--
indicator goes away and you return to normal mode. (The indicator for this
mode is a blank indicator.) Your screen should now look something like Figure


Save the file
To save your changes type :w,
If you have opened in a read only mode, you have to force write it, so inset :w! .
This will force write the file

Getting Out of Trouble
One of the problems for Vim novices is mode confusion, which is caused by
forgetting which mode you are in or by accidentally typing a command that
switches modes. To get back to normal mode, no matter what mode you are in,
press the <Esc> key.


Moving Around

After you return to normal mode, you can move around by using these
keys: h (left), j (down), k (up), and l (right). At first, it may appear that these
commands were chosen at random. After all, who ever heard of using l for
right? But actually, there is a very good reason for these choices: Moving the
cursor is the most common thing you do in an editor, and these keys are on the
home row of your right hand. In other words, these commands are placed where
you can type them the fastest.

You can move the cursor by the arrows also, But if your have them missing you can use the above said options.

Deleting Characters

To delete a character, move the cursor over it and type x. (This is a
throwback to the old days of the typewriter, when you deleted things by typing
xxxx over them.) Move the cursor to the beginning of the first line, for example,
and type xxxxxxx (seven x's) to delete the first seven characters on the line.


Undo and Redo

u

Type a text, and delete first 5 letters of the text. And lets see how to undo the changes ( one character at a time)

Type u to undo the last delete. That delete removed the g, so the undo
restores the character.

ctrl+R
Reverse the changes
This will un do the previous command. Use this if you want to use 'u ' a lot of times.


U

There's a special version of the undo command, the U (undo line) command.The undo line command undoes all the changes made on the last line that was
edited.

This will restore all the changes done on the command.

Save

To exit, use the 'w' or 'w!' command. This command writes the file (if modified)
and exits.

discard

It is :q!. For those of you interested in the details, the three parts of this command are the colon (, which enters command mode; the q command, which tells the editor to quit; and the override command modifier (!).

q-quit

***************
Deleting a line

To delete a line, use the dd command, which deletes the line on which the
cursor is positioned.

Or use S. But this will enter into the insert mode right after deleting the line

to mention data between lines, Use the : and mention the line numbers and type d and enter.

Opening Up New Lines

To add a new line, use the o (lower case)command to open up a new line
below the cursor. The editor is then placed in insert mode.

To open a line above the cursor use the upper case 'O'.

Moving to the Start or End of a Line

END
The $ command moves the cursor to the end of a line. Actually, a bunch of
keys map to the "end-of-line" command.


The $ command takes a numeric argument as well. If present, it causes
the editor to move to the end of the next line. For example, 1$ moves you to the
end of the first line (the one you're on), 2$ to the end of the next line, and so on.


START
The ^ command moves to the first nonblank character of the line. The
<Home> or <kHome> key moves to the first character of the line, (The 0 [zero] command does the same thing.) Like every other command
previously discussed, these commands (except 0 [zero]) can take a numeric
argument. They do not do anything with it, but you can specify it if you want to.


Word Movement

Let's start with movement. To move the cursor forward one word, use the w
command. The b command moves backward one word. Like most Vim
commands, you can use a numeric prefix to move past multiple words. For
example, 4b moves back four words.


Moving to a Specific Line

can use the :set command in many different ways, which are described in
Chapter 28: Customizing the Editor. The 'number' option is a boolean option,
meaning that it can be on or off. To turn it on, use this command:
:set number
To turn it off, use this command:
:set nonumber


Where Am I?
The CTRL-G command displays a status line that indicates where you are in
the file. For example:
This indicates that you are editing a file called c2.txt, and that it has been
modified since the editing started. The cursor is positioned on line 81 out of a
total of 153, or about 52% of the way through the file. The cursor is currently
sitting in column 1.

Scrolling Up and Down

The CTRL-U command scrolls up half a screen of text. (Up in this case is
backward in the file; the text moves down on the screen. Don't worry if you have
a little trouble remembering which end is up. Most programmers have the same
problem.) The CTRL-D command scrolls you down half a screen.

Deleting Text

uppose you want to delete part of a line, say a word. The way to do that is
to start visual mode with the v command. You can now highlight the text you
want to delete with the cursor movement commands.

Deleting Text Without Visual Mode
It is possible to delete sections of text without using the visual mode. The
advantage of doing things non-visually is that you save a single keystroke. The
disadvantage is that you really have to know what you are doing as there is no
visual feedback to show you what's going on.


Visual vs. Normal Mode Delete
The visual method of deleting things lets you see exactly what you are
going to delete before you delete it. You can also make adjustments before you
delete the text. This makes things much easier for large complex deletes.
On the other hand, the normal mode delete provides no feedback. You
type d and a motion command and hope you did the right thing. (If you didn't
there's always the undo command.) The advantage of the normal mode method
of doing things is that it's quicker for small amounts of text, especially when you
know where the motion command is going to send the cursor. In other words if
you have to delete three words, d3w is probably what you want to use. But if
you have to delete 57 words (maybe 57 you're not sure) the v<move>d is better.


Changing Text

The c command changes text. It acts just like the d command, except it
leaves you in insert mode. For example, if you go into visual mode (v) highlight a
word (w) and then press c, the word will disappear and you'll be left in insert
mode.


Joining Lines

To join a set of lines in visual mode, start visual mode (v) highlight the lines
you wish to join, and press J. All the highlighted lines will be put together in
one big line. A space is placed between the pieces that are joined

Replacing Characters

The r{char} command replaces the character under the cursor with
{char}. Figure 2-18 shows how you can use the r command to replace a z with
an s. The r command can be preceded with a count, indicating the number of
characters to be replaced. In Figure 2-19, we go to the beginning of line (the ^
command) and execute 5ra to replace the first five characters with a.

Changing Case

The ~ command changes a character's case. It changes uppercase to
lowercase and vice versa. If a count is specified, the count characters are
changed.

*********

Simple Searches

To search for a string, use the /string command. To find the word
include, for example, use the command /include. An <Enter> is implied at the
end of this command. (Any time the cursor jumps to the bottom of the screen
and you type something, you must end it with <Enter>.)

Search History

Use the up arrow to move to the previous command used.
Else type the "History" to print the recent command history

also use the command "q/" to open the history window.

Searching Options Highlighting

The following command causes Vim to highlight any strings found
matching the search pattern:
:set hlsearch
If you turn on this option and then search for include, for example, all the
include strings are highlighted, as seen in Figure 3-4. To turn off search
highlighting, use this command:
:set nohlsearch
To clear the current highlighting, use the following command:
:nohlsearch
Search highlighting is now turned off; matched text will not be highlighted.
However, the highlighting will return when you use a search command.

Incremental Searches

By default, Vim uses the traditional search method: You specify the string,
and then Vim performs the search. When you use the following command, the
editor performs incremental searches:

:set incsearch

Next, you start the search by typing the /i command. Figure 3-5
shows how the editor searches for the first i and positions the cursor on it.

To turn off incremental searches, use the following command:
:set noincsearch



Searching Backward

The reverse search command (?) searches backward. The n command
repeats the last search. If a reverse search was the last one used, the n
command searches in the reverse direction. If the last search was a forward
search, the n command searches forward.


Reverse Search History
Like forward search (?) you can use the <Up> and <Down> keys to go
through the search history. You can also open a search history for reverse
searches with the command q?.


Changing Direction
Suppose you start a forward search for unsigned using the /unsigned
command. You can turn around and search in the reverse direction by using the
? Command. The n command repeats the search in the same direction. The N
command reverses the direction on the search and repeats it. To make things a
little clearer, line numbering has been turned on using the following command:

Basic Regular Expressions

The Vim editor uses regular expressions to specify what to search for.
Regular expressions are an extremely powerful and compact way to specify a
search pattern. Unfortunately, this power comes at a price because regular
expressions are a bit tricky to specify. Let's start with the simple stuff. In a
regular expression, the normal letters match themselves.


The Beginning (^) and End ($) of a Line

The ^ character matches the beginning of a line. (It is no coincidence that
this is also the command to move to the beginning of the line.) The expression
include matches the word include anywhere on the line. But the expression
^include matches the word include only if it is at the beginning of a line.
The $ character matches the end of a line. Therefore, was$ finds the word
was only if it is at the end of a line. Figure 3-10, for example, shows a search for
the pattern the with highlighting enabled.


Match Any Single Character (.)

The character . matches any single character. For example, the
expression c.m matches a string whose first character is a c, whose second
character is anything, and whose third character is m. Figure 3-13 shows that
the pattern matched the com of computer and the cam of became.


Matching Special Characters

Most symbols have a special meaning inside a regular expression. To
match these special symbols, you need to precede them with a backslash (\). To
find the] , for example, use the string the\].

Regular Expression Summary

Character Meaning
x The literal character x
^ Start of line
$ End of line
, A single character
\x Turns off the special meaning of many characters, gives special

**********
Cut, Paste, and Copy

When you delete something with the d, x, or another command, the text is
saved. You can paste it back by using the p command. (The Vim name for this is
a put, but everyone else calls it “paste”).

If you delete part of a line (a word with the dw command,
for instance), the p command puts it just after the character under the cursor


Character Twiddling

Frequently when you are typing, your fingers get ahead of your brain. The
result is a typo such as teh for the. The Vim editor makes it easy to correct such
problems. Just put the cursor on the e of teh and execute the command xp.

x Deletes the character e and places it in a register.
p Puts the text after the cursor, which is on the h.

Moving Large Blocks of Text

Let's say you wish to move 57 line block of text from one place to another.
One solution is to perform the following commands:
1. Put the cursor on the first line of the block
2. Type 57dd to delete the block.
3. Move the line just before where you want to insert the block.
4. Execute p to “put” the block on the line after the one the cursor is on.


moving selected lines to a file

To move few lines of a large file to another file , mention the start line and end line and give the new name in the command mode
: 1, 445 w! file_name

This will save the content in the new file "file_name"



To delete selected lines

Mention the deleted lines and press dd

eg :1, 25 dd

This command will delete the lines. These commands becomes very handy when you want to manipulate the data.


So to move a block using line visual mode, we execute the following commands:

1. Put the cursor on the first line of the block.
2. Start line visual mode with the V command.
3. Put the cursor on the last line of the block.
4. Delete the text with the d command (visual delete).
5. Move the line just before where you want to insert the block.
6. Execute p to “put” the block on the line after the one the cursor is on.

Marks

The Vim editor enables you to place marks in your text. The command ma
marks the place under the cursor as mark a. You can place 26 marks (a through
z) in your text. (You can use a number of other special marks as well.)
To go to a mark, use the command `{mark}, where {mark} is the mark
letter (and ` is the backtick or open single-quote character).
The command '{mark} (single quotation mark, or apostrophe) moves you
to the beginning of the line containing the mark. This differs from the `{mark}
command, which moves you to the marked line and column.


1. Move the cursor to the beginning of the text you want to delete.
2. Mark it using the command ma. (This marks it with mark a.)
3. Go to the end of the text to be removed. Delete to mark a using the
command d'a. (Entire lines will be deleted since 'a is a line type move.)

Note:There is nothing special about using the a mark. Any mark from a to
z may be used.


One nice thing about marks is that they stay with the text even if the text
moves (because you inserted or deleted text above the mark. Of course, if you
delete the text containing the mark, the mark disappears.

Yanking

For years, I used a simple method for copying a block of text from one
place to another. I deleted it using the d command, restored the deleted text
with the p command, and then went to where I wanted the copy and used the p
to put it into the text.
There is a better way. The y command "yanks" text into a register (without
removing it from the file). (Every other editor calls this a “copy”.)



The general form of the normal mode y command is y{motion}. It works
just like the delete (d) command except the text is not deleted. And the
shorthand yy yanks the current line into the buffer.

Paste it with "p"


Filtering

The linux commands will work in vi.

! Is a command utility that is very help full.
when you select the data and press !, This will pass the selected data to the particular command. Let us see this in the particular example

You want to sort lines 1 through 10 of a file.
First start visual mode (v) and highlight the first 10 lines. Then press !.
In anticipation of the filtering, the cursor drops to the bottom of the screen
and a ! prompt displays. You can now type in the name of the filter program, in
this case sort. Therefore, your full command is as follows:

!sort<Enter>


The !! command runs the current line through a filter. (I have found this a
good way to get the output of system commands into a file.)


I'm editing a
readme.txt file, for example, and want to include in it a list of the files in the
current directory. I position the cursor on a blank line and type the following:

This puts the output of the ls command into my file. (Microsoft Windows
users would use dir.) Another trick is to time stamp a change. To get the
current date time (on UNIX), I use the following command:
!!date
This proves extremely useful for change histories and such.
Note: Using !! like this is technically not filtering because commands like
ls and date don't read standard input.

Editing Another File

Suppose that you have finished editing one file and want to edit another
file. The simple way to switch to the other file is to exit Vim and start it up again
on the other file. Another way to do so is to execute the following command:
:e file
Note: :e is an abbreviation for :edit.
This command automatically closes the current file and opens the new one.
If the current file has unsaved changes, however, Vim displays a warning
message and aborts the command

Or you can force Vim to discard your changes and edit the new file using
the force (!) option, as follows:
:e! file
To edit a new,unnamed buffer use the :enew (:ene) command. It works
just like :edit only the new buffer is unnamed.

The Write command
To save the changes
At this point, you have a number of options. You can write the file using
this command:
:write
Note: :w may be used instead of :write.


The :view Command

The following command works just like the :vi command, except the new
file is opened in read-only mode:
:view file
Note: :vie may be used instead of :view.


Dealing with Multiple Files


So far the examples in this book have dealt with commands that edit a
single file. This section introduces you to some commands that can edit multiple
files. Consider the initial Vim command, for example. You can specify multiple
files on the command line, as follows:
$ gvim one.c two.c three.c
This command starts Vim and tells it that you will be editing three files.


To edit the next file, you need to change files using the :next command
(:n)


Note that if you have unsaved changes in the current file and you try to do
a :next, you will get a warning message and the :next will not work. You can
solve this problem in many different ways. The first is to save the file using the
following command:
:write
In other words, you can perform a :write followed by a :next. The Vim
editor has a shorthand command for this. The following command performs both
operations:
:wnext

Or, you can force Vim to go the next file using the force (!) option. If you
use the following command and your current file has changes, you will lose those
changes:
:next!


Auto Write Option
Finally, there is the 'autowrite' ('aw') option. If this option is set, Vim
will not issue any No write... Instead, it just writes the file for you and goes
on. To turn this option on, use the following command:
:set autowrite
To turn it off, use this command:
:set noautowrite
You can continue to go through the file list using the following command until
you reach the last file:
:next

:2 next


Which File Am I On?
Suppose you are editing a number of files and want to see which one you
are on. The following command displays the list of the files currently being
edited:
:args
The one that you are working on now is enclosed in square brackets.

Going Back a File
To go back a file, you can execute any of the following commands (all are
equivalent):
revious
rev
:Next
:N
These commands act just like the :next command, except that they go
backward rather than forward. If you want to write the current file and go to the
previous one, use any of the following commands:
:wprevious
:wp
:wNext
:wN

Matching

Though not exactly a search command, the :match (:maxx) command can
be very useful in finding test. It causes all text that matches a given pattern to
be highlighted on the screen. For example, to highlight all the word “TODO”
with the Error syntax highlighting, use the command:
:match Error /TOOD/


To find out what highlighting names are available use the :highlight (:hi)
command:
:highlight

To remove the mathched the patterns use
:match none

There are can be three matches active at one time. These are set by the
:match, :2match, and :3match commands. If some text is matched by more than
one command, the lowest one wins.

*******
Opening a New Window

The easiest way to open a new window is to use the following command:
:split

This will split the window horizontally

The :split (:sp) commands divides the screen horizontally. To divide the
screen vertically use the :vsplit (:vs) command. Figure 5-3 shows the result of
this operation:


Than you can open the required file.
Suppose you want to split and open a file directly than you have to use the below command

The following command opens a second window and starts editing the
given file:
:split file

Controlling Window Size


The :split (:sp) command can take a number argument. If specified,
this will be the number of lines in the new window. For example, the following
opens a new window three lines high and starts editing the file alpha.c:

:3 split alpha.c
A space appears here for clarity. You could have just as easily write the
following:
:3split alpha.c

To Open a New Window

The :new Command

The :new command works just like the :split command except that the
:split command splits the current window and displays the current file in both
windows while :new opens a window containing a new, empty file.


Split and View
The :sview (:sv) command acts like a combination of :split and :view.
This command proves useful if you want to look at, but not edit, a file in another
window.
There is no :vsview command. However if you wish to split the window
vertically and edit a file you can use the :vertical :sview (:vert :sview)
command. The :vertical command tells Vim to perform the command that
follows splitting the window vertically instead of horizontally.

Change the size of the Windows.
The command count CTRL-W+ increases the window size by count (default
= 1). Similarly count CTRL-W- decreases the window's size by count (default =
1). The command CTRL-W= makes all the windows the same size (or as close as
possible).
The command count CTRL-W_ (CTRL-W CTRL-_) makes the current window
count lines high. If no count is specified, the window is increased to its maximum
size.
The :resize (:res) command can be used to change the height of the
window from the command line. The argument to this command can be an
simple number (i.e. 24) in which case the window is set to that size. If the
argument begins with a plus or minus, (i.e. +5, -7), then the window in increased
or decreased in size.
If no argument is given to the :resize command, the window is enlarged
to it's maximum height.

Buffers

The Vim editor uses the term buffer to describe a file being edited.
Actually, a buffer is a copy of the file that you edit. When you finish changing the
buffer and exit, the contents of the buffer are written to the file. Buffers not only
contain file contents, but also all the marks, settings, and other stuff that go with
it.

Normally it is pretty easy to tell what buffers you have: If it has a window
on the screen, it is a buffer; if it is not on the screen, it is not a buffer.

This is a way to edit multiple files at a time with out closing it, discarding any changes
U can keep them hidden , retrive and edit them as u want.

Any buffer can be in
Active Appears onscreen.
Hidden A file is being edited, but does not appear onscreen.
Inactive The file is not being edited, but keep the information about it
anyway.

To List the buffers

:ls
:buffers
:files

To hide a particular file/buffer
:hide
This will keep it in the back ground

:to open a particular buffer
hit :buffers/ls

To open the particular buffer
:buffer <file number or buffer number>

You can also split the buffers
:sbuffer <file>

To split vertically you can try
:vertical sbuffer <file name>


Buffer movement commands

:bnext
:bprevious
:blast
:sbnext

Tabbs in gvim

The Concept of tabs is also available in Gvim.

To edit a file in a tab, use the below command
:tabedit <file_name>

To create a new tab
:tab <file_name>

To cloase a tab we can use
:q
or
:tabclose


If your using a basi vi, where your mouse wont work, Use need to use the following commands to naviagiate between the tabs

:tablast
:tabfirst
:tabnext
:tabnext number
:tabprevious


Enjoy have a nice time learning..
 
Old 01-12-2015, 02:39 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 5,988
Blog Entries: 12

Rep: Reputation: 2045Reputation: 2045Reputation: 2045Reputation: 2045Reputation: 2045Reputation: 2045Reputation: 2045Reputation: 2045Reputation: 2045Reputation: 2045Reputation: 2045
I would definitely make this a blog entry versus a thread.

Good summary. Stylistically too many options for graphical versus not. Just stick with vi.

Also, some of the actual punctuations have been corrupted, do a proofread to see where there are some unintended smiley faces. To fix those you can put and the [noparse][/noparse] tags around those sections to make it not show smiley faces; or maybe in advanced edit mode check the box to say "disable smiley faces in text"

Last edited by rtmistler; 01-12-2015 at 02:42 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
LXer: TuxArena Linux Cheat Sheet LXer Syndicated Linux News 0 02-23-2014 11:40 PM
LXer: Unix Commands Cheat Sheet LXer Syndicated Linux News 0 07-16-2012 11:10 AM
LXer: Google Talk Cheat Sheet LXer Syndicated Linux News 0 01-18-2010 11:11 PM
LXer: My MySQL - Cheat Sheet LXer Syndicated Linux News 0 01-20-2006 10:01 AM
Linux command cheat sheet freddieB Linux - General 1 02-22-2002 11:50 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 10:01 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration