Hacker News new | comments | show | ask | jobs | submit login

I got pretty proficient at vim using basically the defaults (I added tabs to spaces and tab width to 4 spaces, and auto/smart indent in the .vimrc). These are the keys I use regularly. If you can memorize these, you can use vim proficiently:

  h/j/k/l (movement keys)
  i (insert mode)
  esc (leave current mode/go to default state)
  u (undo)
  >> / << (indent, unindent)
  dd (delete line)
  yy (copy line)
  p (paste)
  v (visual mode, used to highlight stuff)
  gg (go to beginning of file)
  G (go to end of file)
  r (replace the char under the cursor with the next char entered)
  % (jump to next paren/bracket, or if on a paren/bracket, jump to matching paren/bracket)
  w (go to next word)
  dw (delete word)
  cw (change word (deletes the word then goes into insert mode))
  A (append (goes to the end of the line and into insert mode)
  0 (go to beginning of line)
  $ (go to end of line)
  /some_word (searches for some_word, hit n to jump to the next occurrence)
  ?some_word (searches for some_word, in reverse direction)
  
Common commands:

  :%s/regex/replacement/g (replace regex matches with replacement)
  :q (quit)
  :q! (quit without saving)
  :w (save)
  :wq (save then quit)
  :x (save then quit)
  :set blah (turns on setting blah)
  :set noblah (turns off setting blah)
Most vim commands you can stick a number N before to execute N times.

e.g. 5d deletes 5 lines. 5y copies 5 lines. 5> indents 5 lines. 5j jumps down 5 lines. Some common command combinations:

  %v%> (jump to next bracket, go into visual mode, jump to next bracket (highlighting everything in between), then indent it all)

  ggdG (jump to top, then delete everything to the end of file, (clears a file))

  ddp (deletes a line of text and then pastes it (below where it currently it), thus this transposes two lines)
You can see how you quickly develop strings of commands you rattle off by chaining basic commands together.

EDIT (joke):

Q. How do you algorithmically generate a cryptographically strong random string?

A. Put a newbie in front of vim.




The ones I find most useful while coding are

    ci"
    ca)
The i and a here are "in" and "around"

So you can be anywhere inside a quote and do "change inside quotemark" and it'll delete everything inside and leave you in insert mode. "around" would delete everything inside and the quotemarks.

These can be used with other things, parentheses, square brackets (and I think some plugins extend it to html tags and the like).

Of course, you can swap out c for d, y or any of the other verbs.

Working in clojure and deleting everything inside a particular level of nested parentheses is awesome.


One nice little extra is that your cursor doesn't even have to be inside the string for the command to work.


Huh. I know almost all of those key bindings in vim, because I have to use vim when I'm editing over ssh. And that brings me to about 20% efficiency compared to what I can do with a modern text editor (I don't use Sublime, but another GUI IDE -- which one doesn't matter, except that it's not Eclipse). So I guess there are different levels of "proficiency" of editor use?

On a related note: I've never understood the fixation with being able to enter numbers into vim. I almost never know the exact number of characters or lines I need to move/delete/indent/whatever, but key repeat is fast enough to "replay" or redo an action that I'm likely done with it before I would have figured out the exact number.

I like the general concept of being able to "program" an editor with strings of commands. But in practice I'm much faster with the GUI approach. As a bonus, all of the commands I need to know to edit text in a Firefox window, or in any other window in a GUI-based OS, also follow the GDI standards, so I get a base level of efficiency in whatever program I need to enter text in.

VIM is great for editing over ssh. When I have no choice. But I feel seriously handicapped having to use ONLY the basics to move around, copy, paste, and edit, when I'm used to having much more powerful functionality at my fingertips.


I guess it's also one thing to know them, and one thing to be comfortable typing them in a nonstop flow of text. My keystroke speed rarely changes from insert mode to control mode; vim commands have become an integral part of my typing process. I feel severely gimped whenever I can't use my vim controls.

As for knowing the number of lines you need to jump, either do :set number and be quick at math, or you can modify the numbers to be relative to your current line.

I took me probably a week to be able to actually get anything done in vim, and a month to prefer vim to anything else.

That being said, I know far more commands than the ones I listed. Those commands got me to my first month, though.


I think the number thing can be confusing, and it is hard to think in terms of 'delete the following 5 lines'. For those things I tend to use visual mode and highlight (still think keyboard-based selection is faster than with a mouse most times).

