Click to See Complete Forum and Search --> : vi help


nisim7
07-10-2000, 11:36 AM
I am new t programming and vi has literally thrown me through a loop. I have yet to have one program compiled correctly in Linux (including with gcc, vi, or emacs.)

Others have told me to use visual studio on a windows machine but I know that there is a reason that everyone is raving over vi and such.

What I am asking for is possibly some very easy, lay terms, help with any of these editors. when I open vi, what key do I press next, not just what do I do, but what key comes next.

Sorry to be a bother but I want to be happy on the Linux bandwagon because I never was relly happy on the Microsoft.

BTBGuy
07-10-2000, 12:10 PM
I just started with vi/vim a few weeks ago, there's an NHF on vi/vim here (http://www.linuxnewbie.org/nhf/intel/tools/vicc.html) but to get started (and I do mean get started, the stuff you can do with vi/vim is, in a word, amazing)
(a)ppend or (i)nsert will let you start typing. (type a or i)
depending on your terminal stuff, the arrow keys will move the cursor, but if not, press ESC and then h moves left, j moves down (it looks sorta like a down arrow...), k moves up, and l moves right.
Then press a or i to start typing again.
To delete a single character, press ESC (to make sure you're out of the append or insert mode) and press x. dd deletes an entire line.
When you're finished editing and stuff, save by pressing ESC (get out of append/insert), then type this- ":w filename" and to quit type ":q" or ":q!" (the exclamation mark forces it to quit if regular :q doesn't work)

Hope this helps!

One other thing you may want to try...
In Vim there's a tutorial, you get to it by typing (i think) :help tutor but if that doesn't work, type :help and scroll around some. There's something enclosed in | | that you have to move the cursor to, then press Ctrl-] to go there.

Good luck in the world of Vi/Vim!!!

toolie
07-10-2000, 02:20 PM
Here are just a few of the commands (read the tutorial for a better understanding).

In the insert mode (ie, type and it appears):
<esc> Change from insert mode to edit mode

In the edit mode:
h move the cursor left a line
j move the cursor down a space
k move the cursor up a space
l move the cursor right a space
0 (the number, not letter) move the cursor to the beginning of the line
$ move the cursor to the end of the line
cntrl-f move forward (down) a page
cntrl-b move backward (up) a page
a insert characters to the RIGHT of the cursor
i insert characters under the cursor
r replace a single character (overwriting existing char)
R replace characters (overwriting existing chars)
cw change word - replace up to the end of the word, then add
x erase the character under the cursor and shift the line left a space
dd delete line

J append the next line to the current line (move the line up and to the end)

:w write (save) the file
:w <filename> saveas filename
:wq write the file then quit
:q quit (as long as no changes took place)
:q! quit without saving changes

:<number> such as :11 -> go to line number
/<string> such as /11 or /new -> search for that string, case sensitive
n after searching, find the next occurance
N after searching, find the previous occurance

cntrl-g status (filename, linenumber, column)

:startline_number,endline_number s/stringtoreplace/replacementstring/g this confusing piece of work searches from startline_number to endline_number replacing all occurances of stringtoreplace with replacementstring. You can leave the g off the end, and it will only replace the first occurance per line. It looks like this:

:1,5s/there/this/g
That will search lines 1-5 and replace there with this every time it occurs.

:15,$s/this/that
This will search lines 15 to the end of the file and replace the first occurance of this with that.

Search is case-sensitive.

You can put a number in front of several commands (such as dd and x) and the command will execute that many times, like:

5x will delete 5 spaces
7dd will erase 7 lines

:help tutor this will run the tutorial (very helpful)

Any other questions, just ask (I hope I helped more than confused).

Carnel
07-10-2000, 03:28 PM
Okay, first thing I recommend you do. After typing Vi (or vim) type :h tutor (with the colon). Or from the command line, type vimtutor (I believe that's what it's called). It will help you a lot!

------------------
-- In order to survive in the real world, you have to be realistic.

Strike
07-11-2000, 03:10 AM
Let's add more vi coolness:

