Stories
Slash Boxes
Comments

News for nerds, stuff that matters

Slashdot Log In

Log In

Create Account  |  Retrieve Password

(Useful) Stupid Vim Tricks?

Posted by timothy on Thu Nov 06, 2008 04:35 PM
from the make-it-roll-over dept.
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?"
+ -
story

Related Stories

[+] (Useful) Stupid Unix Tricks? 2362 comments

So the other day I messaged another admin from the console using the regular old 'write' command (as I've been doing for over 10 years). To my surprise he didn't know how to respond back to me (he had to call me on the phone) and had never even known you could do that. That got me thinking that there's probably lots of things like that, and likely things I've never heard of. What sorts of things do you take for granted as a natural part of Unix that other people are surprised at?

[+] News: (Stupid) Useful Emacs Tricks? 412 comments
Count Fenring writes "Since the Vi version of this question was both interesting and popular, let's hear from the other end of the spectrum. What are your favorite tricks, macros, extensions, and techniques for any of the various Emacs? Myself, I like 'M-x dunnet' ;-)"
[+] (Useful) Stupid Regex Tricks? 516 comments
careysb writes to mention that in the same vein as '*nix tricks' and 'VIM tricks', it would be nice to see one on regular expressions and the programs that use them. What amazingly cool tricks have people discovered with respect to regular expressions in everyday life as a developer or power user?"
[+] (Useful) Stupid BlackBerry Tricks? 238 comments
Wolfger writes "Continuing the recent (useful) stupid theme: I've recently become a BlackBerry user, and I'm in love with the obvious(?) tricks, such as installing MidpSSH to access my home box remotely. But I'd like to know what more experienced Crackberry addicts can share."
This discussion has been archived. No new comments can be posted.
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More
Loading... please wait.
  • by shawn(at)fsu (447153) on Thursday November 06 2008, @04:38PM (#25666113) Homepage

    :r! emacs /I partly kid I like Vi

        • by tchuladdiass (174342) on Thursday November 06 2008, @05:27PM (#25666839) Homepage

          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).

          • Re:Just using VIM (Score:5, Informative)

            by el momia (1400135) on Thursday November 06 2008, @06:06PM (#25667363)
            :%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.
          • by Bandman (86149) on Thursday November 06 2008, @06:10PM (#25667439) Homepage

            Amen. Know vi, and know the bourne shell, and you're good on close to 100% of the unix machines you'll ever touch.

        • by tolan-b (230077) on Thursday November 06 2008, @05:28PM (#25666851)

          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).

        • by Sancho (17056) * on Thursday November 06 2008, @06:02PM (#25667289) Homepage

          I use Vim primarily because I can perform almost any task without moving my fingers from the standard typing position. I certainly feel much more efficient being able to (for example) use j/k/l/h for movement than moving my hand over to the arrow keys, or worst, to the mouse. That's one of many tasks which simply becomes natural over time (in fact, when I'm typing in a web form, I frequently find myself trying to use Vi shortcuts) and which really make things go more smoothly for me. Even when I'm on a full desktop, I prefer using Vim. I only wish the OS X port of gvim worked better.

          The reason that I don't use EMACS is because of the finger gymnastics you have to perform for even the simplest of tasks. Of course, one could complain similarly about Vi--having to switch to command mode is something that gets just about every single newbie.

          • by eln (21727) on Thursday November 06 2008, @05:47PM (#25667113) Homepage

            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.

  • Filter Lines (Score:5, Interesting)

    by saberworks (267163) on Thursday November 06 2008, @04:48PM (#25666243) Homepage

    Use visual mode (shift-v) to highlight lines, then shell out to external programs to filter them, such as perltidy. To do that, with lines highlighted, type !perltidy (assuming you have it on your machine). This lets you filter specific lines instead of the whole file.

  • Not horribly exciting ones, but useful:

    xp - reverse next two characters
    dL - Delete to end of page, in other words, everything visible.
    C - Often overlooked: chop off end of line and go into insert mode.

  • Vim tips (Score:5, Informative)

    by icsEater (1093717) on Thursday November 06 2008, @04:50PM (#25666271)
    Why bother asking slashdot when all the best Vim tips have been collected and compiled? http://vim.wikia.com/wiki/Best_Vim_Tips [wikia.com]
  • by gEvil (beta) (945888) on Thursday November 06 2008, @04:51PM (#25666303)
    :q
  • retab (Score:5, Informative)

    by DigitalCrackPipe (626884) on Thursday November 06 2008, @04:54PM (#25666337)
    :ret over highlighted text will reformat using the tabbing rules set up in your .vimrc files. Quite handy when you have legacy code and new code mixed together leaving a big mess when opened in a viewer with different settings.
    And, to remove the ^M from files that came from windows:
    :se ff=unix
  • by bugnuts (94678) on Thursday November 06 2008, @04:58PM (#25666385) Journal

    Am I just a vim noob? After doing a search and loving the nice highlighting, is there a way to unhighlight the search term without doing a "/lkasjdfkjdfdf"? In less(1), you'd hit <esc>u but haven't found anything for vim.

    The tricks I use in vi/vim are mostly the arcane flags.

    :set nows

    will not search past the top or bottom.

    :set sw=4

    will make a nice indentation shiftwidth, especially for using the indent command (>). Works great for programming, especially with autoindent (:set ai). But when programming with autoindent, you often need to unindent one shiftwidth... do that by typing control-D at the beginning of the line. You can go to the very beginning of an autoindented line with 0 control-D.

    :set list
    :set nolist

    will turn on/off hidden characters, and show end of lines. Great for finding tabs or spaces at the end of a line.

    :set nu

    will turn on line numbering.

    Of course, if you want actual line numbers in your file, in *nix you'd use
    :%!cat -n

    %

    when pressed over a parenthesis, finds the matching parenthesis or brackets

    Now, I want someone to write a lisp interpreter based in vi macros. That way we can port emacs to vi.

  • WTF?? (Score:5, Funny)

    by monkeySauce (562927) on Thursday November 06 2008, @05:03PM (#25666473) Journal

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

  • Stack Overflow (Score:5, Insightful)

    by JPLemme (106723) on Thursday November 06 2008, @05:53PM (#25667185)

    When did Slashdot become Stack Overflow?

    • Also, you can do use "ma" to mark the beginning line, "mb" to mark the ending line, and then:

      :'a,'bs/FROM/TO/g

      • Re:Replacement (Score:5, Informative)

        by pluther (647209) <{pluther} {at} {usa.net}> on Thursday November 06 2008, @05:46PM (#25667095) Homepage

        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.

      • Re:Replacement (Score:5, Informative)

        by iggya (1401047) on Thursday November 06 2008, @05:03PM (#25666479)

        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