I also have a shortcut for a 3-way toggle using F2 - so I can either see absolute, relative or no line numbers at all. I don't use it much, but it can be handy at times.

    function! NumberToggle()
      if(g:numberstate == 0)
        set number
        let g:numberstate=1
      elseif(g:numberstate == 1)
        set relativenumber
        let g:numberstate=2
      else
        set nonumber
        set norelativenumber
        let g:numberstate=0
      endif
      set foldcolumn=0
    endfunc
    let g:numberstate=0
    nnoremap <F2> :call NumberToggle()<CR>


You can get rid of the global variable by testing the actual settings instead:

    function! NumberToggle()
        if (!&number && !&relativenumber)
            set number
            set norelativenumber
        elseif (&number && !&relativenumber)
            set relativenumber
        else
            set nonumber
            set norelativenumber
        endif
        set foldcolumn=0
    endfunc


Thanks! It was my first attempt writing a vim function, so wasn't exactly sure. This looks much better.


Numbers are useful for repeating actions in a deterministic way: I'd take 6p over Cmd+v-Cmd+v-Cmd+v-Cmd+v-Cmd+v-Cmd+v any time.

Numbers are also a good way to limit haphazard movements that have nothing to do with what you are trying to do. Do you want to "move the current line to after line 13" or do you want to "move to the first column of the current line, select to the EOL, cut, move down, down, down, down, down, down, open a new line and paste"?

    :t13<CR>
Anyway, you don't need to use {count} for everything.

