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?"

14 of 702 comments (clear)

  1. Replacement by lunchlady55 · · Score: 2, Interesting

    %s/FROM/TO/g

    replaces every instance of FROM to TO in the document.

    % = every line. Drop that to just affect the current line
    g = every instance within the line. Drop that to change only the first occurrence in the line.

    also - use CTRL-v CTRL-m to get a newline - will look like ^M but match newlines.

    1. Re:Replacement by MrKaos · · Score: 3, Interesting

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

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

      And if you add a c (confirm) to the end

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

      you will get a Y/N to replace that instance or not, in case you don't want to replace every occurrence. if you search like this :'a,'b g/FINDME/ s/FROM/TO/gc

      vi will ask for confirmation to replace FROM to TO only on line between a and b markers on lines with the string FINDME on it.

      :.,$ g/FINDME/p will search from your current cursor position (.) to the end of the document ($) and get /regular expression/ print (i.e grep) inside of vi.

      456G

      go to the 456 line (G for the last line)

      These are a few of my favourite things. Vi plugin for Eclipse and Visual Studio actually makes them have a worthwhile editor, I couldn't imagine not having all the effort I invested into using vi available in some of the "editors" available today.

      --
      My ism, it's full of beliefs.
  2. Filter Lines by saberworks · · Score: 4, Interesting

    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.

    1. Re:Filter Lines by Jameson+Burt · · Score: 2, Interesting

      In vim, I often enter
      :set paste
      then highlight text from a browser
      and paste that text (middle mouse button)
      into my vim session.
      The "set paste" prevents the lines from indenting further and further to the right.

      After pasting, many lines are too long.
      In particular, a whole page gets pasted as one line.
      So, I enter
      :1,$!fmt
      or like the author (shift-v or ctrl-v) then

            !fmt

  3. Couple off-hand by Reality+Master+101 · · Score: 4, Interesting

    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.

    --
    Sometimes it's best to just let stupid people be stupid.
  4. Viewing hexdumps of binary files in vim. by CheeseMonger · · Score: 0, Interesting

    First edit the file in binary mode
    vim -b datafile
    Now convert the file to a hex dump with xxd
    :%!xxd
    Well, I thought it was cool.

    --
    CheeseMonger
  5. Most time-saving features by Anonymous Coward · · Score: 1, Interesting

    1. Tags. No learning curve. Generate tags with !ctags -R *.c *.h (if you are looking at a C code base). Ctrl-] on any function name, structure name/field, etc. to jump to the declaration point. Ctrl-T to come back. Tag stack is maintained. Then, :ta to jump to the tag. Auto completion for the tag name makes it a breeze. Ctrl-], Ctrl-T, and :ta are enough to save 50% of your time.

    2. Ctrl-P and Ctrl-N for very smart auto-completion can save another 30-50% of your time.

    3. Record a sequence of key strokes and replay them. Hit 'q', , 'q', , 'q' . Then, hit @f to repeat those actions. @@ to repeat the previous @ command. So one can do: @f, then, @@, @@, wherever they want to apply the changes.

    4. Hitting 'gf' on a header file will take you to the header file (not to your girlfriend). Hit Ctrl-O to come back.

    5. Use j,k,l,; to move up, down, left, and right. Learn touch typing.

  6. 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."
  7. Hi, I'm Kerrigan and I'm a vim addict.. by Kerrigann · · Score: 2, Interesting

    I've used vim for years... to the point that it is forever ingrained in the way I think and code. I can't even think of the last thing I edited *not* in vim (besides this post :). I consider myself to be *kinda okay* at using it...

    Here are three of my *cannot live without* vim features...

    1. Highlight text with visual mode and 'gq' to format it according to the currently set formatting rules. I know full-fledged IDEs have this feature, but I can't think of anything outside of vim or emacs that can do this from a thousand miles away in a terminal.

    2. "smarttab" mode and the 'retab' command. I've worked in lots of places with very disparate IDEs and environments and everyone seems to produce code with slightly different tabstops and tab consistency. 'smarttab' and 'expandtab' tell vim to output spaces instead of tabs for consistency in viewing across editors, but to treat consecutive spaces as virtual tabs for the purposes of deleting or selecting. 'retab' changes tabs across the entire file to match the currently set convention. *Very Useful* for editing python :)

    3. This is the biggest one... macro recording and programmatic repeat. 'q' starts recording a keystroke macro, which thanks to vim can be something as complicated as... "go to start of line, replace text up to first ( with blah, go to second ',' and capitalize the first letter of that word" etc... Then, you can either use the :global command to search a file for lines matching a pattern and execute that macro on each line, or even easier, use V (visual line select mode) to select groups of lines, then execute the macro on all of those, or a subset of those with global. I always do this via the :normal command, which allows you to execute a keystroke based command through command-line mode. So, 'qa^f(cbfoobarf)a const' would replace a function call name with 'foobar' and add a const to the declaration. Select a group of lines with V, then type :normal @a to call the macro on every line. I think there might be a better way to call macros from command-line mode, but that's the only one that I know.

    I have actually whined and cried and begged to put vim on everything larger than a toaster where I work so that I don't go crazy. Vim has saved me countless hours of programming time over the years, and I highly recommend trying to learn some of its deeper features.

    Plus, all the other editors I use have this weird habit of putting ':wq's and 'dd's in my text...

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

  9. Re:Whitespace + Searching by kangasloth · · Score: 2, Interesting

    +1 to visually distinguishable tabs. I still want a bit "whiter" whitespace though, something that settles a little more easily into the background. I've lately settled on the following, in combination with DejaVu Sans Mono. Unicode support mileage with other fonts varies.

    if &encoding =~ "utf-8"
    set list listchars=tab:┄─,trail:·
    else
    set list listchars=tab:._,trail:-
    endif

    ... not that I can actually show what those look like since it appears that slashcode doesn't approve of box drawing characters. At least not "BOX DRAWINGS LIGHT TRIPLE DASH HORIZONTAL" and "BOX DRAWINGS LIGHT HORIZONTAL". You'll have to decode those HTML entities yourself, oh intrepid reader. I could probably get "MIDDLE DOT" through, but I guess I'm just not that committed.

  10. Re:Just using VIM by petard · · Score: 3, Interesting

    Sigh. I'm sure I'm not the only one here... I distinctly remember purchasing my first 9600bps modem. (A real Hayes, no less! I sent them a large manilla SASE and they shipped me the AT command manual for no charge.) I spent a few months mowing every lawn I could to raise the funds for it. Exactly a week after I got it installed and found a couple local BBSes I could connect to at 9600, Hayes shipped the very first 14400bps modem.

    Apart from the nature and amount of labor involved in raising funds, that's been a pattern for so many equipment purchases since. That was the very first time I bought something so close to the release of the new shiny, though :-)

    --
    .sig: file not found
  11. Re:Just using VIM by dutchd00d · · Score: 2, Interesting

    By the way, you can use any character instead of the '/'. Handy for replacing path names in a file:

    :%s#/bin/ls#/usr/bin/ls#g

  12. ClownColors!!!! by tempest69 · · Score: 2, Interesting

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

    Because..drum roll.. Vim has a syntax file for your favorite language..

    You can customize if for your coding style... especially for things that you make wiked mistakes on..
    for example:

    if (x=y){do some code;}

    any c programmer will see the mistake pretty quick.
    but once it's in thousands of lines of code,, its a bear to find.
    with clown colors you set the color of the = to bright purple, and the parens to yellow. set == to orange.. so that your eye catches a painful clash when you type something stupid..

    for the common typos that I make.. I set it to INVERSE... so retrun x; get fixed before get to the next line.

    some of the common variable names I use get colorized so that I can recognize the specific variables...

    So a common for loop that you code without blinking has a "color signature" that lets you know it's your normal.
    fantastic crutch..

    Storm