Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

So you need a perl editor. vi/vim (vi-improved) has its advantages. You will find it on virtually any *nix and you can get it for Win32. I syntax higlights perl, C and everything else pretty well. And it is simple to get the basics. So let's begin. We only have 15 minutes, and time is a wastin ;-)

To edit a file with vi/vim do:

vi file.txt vim file.txt

vim is better and on many systems if you type vi you actually get vim. Use vim if you possibly can rather than plain old vi. If you did that let's go on..... Vi is a tie died hippy modal editor possibly developed by guys who did too much LSD. But once you get you head around the wierdness it works well. It has 4 MODES.

  • Nav.
  • Match.
  • Colon.
  • Insert(Edit).

You are only in one MODE at a time. When you are in a MODE what you type causes different stuff to happen. Escape (ESC at top left of keyboard) will get you out of whatever mode you are in. You will learn to love ESC. Press it lots if needed. It does respond to lots of stabs!!! You will end up back in nav mode which is where you begin. So without further ado let's begin....

NAV MODE

Default is nav mode. This is where you start and where you end up with lots of ESC ESC ESC if you get lost. Arrow keys work as expected. In short h j k l move the cursor as well. Direction not entirely intuitive but you get used to it. Try it. You need to be able to feel it rather than have me say this key moves you this way.

Almost all vi commands will take a prefix integer argument. For example typing 12j is the same as typing jjjjjjjjjjjj. In this case you will execute 12 j commands, each one of which moves the cursor down one line, so you will jump down 12 lines. Useful when you mean it, potentially painful if you don't. Examples 5h - left 5 chars, 10j - down 10 lines, 3k - up 3 lines, 7l - right 7 chars. We will use NN to represent an integer argument to a command.

gg goes to top of file. NNgg goes to line NN. NN means an integer +OK? G goes to bottom of file dd deletes current line under cursor. NNdd deletes NN lines yy 'yanks' ie copies the current line and NNyy yanks NN lines. p pastes the current buffer at the end of the line the cursor is +on. Both dd and yy fill the buffer. ^ goes to begining of line like REs. Literal ^ not control BTW $ goes to end of line like REs u undo, NNu undo last NN changes. Ctrl+r redo, NNCtrl+r redo last NN undo-s x deletes char under cursor. NNx deletes NN chars to RHS of curso +r. r type r and the next char you type will replace the char under t +he cursor. handy for s/'/"/ and the like. J Joins the cursor line to the line below ie remove \n from cur l +ine. Note j is nav J is join..... Note Backspace will more you backwards (ie navigates but no deletion i +n nav mode) but DEL deletes as normal (ie no navigation stuff disappears). GOK.

MATCH MODE

Type a / or a ? and you enter match mode. You will see either a / or a ? at the bottom left of your editor window. Then type almost any perl RE and hit enter. Note there is no trailing / or ? required. The cursor will move to the first match. Hit n to go to the next or NNn to go the the NNth match moving forward.

Type a ? and then almost any perl RE and hit enter - works the same as / but heads towards top of file, so n gets you the match before....

As always ESC gets you back to nav mode.

COLON MODE

Type SHIFT+: to enter colon mode. That is shift and the colon key together. Hit ESC key to exit colon mode and get back to nav. Colon appears at bottom left of window. You have 3 main chars you can type (w q !)

:w writes file :w! writes file even if read only :q quit :q! quit and don't mess with me. ie if I have made changes ignore them and exit with no write (good for f ups) :wq write quit (usually what you want) :wq! write quit don't question my wisdom! There are also lots of neatish widgets, here's three :shell gives you a shell to do stuff. type exit to return to vi. :!command gives you a shell immediately below colon. ie !./script while you are editing 'script' it will exec +it. hit enter to return to vi as prompted, else type more com +mands..... :%perldo s/this/that/ execs that perl s///

INSERT MODE

You have 2 chars a and i and the shift key adds A and I. From nav mode typing the following enters insert mode at the following position relative to the cursor:

a Insert after the current cursor pos A Insert after the end of current line i Insert before the current cursor pos I Insert at the begining of the current line

In insert mode when you type 'stuff' it appears at the cursor. Arrows work as normal for navigation if needed. hjkl of course don't navigate you they just type letters. To use the undo key 'u' you need to hit ESC to escape into nav mode, then type undo which will undo all your insert mode edits. If you need to delete just a bit use arrows, backspace and delete as normal.

There is a lot more power than what I have shown you but if you take the time to learn those 20 odd keystrokes you will be able to use vi and perhaps learn more.....happy beeping.

Updates

Added J as suggested by dragonchild and %perldo s/// as suggested by Caron. Recommended vim explicitly per Abigail-II halley made it read better.


In reply to Learn vi/vim in 50 lines and 15 minutes by tachyon

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others chanting in the Monastery: (6)
    As of 2014-12-22 03:14 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (110 votes), past polls