Slashdot Mirror


(Useful) Stupid Vim Tricks?

haroldag writes "I thoroughly enjoyed the recent post about Unix tricks, so I ask Slashdot vim users, what's out there? :Sex, :b#, marks, ctags. Any tricks worth sharing?"

17 of 702 comments (clear)

  1. Re::Sex by Anonymous Coward · · Score: 5, Funny

    You're asking on the wrong site. No one on Slashdot has ever tried it.

  2. Re::Sex by TrekkieTechie · · Score: 5, Funny

    I'll tell you when you're older.

  3. Vim tips by icsEater · · Score: 5, Informative

    Why bother asking slashdot when all the best Vim tips have been collected and compiled? http://vim.wikia.com/wiki/Best_Vim_Tips

  4. very useful (especially for noobs) by gEvil+(beta) · · Score: 5, Funny

    :q

    --
    This guy's the limit!
  5. WTF?? by monkeySauce · · Score: 5, Funny

    I typed :Sex and it opened up a HUGE list of folders choc full of porn!

  6. Re:Replacement by iggya · · Score: 5, Informative

    One of vi's best features is the '.' command to repeat what you last did. You can do 'dd' to delete a line, then press '.' (dot) to do it again. Or '100.' to do it 100 times. Typing in numbers before a command repeats the command. Typing in '100ihello[esc]' will insert 'hello' 100 times. Then typing dot will give you 100 more.

    On a modern vi you can press up-arrow after pressing colon to get your previous colon command back for editing.

    Some examples of changing things on various lines:

    # add 'gronk' to the end of every line
    # 1 is line 1, $ is the last line
    :1,$ s/$/gronk/
    # put 'bing' at the start of lines 5-10
    :5,10 s/^/bing/
    # change foo to bar for all occurrences in the rest of
    # the file from where the cursor is
    :s/foo/bar/g

  7. Re:Just using VIM by tchuladdiass · · Score: 5, Insightful

    If you are on a single box, that is fine. But when you have to admin about 500 servers, spread out across the country, and sometimes over a dial-up link, you often don't have a graphical environment available. Even on the local network, I often ssh from one box to the next, and forget to forward my X11 connections. Since vi is always available, that is what I use.

    The other thing is that I appreciate having only minimal hand movement to get around a file and make changes. Much like people used to love the Word Star diamond, the same thing with vi's ctrl-f, ctrl-b, h, j, k, l, etc. And since I've been using it for about 20 years, these commands are second nature to me. Not to mention the search/replace supporting regular expressions (something a lot of gui word processors don't have).

  8. Re:Just using VIM by tolan-b · · Score: 5, Insightful

    Try using most GUI editors on a remote server over ssh. Kate may be an exception with KDE's nice network abstraction I don't know (I use Gnome), but to be honest for me the main utility of vi is that I know it's going to be there in any Linux enviroment (and I suspect Unix in general).

  9. Re:Replacement by pluther · · Score: 5, Informative

    Very cool. I didn't know how to mark a range like that before.

    And, while we're having fun with search and replace, ^ will match the beginning of a line, so if you mark as above, and then change the command to: :'a,'bs/^/#/

    you will have commented out a section of your code without having to insert a comment character independently on each line.
    Reverse it with: :'a,'bs/^#//

    to remove the comments.

    Also, you don't have to use the / command as a separator. Anything typed after s will become the separator, so if you want to, say, change all your Windows paths to Unix paths, instead of starting with: :%s/\\/\//g

    which, while undeniably cool, can be more easily written as: :%s;\\;/;g

    which is a little easier to read.

    Two other interesting bits:

    u all by itself will undo the last command. Handy when you're testing your commands before posting them to Slashdot.

    Also, Slashdot's editor will remove the newlines before any line that starts with a :
    In my examples, I put each command on it's own line, but Slashdot keeps appending them to the previous line. Weird.

    --
    If the masses can keep you down, you're not the Ubermensch.
  10. Re:Just using VIM by eln · · Score: 5, Funny

    Young whippersnappers. When I was "at a young age", it was called vi, and it didn't have any of this fruity syntax highlighting, and if you wanted to navigate around a document you had to use h,j,k,l, not those hand-holding arrow keys.

    Remember the old dig at emacs, "Eight Megs and Constantly Swapping?" Well back then, an 8 MB program actually did mean constant swapping!

    I've been in this business for too damn long.

  11. Re:Just using VIM by el+momia · · Score: 5, Informative

    :%s/foo/bar/g go through all the file and replace foo by bar :12,20s/foo/bar/ from line 12 to 20 replace foo for bar :s/foo/bar/g in the current line replace foo for bar the g after the last / means to replace all the occurrences of foo vby bar and not only the first one.

  12. Re:Stupid vi tricks? by Anonymous Coward · · Score: 5, Funny

    Stupid vi tricks? (Score:2)
    by argent (18001)

    I believe you and your fancy VIM are on this man's lawn.

  13. Re:Just using VIM by Anonymous Coward · · Score: 5, Funny

    Not really a vim trick, but I always liked to create a file in the / directory named README. The file just contained one line: "ERROR: can not open file README" When someone cats it, they get the error message. Pretty cute until they vi it and figure it out.

  14. Re::Sex by pimpimpim · · Score: 5, Funny
    it give you a file exploring pane above the buffer you are currently using.

    If only my parents had told me earlier.

    --
    molmod.com - computing tips from a molecular modeling
  15. Re:Just using VIM by Constantine+XVI · · Score: 5, Interesting

    main utility of vi is that I know it's going to be there in any Linux enviroment (and I suspect Unix in general).

    vi is part of the Single UNIX Specification, so anything passing itself off as UNIX must include vi. Even without the spec, it's much, much more universal than emacs, and more powerful than pico/nano.

    --
    "I think an etch-a-sketch with an ethernet port would beat IE7 in web standards compliance."
  16. Re:Just using VIM by Adam+Hazzlebank · · Score: 5, Interesting

    The one I find really useful is .,+20s/foo/bar/g

    Replace all occurrences in the next 20 lines from the current line only. Great when your editing code and you've realised you used the wrong variable name in that method for example.

  17. Re:Just using VIM by Anonymous Coward · · Score: 5, Funny

    How else am I supposed to get anything done on Windows?