Vim Intermediate Tutorial Videos

  • :help CTRL-A increments the first number it can find on the line (by one or an optional argument).
  • :help q is how we record a macro. It’s used for both starting and stopping the recording.
  • :help @ lets us run the macro that we recorded.
  • :help :global is how we can run a given command (in this case a :normal command) for each line that matches a pattern.

Page 3 of 3 | Previous page

39 comments on this post.
  1. Mitko:

    Hi Derek,

    you could make use of this:

    autocmd BufEnter * if &filetype != ‘help’ | silent! cd %:p:h | endif

    instead of using ,cd every time

    Cheers

  2. Derek Wyatt:

    I actually do that for C++ files but in truth it’s wildly annoying and I’m thinking about tossing it :) Thx for the tip though… it’s a good one.

  3. Mitko:

    Hi,

    actually :ec$HOME works on any system. I suppose %HOME% is just copied to $HOME, same for all other vars

  4. Phillip:

    Question.

    I want to add number sequentially in a file and add it to the end of the line.

    Just so I am clear. Let’s use and example.

    dummy01
    dummy02
    dummy03

    I have 1000 dummy’s to add but the rest has only dummy. So As you can tell this needs to be sequentially.

    Any easy way to do this in vi or vim?

  5. Derek Wyatt:

    There are a number of ways to do this, but I’ll discuss two here…

    The first is probably just to use the VisIncr plugin – see the video at http://vimeo.com/4457161 and the plugin at http://www.vim.org/scripts/script.php?script_id=670.

    You can also do this as a command-line entry like this:

    let x=0 | g/dummy/s/$/\=x+1/ | let x=x+1

    If you want to format the numbers to be a width of 3, padded by 0′s then you can do this:

    let x=0 | g/dummy/s/$/\=printf(“%03d”,x+1)/ | let x=x+1

    If you only want “dummy”s in a particular range to be modified, then do this:

    let x=0 | ‘a,’bg/dummy/s/$/\=printf(“%03d”,x+1)/ | let x=x+1

    where ‘a and ‘b are marks you’ve created to mark particular lines. Of course, you can use any range element in there that you want (e.g. 1,20).

  6. Blazej Floch:

    You tutorials are really great. I appreciate your effort.
    Could you do one on folding? Or did I miss it somewhere?

    … well if you are not super busy with other stuff. Actually no, I don’t mind if you are busy. Make more tutorials :p

    Cheers,
    Blazej

  7. Mert Nuhoglu:

    Derek, these videos are wonderful. Reading a book or an article cannot bring the knowledge learned from these videos.

    Is it possible that you make videos for normal and command-line modes as well?

    Best, Mert

  8. Jake:

    useful,thank you.

  9. David Rivers:

    These videos are TERRIFIC! Very informative and amusing!

    Please don’t stop the music!

  10. Nick:

    Hi Derek, fantastic videos, you make me fall in love with vim.

    I have a question: I tried to customize my Vim and started with something I thought simple like change the default color scheme and so I’ve simply added a line at the end of the vimrc file like so:

    set background=dark
    syntax on
    colorscheme neon

    Now what happen is weird, I have all the texts with the correct color scheme but the background is wrong, is light-gray like the default one. It seems that there’s something in the default configuration that override my settings. I don’t want to change the vimrc file which is inside the .app (I’m on a mac) because with a new version of vim the problem would be there again and so: can you help me find a way to have my preferred color scheme working?

    I know you’re not a living-help-system so answer me just if you got some free time. thanks ;)

  11. Derek Wyatt:

    I don’t have any wisdom here, Nick. All I can really suggest is trying other color schemes to see if they work. Then it might be the color scheme. Or you can try switching to the color scheme after startup to see what happens.

    Try also not setting the background variable at all and see what happens there.

  12. Nick:

    Thanks Derek, I already tried to use other color schemes and many configurations in the vimrc file with and without the background but the result is always the same. Searching I found out that macports have installed its version of vim and I’m afraid that maybe it could be something in that configuration which create problems but I’m not good enough to check it out. For now I will keep to blindly type (the text color and the background color is the same when vim opens….) the :colo command and change manually the scheme each time I open vim, doing it in this way obviously works (maybe I will try to put something in the after folder when I will be more skilled).

    Thanks again

  13. Derek Wyatt:

    Yeah that sounds like a problem. I don’t use macports or fink so I can’t really comment there, but they could be doing all sorts of configuration trickery that MacVim is picking up (I’m assuming you’re using MacVim and if you aren’t then you should be :D).

    You might want to clear out the macports version and make sure you’ve got a clean system.

  14. Nick:

    I’m planning to uninstall completely macport because I don’t need it when I’ll have some time (it’s there just because I’m lazy :) and yes I’m using MacVim.

    thank you very much

    p.s.
    I just watched the beginner videos and maybe I remember half of the commands you described but I’m already working with vim. great app, great videos.

  15. Nick:

    Solved :)
    I’ve find the problem: it was the gvimrc file. it have some gui settings that override the vimrc (the gvimrc is loaded after the vimrc) settings and so the background was always light.

    off I go to rule the world!

  16. Jaime:

    @Nick This is kind of off topic but I felt compelled to inform you of Homebrew for OSX. It’s better than both Fink and Macports, and doesn’t make a mess out of your system. http://github.com/mxcl/homebrew

  17. Sven:

    Instead of using you “,cd” command, you could “set autochdir”

  18. Derek Wyatt:

    Yup. The only problem with that is you lack some choice. If you want to /not/ change directories, you’ve got to have something that turns it off… I prefer to opt-in.

  19. Sven:

    yeah, after posting I thought about that, too.

    Btw: Big thanks fo the videos. I learned a lot in the last few days.

  20. Rylie:

    In your “Insert Mode” video @ time 3:59, you mention that you have your text width for comments set to 80 and your text width for code set to 140. Um, how do you do that? I mean, there’s only one textwidth property- how do you distinguish comments textwidth from code textwidth in order to set one to 80 and the other to 140? Thanks!

  21. Derek Wyatt:

    See :help ‘formatoptions’

    Specifically, I set formatoptions=crq and textwidth=80

    Now, I don’t wrap code at 120, instead I use a warning marker to let me know when I go over the limit. You can get this easier with Vim 7.3 by using cursorcolumn (see :help ‘cursorcolumn’) but I do it with a syntax highlight. You can check my C++ vimrc settings to get the gist of that.

  22. Rylie:

    Hi again, Derek. Just finished watching your “One Vim… Just One” video. This whole notion of a clientserver Vim sounds wicked cool, but I’m still trying to wrap my head around how it actually works and how to use it correctly. The help page says: “When compiled with the |+clientserver| option, Vim can act as a command server.” I use MacVim. My first question is- how can you tell if your vim binary was compiled with the +clientserver option (or any other option, like python, for that matter)? Second question- how do you invoke Vim as a command server as opposed to a client? I mean, is there a different command line incantation for invoking Vim as a server vs. invoking it as a client (gvim –remote-silent)?

  23. Derek Wyatt:

    You can run :version to see if it’s got +clientserver. As for what you can do with it, the ‘:help clientserver’ should really tell you everything you need to know.

  24. Cliff:

    Had been battling with trying to get –remote-silent working until I did :version and discovered that all of my installs are -clientserver… Why is this not a standard install? Will install it locally. But, I need to install it on a shared hosting server that I don’t control. I wish this were installable as a plugin.

  25. MightyUhu:

    Haha nice i like the World War 3-Part :)

  26. Ryan:

    Great videos Derek!

    I want to have macvim read from stdin with the – flag (i.e. mvim -) but also send it to the vim server via –remote-silent. However, it seems like the remote flags require files. I’d love for something like ‘echo test | mvim –remote-silent -’ to send a buffer with the word “test” to my macvim window and buffer list that’s already open. Do you have suggestions on how to do this?

  27. Derek Wyatt:

    Sounds like work for a shell script:

    #!/bin/bash
    cat - > /tmp/t.$$
    fname=$(head -1 /tmp/t.$$ | awk '{print $1}')
    mv /tmp/t.$$ $fname
    gvim --remote-silent $fname
    

    That’s gonna need a fair bit of tweaking but it’ll get you started.

  28. nach:

    Hi Derek,
    You seem very crazy but your videos are awesome. Thanks.
    I wanted to ask you if there is a reason why you don’t use macvim and use gvim instead.
    I am now convinced to swicth to vimfrom netbeans thanks to your videos. But I am struggling with something that I used to do very good in netbeans. Searching occurences in all the files in a folder(and subfolders), is there a way to do this and get the nice results maybe sorted by file/occurence ?

  29. Derek Wyatt:

    I’m not crazy; my mother had me tested. I do use macvim. The only way to use gvim on the mac is to use X11, and I’m not doing that :)

    As for searching files, check out “:help grep“. I tend to do something like “:vimgrep /pattern/ **/*” which will do a recursive search for /pattern/ in the current directory (and all subdirs). You can then bring up the list of matches using “:cwin“. You might also think about using the “:FufQuickfix” command from the FuzzyFinder plugin.

    As for sorting by occurrence… that I don’t know.

  30. Vídeotutoriales sobre Vim | CyberHades:

    [...] sobre Vim creados por el autor de dicho blog. Están divididos en cuatro categorías: principiante, medio y avanzado. También tiene otra serie en la que nos muestra el uso de algunos [...]

  31. Vim «:

    [...] Vim intermediate tutorial videos [...]

  32. Wesley Steinbrink:

    I totally enjoy your videos just seeing the small part of them I have already. I am probably jumping the gun in asking this, but have you found a good way around the Esc thing going from Insert to Normal? I seem to find that Alt-l (Alt with lower case L) seems to work well for older versions of vim (and for gvim) and also for the bash line (when one does set -o vi). Therefore when I find a vim or vi that won’t, I probably will do an imap to make it so. What do you think?

  33. Wesley Steinbrink:

    Apparently this is already noted:

    “Thus in insert mode pressing alt+h alt+j alt+k alt+l all take you to normal mode and move in the expected direction. You can hold down alt when moving even while in normal mode since the additional ESC that is sent does no harm.”

    http://vim.wikia.com/wiki/Avoid_the_escape_key

    Although the alt+l does NOT move to the right.

  34. Derek Wyatt:

    You’re definitely jumping the gun :) Learn Vi before you start trying to figure out ways to not use it the way it was intended. It’s a powerful editor if you use it the way it’s intended to be used – once you’ve got that, you can start customizing it in ways that you truly understand are better for you.

  35. Wesley Steinbrink:

    Great videos. Knew about macros, but didn’t even think of going across files! And being able to edit them – golden.

  36. Chose:

    That is a great source of wisdom! However I always wonder how the creators of vim binded keys to commands.. seems like they did it somehow randomly… no scene at all…
    Could you please tell me how do you move through the tag completion list? I can do it only with arrow keys but these are evil right? Besides is there a way to move back one character when in insert mode? The only way I know is to use back arrow… evil

  37. Derek Wyatt:

    CTRL-N and CTRL-P move through the pop-up windows. As for moving in insert mode… well, umm… I don’t :) You might want to read through the help and see if there’s a way to do that in a way that you like. It will be a CTRL key, somewhere.

  38. Chose:

    cool! thanks. and according to :h the fastest `good` way to get back one character in insert mode is to use `c^o i`. blah..

  39. Derek Wyatt:

    Left arrow should do it, if you like that. If you don’t, you can simply map a new key to do it. For example, if you want map CTRL-B to do it you can do that with :imap <c-b> <left>

Leave a comment