When not in any command mode
o - throws a carriage return and beings inserting on the next line
b - goes back a word
w - goes forward a word
(Note: b and w can be used in conjunction with c, d, and others to delete/change/whatever a certain number of words -

d7w - deletes the next 7 words
c4b - deletes the four previous words and puts you into insert mode)
} - moves you forward a paragraph and I think { moves you back one
% - matches scope with whatever scope operator you are on - ()[]{}
. - repeats the last command (this is one of the BEST features)
u - undo the last command (and then the one before it, and the one before it ...)
^r - (this is CTRL-R) redo the last undo (and the one before it, etc)
v - puts you into visual mode
ZZ - (capital Z's a must) = same as :wq, writes and quits
y - yank selected text (visual mode)
Y - yank complete lines containing selection (visual mode)
(y and Y can be used with numbers to do multiple-line yanks, I believe)
p - place the contents of the cut buffer (the cut buffer gets anything from yank (y,Y) delete (d,D) or erase (x), and maybe others), places right after current character)
P - same as p, but places on a new line and right in place

hmmm... that's all I want to get into for now... nah, let's do some colon commands

colon commands
:!<anything> - use ! to sort of invoke a shell. to list the contents of the current directory without leaving vim just do :!ls
:mak(e) - runs make in the current directory
:e <filename> - open <filename> for editing
:split [<filename>] - if a filename is given, will divide the window and place that file in the editing window, otherwise divides the window and opens up another instance of the current file
:new [<filename>] - pretty much the same as split, but without a filename will divide the window and open up a blank editor
:only - if you've divided the window a bunch, this one will take up the entire window and the rest go into buffers
:bn & :bp - next buffer or previous buffer
:cl :cn :cp - after running :make, :cl will list the errors you got (or, if none, just the compiler commands), :cn will go to the next error, :cp will go to the previous

With many buffers open
^W <up|down> - will go to the buffer above/below the current one (can use numbers to jump multiple buffers up/down
Cool feature - you can yank text from one buffer and then put it into another buffer

Okay, I think I'm done now

---edit---
fixed UBB flubs

[This message has been edited by Strike (edited 11 July 2000).]

Strike
07-11-2000, 09:32 AM
Let's add some more, shall we?

:hide - will hide the current buffer
:buffers - will show you a list of all the buffers
:b<number> - will bring buffer #<number> up in the current window
:ball - will bring up all the buffers

? - same as /, does searches, but this one does them backwards

:wall - write all, writes all buffers to disk
:qall - quit all, quit/close all buffers

I'll be back with more, maybe http://www.linuxnewbie.org/ubb/smile.gif

toolie
07-11-2000, 11:01 AM
DOH! I can't believe I forgot the . command. That one rocks. Then there is also

:read <filename> which will append filename to the current file

O (the letter), will insert a blank line ABOVE your current one, and will put you in insert mode

:n if you are editing a bunch of files, this will advance you to the next one

:N if you are editing a bunch of files, this will bring up the previous one

:sh or :shell this will drop you into a shell - very nice

:!! works just like :!, it executes ! as a shell command, which repeats the last :! command

http://www.linuxnewbie.org/ubb/smile.gif

Vi rocks.

Edit: Changed my <> tags to UBB friendly [] tags.

[This message has been edited by toolie (edited 11 July 2000).]

Strike
07-11-2000, 12:38 PM
Oh wow, I just found one of the coolest commands entirely by accident.

K - will bring up the manpage of whatever word you are on

hot damn, that's so cool

also, I forgot:

e and E will move you to the end of the next word or WORD (I never really learned the distinction though I know of a few cases where I know how they are different, but not why)

f|F [number] [character] - will go to the number'th occurence of character, with f you go forward, with F you go backward

this can be repeated with ; and repeated in the opposite direction with ,

oh, and ( and ) work just like { and } mentioned above, but it's sentences instead of paragraphs

kmj
07-11-2000, 12:38 PM
If you're ever using a crappy telnet program that doesn't refresh the screen when you want it to, z. will always refresh the screen for you.

* Finds the next occurrence of whatever word the cursor is on and...
# finds the previous.
[[ will take you to the previous { at the beginning of a line
]] will take you to the next one.
[] will take you to the previous } at the beginning of a line
][ will take you to the next one.

The above are very nice for navigating code.

So, your boss told you that he didn't like the variable name "frog", and that it should be changed to "toad", huh? But there are over a thousand instances of that word in amphibian.c! What are you going to do?
: % s/frog/toad/g
poof.

ex addressing (for use with search and replace):
% all lines
x,y from line x to line y
. current line
n line n, where n is a valid integer
$ last line of file
x-n n lines before x
x+n n lines after x
+n n lines after current line

s/pattern/string/ replace first occurrence only (on each line within address)
s/pattern/string/g replace all occurrences on each line within address
s/pattern/string/n replace nth occurrence on each line within address.

http://www.linuxnewbie.org/ubb/smile.gif



------------------
-Pacotron- a vi user through and through

The only reason I keep my DOS partition is so I can mount it like the b*tch that it is.

Strike
07-11-2000, 05:58 PM
You can also do the search and replace things within a highlighted block of text. Just go into visual mode (v), select the block of text, and then hit the colon (:). This will appear, but don't change it:

:'<,'>

that just says "within the visual range". So, add onto that

:'<,'>s/foo/bar/g

This will replace foo with bar anywhere in your visual range.

kmj
07-11-2000, 07:22 PM
Oh, cool! thanks. My experience with v-mode is limited. I tried it for a while, then sort of forgot about it.

------------------
-Pacotron- a vi user through and through

The only reason I keep my DOS partition is so I can mount it like the b*tch that it is.

Strike
07-11-2000, 11:26 PM
That does it, I'm saving this page http://www.linuxnewbie.org/ubb/smile.gif

Oh, and no problem, kmj - visual mode is my friend. It's easier than counting lines for multi-line yanks. Just tap v and then go up or down, Y, go to paste spot, P. It rules for copying multiple words that you don't want to count too, v, w or b a few times[/b], y, go to paste spot - p. Excellent stuff.

Well, nisim7, you got a very good start on vi usage now thanks to the people in this post http://www.linuxnewbie.org/ubb/wink.gif

[This message has been edited by Strike (edited 11 July 2000).]

nisim7
07-12-2000, 10:30 AM
Thank you all very much. This is so wonderful and more than I expected.

Nisim7

toolie
07-12-2000, 06:56 PM
More coolness (I forgot about it till I just had to use it 30 secs ago).

If you have a bunch of spaces you need to delete, do this:

:s/ //

Where the number of spaces from / -> / is the number of spaces you wanna erase.

kmj
07-12-2000, 08:40 PM
I usually do y/<pattern>, for the larger yanks.

------------------
-Pacotron- a vi user through and through

The only reason I keep my DOS partition is so I can mount it like the b*tch that it is.

wmHardRock
07-12-2000, 08:50 PM
To summarize the whole thing:
vi (and clones [especially Vim]) rule and have LOTS of commands.

wmHardRock

------------------
PoWeR DuO:
SlAcKWaRe & BlAcKBox

wmHardRock
07-12-2000, 09:28 PM
And is there a command to know which line we're on?

wmHardRock

------------------
PoWeR DuO:
SlAcKWaRe & BlAcKBox

toolie
07-12-2000, 10:06 PM
cntrl-g will give line number, percentage you are through the file, filename, etc...

EyesWideOpen
07-13-2000, 12:07 AM
Originally posted by wmHardRock:
And is there a command to know which line we're on?

wmHardRock



vim has a command that you put into the .vimrc file in the ~/ directory that will display a ruler with this info at the bottom all the time. I'll edit this when I find out.

dante_d
07-29-2000, 07:59 AM
You can set up your vim environment by editing .vimrc (.exrc for vi, if it's not in your home directory you can create one).

Here's my simple one for example:

set nu
set ts=4
set ai
set showmode


The above will:
display linenumbers
set your tabstop to 4 (default is 8)
use autoindent
show the current mode you're in (insert, append, etc.) Showmode is a default in vim, I believe.

There's a lot of other options, of course.
All of the :set commands can also be issued at the command line if you don't want them to run automatically when you start vi.

I'm getting addicted to vi.

[This message has been edited by dante_d (edited 29 July 2000).]

Strike
07-29-2000, 11:25 PM
I also like just doing

:set <TAB>

hitting <TAB> over and over to see all of the options. And then when I wonder what one means (take for example, :set backup), I just do:

:h backup

bada-bing! there it is!

EyesWideOpen
07-31-2000, 08:55 AM
Originally posted by wmHardRock:
And is there a command to know which line we're on?

wmHardRock



set ruler will show the cursor position all the time

http://www.linuxnewbie.org/ubb/biggrin.gif

[This message has been edited by EyesWideOpen (edited 31 July 2000).]

wmHardRock
11-14-2000, 11:20 AM
Just bringing this up if someone needs it or if someone feels like adding anything.

:set ignorecase => Ignore the case when searching

:set nocp => Don't know what it does, but I like it better when it's there

:set ws incsearch => Incremental search

Strike
11-14-2000, 11:33 AM
Originally posted by wmHardRock:
:set nocp => Don't know what it does, but I like it better when it's there
That means set no-compatible, and trust me you want that on. vim wouldn't be vim without it (it'd be [b]vi[/i]).

wmHardRock
11-15-2000, 11:37 AM
I'm writing a reference card in french. Anyone interested in adding stuff, please send me an email at wmhardrock@hotmail.com

wmHardRock

Unruly
11-15-2000, 12:02 PM
This is so cool http://www.linuxnewbie.org/ubb/smile.gif I have just started to learn VI(M) and basically, I was mashing keys until something happened that I could repeat (ie insert, quit, save quit)

doh, it's ESC!

http://www.linuxnewbie.org/ubb/redface.gif

:wq

------------------
Nathan
Q: How many existentialists does it take to screw in a lightbulb?
A: Two. One to screw it in and one to observe how the lightbulb itself symbolizes a single incandescent beacon of subjective reality in a netherworld of endless absurdity reaching out toward a maudlin cosmos of nothingness.

wmHardRock
11-15-2000, 12:29 PM
You can see the beginning of my work at: www.chez.com/wmhardrock/vicard.txt (http://www.chez.com/wmhardrock/vicard.txt)

kmj
11-15-2000, 12:34 PM
Here's the substitue cheat-sheet which I had on my wall. I don't need it anymore:

to substitute:
: RANGE s/PATTERN/NEWSTRING/FLAGS

RANGE
% all lines
. current line
n line n of file
$ last line of file
x-n n lines before line x of file
x+n n lines after line x
+n n lines after current

RANGE can be any of above or you can define the limits of the range with x,y. For example (".,$": current line to end; ".,+5": this line and the next 5; etc.) RANGE can also be defined with visual mode, select the lines you want and just hit ':', you'll see the range (which looks something like "'<,'>") already there.

PATTERN
has a bunch of stuff, not on my cheat sheet, and I'm too lazy to pull it out of memory. You can use \( and \) to remember things, and \1 (for the first \2 for second, etc) to call back the remembered matches. Can use \t, and other stuff here.

NEW STRING
just a normal string to replaced the matched pattern, except that it may include \1, \2, etc. to insert remembered parts of the patter.

FLAGS
No flag implies replacing only the first match in a line.
'g' means replace all occurances on line
n (where n is a number) does something, but I'm not sure what... http://www.linuxnewbie.org/ubb/smile.gif weird.

So to replace all occurances of a word in a file just do : % s/word/newword/g

kmj
11-15-2000, 03:31 PM
This is useful to add to your .vimrc if you program alot

map <tab> 0i<tab><esc>

It basically lets you tab lines over while you're in command mode; it doesn't hurt anything since tab typically isn't used in command mode. The only thing is that you'll be placed at the beginning of the line when you do it. I don't know how to bookmark a position in a line. (though use can use m and ' to bookmark lines in a file.)

knute
11-15-2000, 03:49 PM
I've been using vim for at least 18 months and haven't really had time to try and decipher the help page that gets brought up w/<F1>......

This has been really helpful... Thanks...

I foresee an NHF coming soon on this topic. http://www.linuxnewbie.org/ubb/smile.gif

------------------
Knute
Email: knuteh@yahoo.com
ICQ: 53979509
GAIM: knutehall

kmj
11-15-2000, 05:23 PM
Originally posted by knute:
I've been using vim for at least 18 months and haven't really had time to try and decipher the help page that gets brought up w/<F1>......

This has been really helpful... Thanks...

I foresee an NHF coming soon on this topic. http://www.linuxnewbie.org/ubb/smile.gif


There's actually a couple of them already,

F1 isn't of great use, but :h (key) is. If you wanna know how something works, just type :h keyword; that'll bring you directly to it if it exists. You can also find out need stuff just by choosing a random key and looking up help on it. For example :h ~ or :h f. I like to browse randomly through the help just to pick neat things up, like info on features they plan to add, and tips. Neat stuff. Course, I don't exactly do this for sport, just when I need a quick break at work or somethign.