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

86 of 702 comments (clear)

  1. best one ever by shawn(at)fsu · · Score: 4, Funny

    :r! emacs /I partly kid I like Vi

    --
    500 dollar reward for tip(s) leading to the arrest of the person(s) who stole my sig.
    1. Re:best one ever by Anonymous Coward · · Score: 2, Funny

      /bin/bash: emacs: command not found

    2. Re:best one ever by smcameron · · Score: 3, Informative

      For all your vi/Emacs flamewar needs, try http://wordwarvi.sourceforge.net/ Full disclosure, I'm the author of that game.

    3. Re:best one ever by rpresser · · Score: 4, Funny

      $ sudo bash
      Password:
      # test -f /bin/emacs  && mv /bin/emacs /bin/emacs.NEVER
      # cat <<EOF >/bin/emacs
      #!/bin/bash
      echo emacs: command not found
      EOF
      # exit
      $ logout

  2. 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 Reality+Master+101 · · Score: 4, Informative

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

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

      --
      Sometimes it's best to just let stupid people be stupid.
    2. 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

    3. 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.
    4. 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.
    5. Re:Replacement by mrons · · Score: 2, Informative

      Also g/foo/s/bar/baz/g

      For all the lines that match foo, replace bar with baz

      With this form, the equivalent of %s/bar/baz/g is g/bar/s//baz/g

      Another useful :g command is g/foo/d to delete all lines with foo

    6. Re:Replacement by Abcd1234 · · Score: 2, Informative

      I much rather use Visual mode for this, as I hate having to remember line numbers. Just hit 'V', highlight the region, and then type ':s/FROM/TO/g'. Vim does the rest.

    7. Re:Replacement by isorox · · Score: 2, Informative

      # change foo to bar for all occurrences in the rest of
      # the file from where the cursor is :s/foo/bar/g

      No, that only works on the current line. perhaps :.,$s/foo/bar/g

    8. Re:Replacement by playerone · · Score: 2, Informative

      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/

      In vim (unsure if its new or in the original vi) a % means do this on all lines.

      eg: :%s/^/foo/

      places "foo" at the start of every line.

      --
      --Question Authority--
  3. Re::Sex by Anonymous Coward · · Score: 5, Funny

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

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

    I'll tell you when you're older.

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

    2. Re:Filter Lines by James+McGuigan · · Score: 2, Informative

      These are useful with prefixes: :r!pwd (read `pwd` into to document) :%!sort (sort all lines) :'$lt;'>!sort | uniq (sort last visual selection, notice the pipe)

      Another useful thing you can do is to bind a filter to a script, in your .vimrc, so you can yy pp a line, and get an instant commented debug line.

      map :.! ~/scripts/javascript-debug.pl==

      #!/usr/bin/perl
      while() {
              ($line = $_ ) =~ s/^\s+|\s+$//g;
              ($quote = $line) =~ s/'/\\'/g;
              print "console.log('DEBUG: $quote ', $line);\n";
      }

      Note: My normal script has half a dozen regexps in the middle, for stripping out semicolons, function declarations, return statements, such that what gets printed is almost always a valid code statememnt. However slashdot can't tell my regexps apart from line line-noise and kept complaining about too many junk characters.

      However its possible to write similar scripts for various languages, and for other simple tasks such as wrapping a for statement around an array variable.

  6. 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.
    1. Re:Couple off-hand by include($dysmas) · · Score: 2, Informative

      I/A - insert at start /end of line

      P/p - paste before / after cursor

      "*y / "*p - yank & paste to / from the windows clipboard

      vat/vit select around/inside a text block like html tags

      vi" select inside speech marks

      ciB correct inside curly brackets pair

      the list goes on, the point is to treat each key as an atom of a command, and learn how they can be combined.

      !python % - run current file through python

      ggVG= - reindent current buffer

      ^wv/s - split window

      ^6 - switch to last touched buffer

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

    1. Re:Vim tips by Amazing+Quantum+Man · · Score: 4, Informative

      :%s/\<\([Ss]\)iteCode\>/\1iteIdentifier/g

      --
      Fascism starts when the efficiency of the government becomes more important than the rights of the people.
    2. Re:Vim tips by TimJL · · Score: 3, Informative

      I'd like to pimp my own cheat sheet here: http://tjl2.com/sysadmin/vim-cheat-sheet.html I love Vim!

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

    :q

    --
    This guy's the limit!
    1. Re:very useful (especially for noobs) by Anonymous Coward · · Score: 4, Funny

      surely:

      :q!
      nano

  9. = and * by Lalakis · · Score: 3, Informative

    There are far too many "essential" commands in vim, but if I had to pick the two that make the most difference, I would pick * and =. * searches for the word under the cursor and = indents the selected text (most useful for programming).

  10. retab by DigitalCrackPipe · · Score: 4, Informative

    :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

  11. Re:Just using VIM by jDeepbeep · · Score: 3, Funny

    is itself a stupid trick. Emacs sucks too. Please welcome you to 21 century - use textedit!

    No thank you. edlin works just great for me.

    --
    Reply to That ||
  12. Need a way to un-highlight by bugnuts · · Score: 4, Informative

    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.

    1. Re:Need a way to un-highlight by Mad+Merlin · · Score: 2, Informative

      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 u but haven't found anything for vim.

      :noh

    2. Re:Need a way to un-highlight by Hatta · · Score: 2, Informative

      If you're pasting something, and don't want Vim's autoindent to screw up the formatting use: :set paste

      --
      Give me Classic Slashdot or give me death!
  13. % - jump between braces, ifdefs, etc by tippe · · Score: 2, Informative
    Also:

    v, V and ctrl-V (various visual selection modes for copy-paste)

    zf, zo, zc: fold creation, open, close (hides sections of your code)

    ~: (toggle case)

    u, ctrl-r: undo, redo

    "vimdiff" or "vim -d": (visual diff of two files)

  14. WTF?? by monkeySauce · · Score: 5, Funny

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

    1. Re:WTF?? by snspdaarf · · Score: 4, Funny

      Congratulations! You found the Easter egg!

      --
      Why, without your clothes, you're naked, Miss Dudley!
  15. delete a block of lines larger than the screen by prgrmr · · Score: 3, Informative

    move up to the top line of the block to be delete

    mm (sets a marker "m")

    move down to the last line in the block

    d`m (deletes to marker "m", and that's the grave below the tilde, not the back-quote)

    1. Re:delete a block of lines larger than the screen by James+McGuigan · · Score: 2, Informative

      Use Ctrl-v and you can select a column of chars. You can copy, delete and paste them.

      I have used this technique, along with vertical alignment of code, to quickly copy/paste a huge spreadsheet of expected results, into very exhaustive unit test code.

  16. Stupid vi tricks? by argent · · Score: 2, Funny

    VIM is a "stupid vi trick".

    Every time I update OS X no I have to dike it out and put the REAL "vi" back.

    1. Re:Stupid vi tricks? by mario_grgic · · Score: 4, Insightful

      Ah, add "set compatible" to your .vimrc file and you have 99% vi behavior.

      Why would you use plain vi, when vim is so much better?

      --
      As the island of our knowledge grows, so does the shore of our ignorance.
    2. 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.

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

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

  19. Re::Sex by Noksagt · · Score: 4, Informative

    :help sex

    :Sexplore[!] [dir]... Split&Explore directory of current file *:Sexplore*

    e.g. it give you a file exploring pane above the buffer you are currently using.

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

  21. Stack Overflow by JPLemme · · Score: 4, Insightful

    When did Slashdot become Stack Overflow?

  22. Re:Just using VIM by careysb · · Score: 2, Funny

    When I was your age I wrote thousands of lines of C code using EDLIN. (P.s. this was on a *nix box not DOS) (P.p.s Also walked to school 5 miles, up-hill, barefoot, in the snow, both ways)

  23. Re:Just using VIM by jaredmauch · · Score: 2, Insightful

    Yes, please tell me about your years spent on Ultrix, Altos and other boxes. You modern kidz and your GUI. The main reason why I use OSX is because I can ssh *and* run Microsoft office at the same time. If you don't understand syntax like

    run *kermit

    load * (Oh, the magical keys on the c64)

    and having to spend time at bps rates lower than 9600 when performing tasks, you clearly don't appreciate the speed increases over the years the same as some of us. I remember going from 300->1200->2400bps. Now I spent my time tracking IEEE HSSG for 100Gb/s interfaces, because I will likely have a need to bundle them.

    I get really sad when I see wasteful memory and cpu bloat. Perhaps these will become scarce resources again with the mobile trend, and people will do a better job watching their consumption. Oh well.

  24. Re:Just using VIM by Sancho · · Score: 4, Insightful

    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.

  25. Perhaps two of the weirdest: Ctrl-A and Ctrl-X by sdt · · Score: 2, Informative

    (in vim only), Ctrl-A and Ctrl-X find the next number on the line starting at the cursor, and then increment or decrement it respectively.

    Apart from being weird, these are surprisingly useful sometimes, e.g. toggling "#if 0" to "#if 1"...

  26. Especially useful for PERL by ickyb0d · · Score: 2, Informative

    To create a comment block:

    move cursor to the start of the comment block
    ma - creates a mark called "a"
    scroll down to the end of the comment block

    :'a,.s/^/#/ - to create comment block
    :'a,.s/^#// - to remove comment block

    basically puts # at the start of every line from mark a to your current cursor position. You can get creative and use this method for tons of things, indenting, substituting words within a given scope, comment blocks, etc.

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

  28. Re:Just using VIM by Bandman · · Score: 4, Insightful

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

  29. Some basic but useful commands by Maexxus · · Score: 2, Insightful

    Very basic commands, but the ones I use the most are:
    o - Open a new line in insert mode
    cw - Delete word and enter insert mode (change word)
    dw - Delete word
    d$/c$ - Delete/Change to end of line
    dd - Delete Line and Yank
    yy - Yank Line
    p - Paste

    These simple commands alone make Vi/Vim very fast for editing.

  30. Re:Just using VIM by Minozake · · Score: 4, Insightful

    The learning curve is worth it, though. If you need to edit text quite a bit, it makes sense to make it efficient.

    I personally recommend trying to learn Vim alongside with another editor, or using a GUI frontend like GVim.

    Replace Vim or GVim with any editor of your choice.

    --
    http://sourcemage.org/ - Have fun :)
  31. 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.

  32. I think I see a pattern in these stories by Xeth · · Score: 2, Funny

    ...and I've already gotten through half of my 20-page "(Useful) Stupid Emacs Tricks" post.

    --
    If your theory is different from practice, then your theory is wrong.
  33. 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
  34. 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."
  35. shelling out by onedeadeddie · · Score: 2, Informative

    :sh works well when you don't know root, but have sudo privs.

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

  37. Re:Here's a few good ones by James+McGuigan · · Score: 3, Informative

    Actually I fix this one within .bashrc, and create a wrapper function around vim, that detects if the file is editable or not, and asks if you want to prefix an sudo.

    argc () {
            count=0;
            for arg in "$@"; do
                    if [[ ! "$arg" =~ '-' ]]; then count=$(($count+1)); fi;
            done;
            echo $count;
    }

    vi () { if [[ `argc "$@"` > 1 ]]; then /usr/bin/vim $@;
                    elif [ $1 = '' ]; then /usr/bin/vim;
                    elif [ ! -f $1 ] || [ -w $1 ]; then /usr/bin/vim $@;
                    else
                            echo -n "File is readonly. Edit as root? (Y/n): "
                            read -n 1 yn; echo;
                            if [ "$yn" = 'n' ] || [ "$yn" = 'N' ];
                                then /usr/bin/vim $*;
                                else sudo /usr/bin/vim $*;
                            fi
                    fi
                }

  38. Reference card by YoungHack · · Score: 2, Informative
  39. Re:Just using VIM by michaelamdavies · · Score: 2, Funny

    Eeee, I used to dream of living in a hole in't ground. We used to live at bottom of lake, hand coding assembly language in hex, rotating bits with a hand crank and communicating it using signal flags or semaphore. And then sysadmins and punch card operators would come and chop us into little bits with paper tape readers and dance on our line printer reams. And you tell young people this today, and they just don't believe you

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

  41. regular expression groups by naveenkumar.s · · Score: 2, Informative

    When you want to move the words around in a line you can do something like this.

    hello world
    ~
    ~
    :%s/\(.*\) \(.*\)/\2 \1/

    world hello

  42. Re::Sex by Randle_Revar · · Score: 2, Funny

    support :sex education!

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

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

  44. Re:Esc ends commands, not starts them by mattack2 · · Score: 2, Informative

    If you use "your" to mean "you're", you're doing it (using the English language) incorrectly.

  45. 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:&#9476;&#9472;,trail:&#183;
    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.

  46. Re:Just using VIM by andrikos · · Score: 2, Informative
    Ctrl+V (Visual mode), select the lines I want, press : (which adds '<,'> automatically) and executing the command:
    :'<,'>s/^/#/
    Comments the lines in quite many languages

    For C I use this
    :'<,'>s+^+//+

  47. 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
  48. Re:Just using VIM by krenshala · · Score: 2, Insightful

    This reminds me of the not-quite-a-joke: A programmer will spend 30 days writing a script that saves him 30 seconds ... every time he uses it. Using vim (or emacs, for those that insist ;) is the same way; you spend time up front to learn how it works only to save truly stupendous amounts of time later when the "obscure commands and meta-keystrokes" you now know saves you minutes to hours per file.

    --

    krenshala

  49. Re:Just using VIM by geminidomino · · Score: 2, Funny

    * leaves himself a note to get cygwin installed on the home system

    How can I install that on Ubuntu?

  50. Re:Just using VIM by rrohbeck · · Score: 4, Funny

    * leaves himself a note to get cygwin installed on the home system

    How can I install that on Ubuntu?

    VMware.

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

  52. The keyboard is the same. by rdebath · · Score: 2, Insightful

    The keyboard hasn't changed in the 100 years and that is the main bottleneck. Vi is the most efficient keyboard interface to a text file I know of.

    Emacs is nicer to use at first and is just as efficient for very simple and very complex edits but it's poorer in the middle. The trade off (both ways) is what gives the nice "community spirit".

    The editors you mention don't even have the concept of complex edits and frequently require you to drag you hands away from the home keys, not just to the further reaches of the keyboard (like emacs) but right off the keyboard. This is the sort of thing that can break your concentration (ie WTF has happened to that ****ing mouse)

    BTW: Regex S&R is one of the simplest of the "complex edits"
     

  53. Clever deleting by Boawk · · Score: 2, Informative

    Everyone is aware of commands such as dw which will delete a word. Many are aware that the valid generalization of that command is d<movement>. Few people are aware that <movement> means pretty much anything which moves your cursor. For example, d/sometext<return>. Use your imagination; anything which moves your cursor.

  54. Getting copy/paste to work by cerberusss · · Score: 2, Informative

    Vim works wonderful together with X. Besides the graphical version (gvim), it's also possible to start vim in a terminal like xterm, konsole or gnome-terminal. With the right options, it's still aware of the X clipboard, the mouse, et cetera. However it's quite a pain in the ass to find out what's wrong when it doesn't work. Here's a guide for using and fixing it.

    vim has one default clipboard, but has an extra one under any a-z character. The two X clipboards are available too, but under a special character. To paste the X clipboards, paste either the + or * clipboard:

    This is the X clipboard for which you just select something with the mouse: [Escape]"+p

    The star buffer contains the X clipboard for which you selected with the mouse, then right-clicked and selected 'copy', or pressed a shortcut like CTRL-C or similar: [Escape]"*p

    If this doesn't work, check the following things:


    • Vim is started in compatibility mode. Solution: don't use 'vi' to start, use 'vim'. On RedHat and CentOS, it's not enough to start 'vim', use 'vimx'. If this program is not present, be sure to install the package 'vim-X11'.
    • Vim is not compiled with the right options. To check: 'vim --version | grep xterm', and the output must show '+xterm_clipboard'.
    • You used SSH to log into a remote machine and X11 is not forwarded. To check: start a simple X program like 'xclock'. Solution: start ssh with option '-X'.
    • You used SSH with -X and X11 is still not forwarded. To check whether the system administrator has configured the SSH daemon to deny X11 forwarding, add the -v option, and check whether the following line shows up and the request is not denied. If this is the problem, ask your sysadmin to add the line 'X11Forwarding true' to /etc/ssh/sshd_config.
          $ ssh -v -X remotemachine ...
          debug1: Requesting X11 forwarding with authentication spoofing. ...
    • You used SSH with -X and X11 is still not forwarded. Use the -v option as above, and check for the following line. If it appears, ask your sysadmin to install the xauth program.
          debug1: Remote: No xauth program; cannot forward with spoofing.
    --
    8 of 13 people found this answer helpful. Did you?
  55. Re:Just using VIM by Anonymous Coward · · Score: 2, Informative

    try MacVIM which is an excellent port

  56. Re:Just using VIM by WoLpH · · Score: 3, Informative

    It's quite easy

    1. ^V for block select
    2. Select the lines you want (10j for the next 10 lines)
    3. I for insert mode
    4. Type the # or whatever you want to prefix
    5. <esc>
  57. Re:Just using VIM by ppc_digger · · Score: 4, Funny

    He said an editor, not an operating system.

    --
    Of all major operating systems, UNIX is the only one originally meant for gaming.
  58. vim cheat sheet by zbharucha · · Score: 2, Informative
  59. Re:Just using VIM by Smeagel · · Score: 4, Insightful

    You do realize you can bind macros to keys in vi too? I've never understood this absurd argument from emacs people..

  60. Re:Just using VIM by DG · · Score: 2, Funny

    Young whippersnapper.

    You think *you* had it tough? We had to figure out all the fundamental constants of the universe and then tough off the Big Bang and wait for limbs and blood to **evolve** before we could paint on the cave walls.

    You "we inherited an inhabited universe" guys had it *so* easy compared to us. Did *you* ever work out the ideal value of Plank's Constant by trial end error? I don't think so.

    Meh. Kids today.

    DG

    --
    Want to learn about race cars? Read my Book
  61. Re:Just using VIM by Brassrat70s · · Score: 2, Insightful

    years ago i had a (fortunately) mild case of carpal-tunnel syndrome. After a cortisone shot, I stopped using emacs - no more CTRL-THIS and CTRL-THAT - and haven't looked back. The advantage of a mode-based editor is that you can get by with simple single keystrokes which are much less stressful to you hands.

  62. How about recording? by jivemonkey · · Score: 2, Informative

    In vim/gvim, I think the most useful thing I have ever used is the recording capability. It works great for editing HTML. If you have 10 lines that look very similar (such as a list of navigation links), start recording, edit the first line making sure that you end your statement with 'j' so that it moves to the next line, then finish recording and you can execute that recording for the next 9 lines.

    To start recording, type 'q' and then a number such as '1'. This will record your next keystrokes into memory bank 1. When you're done typing, just hit 'q' to finish recording.

    So, if you had

    <h1>Foo</h1>
    <h1>Foo</h1>
    <h1>Foo</h1>
    <h1>Foo</h1>

    and you wanted

    <h1>Foo Bar</h1>
    <h1>Foo Bar</h1>
    <h1>Foo Bar</h1>
    <h1>Foo Bar</h1>

    you would hit 'q1' to begin recording in bank 1, '^' to start at the beginning, 'l' (as in ell) 6 times to move six characters to the right, 'a' to insert after the last 'o' of "Foo", ' Bar' to insert the string, 'esc' 'j' to finish and move down one line, 'q' to finish recording, then '3@1' to execute the operation in bank 1 three times.

    I can't count the number of times I have used this functionality! Makes coding immensely easier.

    --
    Got a problem? Call a monkey!
  63. 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