by gamegoblin · 13y ago · 0 comments
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.
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.
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.
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 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> function! NumberToggle()
if (!&number && !&relativenumber)
set number
set norelativenumber
elseif (&number && !&relativenumber)
set relativenumber
else
set nonumber
set norelativenumber
endif
set foldcolumn=0
endfuncNumbers 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.
> 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.
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.
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).
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.
I just switched b/p (since I use p w/ ctrl-p as a fuzzy finder to list all files).
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.
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&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.
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.
Thank you.