Nik Kolev on 24 Oct 2008 23:10:27 -0700


[Date Prev] [Date Next] [Thread Prev] [Thread Next] [Date Index] [Thread Index]

Re: any vim users here?


Wow Kyle.... T'was a love indeed...
In some situations I find a good use of :sh (in command mode) - takes you out of vim to the shell where you can do a bunch of stuff (like build - even though if you are building with make you can simply do a :make) and then return to vim (state before :sh is preserved even if you haven't saved the buffer) with a CTRL+d
-nik

On Thu, Oct 23, 2008 at 5:37 PM, Kyle R. Burton <kyle.burton@gmail.com> wrote:

> Are there any VIM users on this list?  I've recently decided to try out VIM
> and I'm looking for advice for becoming a VIM "master".
> Thus far I've been pretty focused on the basics (navigation as well as
> insertion and deletion commands).  I'm finding my way around reasonable well
> and I'd like to take my use to the next level. Does anyone have any small
> useful tricks they often use in VIM?  What features make you use VIM instead
> of some other text editor?  Or if you're using some other editor what makes
> you use it instead of VIM?  Do you have any vim plugins that you can't live
> without?

Are you already using CTRL-N and CTRL-P for word/text completion?
Huge productivity boost if you're not doing it already.

Learn about c-tags, CTRL-T and *.

Learn the not-so basic navigation:

 - H, M, L => move to the Highest, Middle and Lowest parts of the buffer
 - (, ), {, and } - move by sentence and paragraph (blank line)
 - gg and G to jump to the top/ bottom of the buffer

Learn to use the bookmark feature:

 ma => leave a mark named 'a'
 ... move about in the buffer ...
 'a => jumps back to line with the mark named 'a'
 `a => jumps to the character with the mark 'a'

they are a huge boost to navigation when you're working across
multiple parts of a file, or when you need to select a range of lines
(leave a mark at either the start or end of the region, move using the
normal navigation primitives, then y'a or d'a to yank or delete to the
mark named 'a').

Learn to use the multiple named copy&paste buffers - you can append to
them as well.

 "add => cut current line into the 'a' buffer
 "ayy => copy current line into the 'a' buffer
 "Ay} => append from the current position (line based) to the next blank line

Also, don't forget about the numbered buffers 0-9, which allow you to
get back previous yank/cut values (like a stack).

Change how think - imagine all your editing as repetitive tasks and
get used to dot for small repeated actions.  Think higher level, be
obsessive about consistently formated code/text and learn to use the
macro system:

 - qq => start recording a macro named 'q'
 - q [when recording] => stop recording
 - @q 'run' the macro named 'q'
 - @@ re-run the last run macro (very useful for repetition)
 - 10@q run the 'q' macro 10 times
 - "qp => pull the q macro into the buffer, then you edit it and...
 - "add => push the macro back into the q buffer so you can run the
updated macro
 - oh and btw, this is one area where CTRL-V comes in real handy (like
putting a literal ESC into the macro as you're editing it)

Learn how to use :map - b/c you can do things you can't (or at least
it did't support it last time I checked [1]), such as save the file
and move to the next one.  One thing I often do with vim is:

 vi $(find . -name '*.java')
 :map ^W/public class^Mww"aywo  private static final Log LOG =
LogFactory.getLog(^]"apa.class);^]:w:n

Then I hold down CTRL-W till it beeps at me.  The explanation of the
above is effectively: open all the .java files, find the class
declaration, yank it into buffer 'a', open a new line, insert the
string 'private static final LOG = LogFactory.getLog(', hit ESC to get
back into command mode, yank back the contents of the 'a' buffer,
append '.class);', write the file and move on to the next file.  I
didn't test that out for correctness, but it's in the spirit of how I
use vim.

I know you can do this with the various languages (perl, python, ruby,
awk + sed), but b/c you're pulling info from 1 line onto another (and
possibly using other context from the file) it's harder to do with a
line-by-line process.  Automating exactly what I'd do if I was in the
buffer fits how I think better than shifting down from my brain's
vim-commands into perl/python/ruby commands.

Be brave with :map and macros - that kind of thinking will make it
look to casual observers like you couldn't possibly be doing what
you're doing - it'll look like you're navigating code that pre-existed
rather than are actively engaged in editing it (or at least I've
gotten that from people who've watched me work within a well tuned
environment).

Learn to work with multiple open buffers:

 :badd file-name => 'buffer add' - opens a new file
 :b<<digit>> => jumps to that numbered buffer
 :bl => go to the last buffer
 :n => next buffer
 :p => previous buffer

If you're using VIM from cygwin, on Unix (the mac counts), learn to
embrace the other available commands, eg:

 !}sort => from the current line to the next blank line, run it
through sort and put the result back into the buffer
 :r!date => insert the output of the date command

There may be more later as my mind remembers things...

HTH,

Kyle



[1] I haven't used vim seriously (eg: reading up on new features) for
probably ~5 years - though this is how I remember using (abusing?) it
when it was my only love