More vim tricks and vim screencasts

I have been coming across quite a few incredible vim commands and shortcuts recently. Many are related to programming Python, which I am collecting and working into a post. For now, I wanted to mention a few unrelated items. This screencast series by Justin Lilly over at ShowMeDo was the source for most of these. The videos are well-done, short, and useful. Check them out!

  • %: Complements and file reference
    This operator serves two useful roles. When in normal mode, if your cursor is over one of a pair of “()”, “{}”, or “[]“, pressing “%” will move your cursor to the complement. This is handy for jumping to the end of function definitions, list comprehensions, and the like.

    When in command mode (press Esc then “:”), “%” acts as a reference to your current file. One use of this is to add a listing of your current directory to the file. Open a file, add some text, save it, go to command mode, and enter:

    1
    !ls >> %

    You will be shown the output, press Enter to continue. Then vim will alert you that the file has changed, and allow you to load it. Once it loads, it will contain the previous contents, with a list of the current directory at the bottom! Other uses might be: adding and committing files to svn as you are editing them, running utility functions on your file, executing it, and more.

  • !: To the shell
    As shown in the example above, “!” acts as an escape to the shell. It can be followed by any bash command. Once executed, the output is shown, and stays until you press a key. After this, you are returned to the file you were editing. This can allow you to perform various checks, or any command you feel like running, without leaving vim.
  • ctrl+v: Visual mode in columns
    This enters visual mode for columns, instead of characters and rows with just “v”. I haven’t thought of too many uses for this aside from variations on the example mentioned in Lilly’s video: list tags. Really any code tag that often involves a large number of lines with similar beginnings, such as:

    1
    2
    <li>I am some item.</li>
    <li>I am some other item.</li>

    What if you wanted to add a class to all those li’s? But not every li in the file… You could go to the position in the first where you wanted to make the addition, press Ctrl+v, press “j” down to the last line of those you want to change. This press Shift+i, type what you want to add, and press Esc twice. Then whatever you typed in the one line will appear in the same place in all lines selected!

    1
    2
    <li class="spiffyItem">I am some item.</li>
    <li class="spiffyItem">I am some other item.</li>
  • ZZ: Faster write and quit
    This equivalent to “wq”, saving and quitting the file. But the whole sequence is two keystrokes shorter using “ZZ”. It’s also a lot faster to hit the same letter twice than two different ones.
  • taglist: Handy code browser and more
    taglist is a great plugin for browsing code. Be sure to read the install instructions, as you need to run a special command to initialize it. After it’s installed, enter “TlistToggle” in command mode. A window will open showing all the functions, classes, and other entities in your current file. You can Ctrl+w, move into it, scroll up and down to a function you want, and press enter. You are taken right to that function! And after a second or two, that function/class/etc is highlighted in the taglist window:

    This is a very customizable plugin. I won’t go into much in that department, check out the extensive help file (press F1 once you open the tag window). Some good improvements so far:

    • Add to .vimrc:
      1
      2
      " A shorter command to toggle Taglist display
      nnoremap TT :TlistToggle

      Now you can just press “TT” to view taglist.

    • Set “Tlist_Use_Horiz_Window” to 0: This causes the tag window to appear across the bottom, which I find easier to read.
Share and Enjoy:
  • email
  • LinkedIn
  • Slashdot
  • StumbleUpon
  • Technorati
  • Netvibes

Post to Twitter Post to Delicious Post to Digg Post to Reddit

No related posts.

Related posts brought to you by Yet Another Related Posts Plugin.

This entry was posted in CLI and tagged , , , , , . Bookmark the permalink.

7 Responses to More vim tricks and vim screencasts

  1. Justin Lilly says:

    Hi. Just wanted to say thanks for the mention!

  2. Mike says:

    Great you just learned me taglist, it was the last thing I was looking for after having found out the plugin Project :)

  3. Justin: No problem! Thanks so much for the screencasts. I hope you make more, they were great!

  4. Kevin M says:

    :r!ls will insert listing of your current directory after the cursor, :0r!ls will
    insert at the beginning of the file, $r!ls will insert at the end of the file…

  5. Kevin, running that command made me laugh out loud. So much shorter! Thank you!

  6. Moos says:

    Nice python taglist plugin there!

    For saving and exiting in vim, :x works too.

  7. Wow, I really need a new comment plugin. Moos’ suggestion was “: x” (no space) to save the file close vim. My apologies.

    I like any command that gets away from having to type the colon, as that seems to always take the longest for my poor fingers. That’s what I especially like “ZQ” and “ZZ” to quit without saving and save and quit, respectively. Takes a lot less time for me at least.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>