Do you want to "group together all the variable declarations scattered around the current function at the top" or do you want to "move your cursor to the line of the next variable declaration, move the cursor to the first column, select to the EOL, cut, move up, up, up to the top of the function, open a new line, paste and repeat that nonsense for every variable declaration in that code block"?

    vi{               " visually select the function
    :g/var/m?funct<CR> " move every var declaration right under the function declaration
You'd be blind to not see any value in all that. Being able to do (almost) exactly what you have in mind rather than a long and awkward combination of unrelated actions is an incredible boost.

You have many "more powerful functionalities" at your fingertips. Being lazy or incredulous doesn't make Vim an overrated tool, it makes you an inefficient user.

Using Vim efficiently is a very valuable goal but it's not a goal that can be reached without a little bit of work. You won't get any benefit if you refuse to do any investment.

"Normal" editors/IDEs don't require that much learning because they don't offer much in the first place. You can become a TextMate/Sublime Text power-user in a week because there's not much to them. Becoming a Vim power-user is a life-long experience: it's your choice if you want to take that path or not.

That said, nobody (I hope) is forcing you to like it or learn it. Keep using what works for you, there's absolutely no problem with that.


The guy I was replying to claimed that one could use it "proficiently" with just those commands. I disagreed, and you didn't contradict that assertion.

> I'd take 6p over Cmd+v-Cmd+v-Cmd+v-Cmd+v-Cmd+v-Cmd+v any time.

Does Cmd+V not repeat? If so, I'll add that to the list of reasons I could never be happy on a Mac. I will happily hold down Ctrl-V for as long as I need rather than trying to think in numbers, yes, absolutely, ESPECIALLY when the number I need is some number between, say 20 and 30. Thinking in exact number of things that I'll need interferes with the (mental) registers I'm using to hold all of the code I'm thinking about. This is also true of the more complex vim commands I've seen, including the ones you've quoted.

Nice trick, though, with the "move var declarations" example you gave. Considering I tend to do the opposite (moving variable declarations to be as late as possible), that particular trick isn't something I'd need, but I see how it could be useful in general.

Thing is, if I had a similar task, I'd create a macro that would do the right thing. There's a skill to creating macros like that, of course, but really learning to use a powerful GUI editor is a life-long experience. It just happens to have a shorter and almost infinitely less painful learning curve at the beginning than vim.

The problem is that some people (such as apparently the developer I was replying to) learn the basics and never any more. That's true of users of all editors, I'm sure. But just because 95%+ of GUI editor users don't ever go beyond the basics doesn't mean that there isn't power hidden under the surface. Maybe more vim users delve deep into its power, statistically speaking. I submit that says more about the kind of person who would use vim than it does about the power of vim relative to (modern) GUI editors.


I plateaued very quickly with TextMate. I'd say after 5 months. I'm 3+ years in with Vim and I learn new things every day.

Those numbers are obviously bothering you but they are definetely not needed. Stop focusing on that. They are only one way, largely inherited from vi's ancestry, but there are so many other ways. I don't use counts much for the same reason as you (and I find that counting breaks my train of thought) but I use search and "advanced" motions a lot and quite naturally. The ability to jump to arbitrary positions in my code and directly work (without selection) on things like inner parenthesis and other text-objects have been extremely useful in my day to day work.

Once you are used to those text-objects and motions, using any other editor is a monumental pain in the ass, however advanced and modern they are.

But, as I alluded at the end of my comment, I don't mean to convince you or anyone of the superiority of Vim because I don't care: I have no interest in the success or failure of Vim. I use Vim and a couple of IDEs in my work and I'm totaly fine with you using and prefering other tools.


I have a scenario that I need help with in VIM: switching between 2-3 files while working on them simultaneously.

For ex: working on an HMTL+CSS+JS documents all at once. Assuming smaller screen where I can't have a vsplit screen.

How to I jump between them quickly? Buffers/tabs?

I love VIM but I haven't found a good tab/file switching solution. In Textmate tabbing was straight-forward. VIM doesn't seem well suited for tabs (especially with things like NERDTree and control-p).


I don't have any plugins for this stuff. I just do:

    nnoremap <Leader>b :buffers<CR>:buffer<Space>
    nnoremap <Leader>n :bn<CR>
    nnoremap <Leader>p :bp<CR>
My <Leader> is space. So "space+b" opens up my buffers, I type a few letters that match a file and Tab to autocomplete it, then press enter to open it. I also use ctrl+shift+6 to quickly switch between the 2 most recent buffers.

So I'd give that a shot. If that doesn't work for you, try just ":tabe <file>" for your three files, then ctrl+pageup, ctrl+pagedown (or gt, gT) to quickly switch between them.


Thanks this is working for me.

I just switched b/p (since I use p w/ ctrl-p as a fuzzy finder to list all files).


For opening each file the first time:

https://github.com/kien/ctrlp.vim

For switching quickly between the 10 most recently used buffers:

https://github.com/vim-scripts/LustyJuggler

I suggest adding this to your .vimrc (the default mapping is <Leader>lj; my leader key is the spacebar, and I find space space to be much better):

let g:LustyJugglerDefaultMappings = 0

nnoremap <silent> <Leader><Leader> :LustyJuggler<CR>

For very quickly jumping between the current buffer, and the last buffer I have a mapping:

nnoremap <leader>dd <C-^>

because <C-^> is a bit awkward; <leader>dd could be anything that's very easy to type.

So most of the time I'm alternating between two files using the <C-^> mapping. When I need to jump to a less recently used buffer I double tap <space>, opening LustyJuggler, then double tap the appropriate home row key to jump to the buffer I want. If the buffer I want isn't in the ten most recently used, I open Ctrl-P, type enough characters from the file path of the file I want to make it the first choice, then hit enter.


Vim's tabs are not the same as TextMate tabs: they are workspaces, not 1-to-1 proxies for files. Don't try to use them like in other editors, it's an exercice in futility.

You can make use of command-line completion:

    :b <Tab>
which does partial words too:

    :b js<Tab> completes with teh name of your JS file
You can make command-line completion even faster:

    set wildcharm=<C-z>
    nnoremap <leader>b :b <C-z>
You can try quick, generic, mappings:

    nnoremap <PageUp> :bnext<CR>
    nnoremap <PageDown> :bprevious<CR>
You can try more precise ones:

    nnoremap <leader>1 :b1<CR>
    nnoremap <leader>2 :b2<CR>
    nnoremap <leader>3 :b3<CR>
    ...
to mimic TextMate.

You can try listing and choosing in one move:

    nnoremap gb :buffers<CR>:b<Space>
You can try the builtin Ctrl+^:

    3<C-^> is equivalent to :b3<CR>
You can remap it if you want:

    nnoremap & <C-^>
and do:

    1&


I swear by tabbar[1] as well as a few bindings to make switching between buffers faster. Specifically:

map <C-n> :bn<Cr>

map <C-p> :bp<Cr>

It probably helps that I've mapped caps-lock to ctrl so using ctrl is very convenient for me.

[1] http://www.vim.org/scripts/script.php?script_id=1338


Tabs in vim are different to tabs in other programs. You should be using buffers. This[1] StackOverflow answer is excellent. I use a plugin[2] to view my buffers in a bar.

1: http://stackoverflow.com/a/103590/1212338

2: https://github.com/bling/vim-bufferline


To be honest, I usually just have several terminals open and I alt-` (ubuntu) between them.

I have a friend who has a sweet setup with gvim that has a lot of tab and file management tools, so you might look into gvim, but I don't use it.


If you use tabs, you can use gt and gT in normal mode to switch to the next and previous tabs, respectively.


Kids to day I first hear a similar joke and the editor in question was TECO.


This one post had me learn a lot better than any fancy game or blog post about vim.

Thank you.


I do suggest although you read the "Practical Vim" book, it has some real scenarios in there.


Anything to spread the gospel of vim.




Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | DMCA | Apply to YC | Contact

Search: