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

15 of 702 comments (clear)

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

  2. 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.
  3. = 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).

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

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

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

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

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

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

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

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