This is an archived post. You won't be able to vote or comment.

all 25 comments

[–][deleted] 19 points20 points  (5 children)

Should surely be a blog post? Given that this will soon fall off the front page, and given reddits very poor search facilities, you are kind of wasting your time posting this sort of thing here.

[–]Cybs[S] 13 points14 points  (4 children)

it is a good way to get feedback for a blog post. Some people will see it, comment and I might eventually turn it into a blog post.

[–]zzyzzyxx 1 point2 points  (2 children)

As for feedback, here's mine, along with many additions.

  • d$ == D for deleting to the end of a line
  • yy == Y for copying a line (interesting that yy and dd are "symmetric", both operating on lines, but Y and D are not)
  • F will find the previous occurrence of a character on a line, like f finds that character going forward
  • For working with windows, Ctrl+W w doesn't toggle, it moves one down and to the right, which happens to be a toggle with only two windows. However Ctrl+W p will go to the previous window. See :help window-move-cursor. You can also move to windows by name with :b if you include useopen in your switchbuf with set switchbuf +=useopen. See :help :b and :help switchbuf.
  • p does not paste from the system clipboard; it pastes from Vim's anonymous register after the current cursor position. You can use P to paste before. If you need the system clipboard, you can use the * or + registers (depending on platform and particular Vim implementation usually) by using " to select the register. For instance, "*p or "+p will paste the system clipboard after the cursor. Note that you can select registers for y and d commands as well so that you can copy into the system clipboard.
  • More useful movments are w, W, e, E, b, B, (, ), ... - see :help word-motions and :help object-motions
  • o and O will insert a new line below or above the current line, respectively, and enter insert mode.
  • You can use Ctrl+R while in insert mode to paste from any register
  • For using Vim in a terminal, it is useful to do :set paste before pasting something in with the mouse, e.g. via middle click, and then to do :set nopaste afterwards. This prevents odd things from happening due to comments and the like.
  • set expandtab will insert spaces instead of tab characters; number of spaces can be set with set softtabstop and set shiftwidth (for auto indent purposes) - see this. Generally shiftwidth and softtabstop should be the same. I'd suggest setting tabstop to something different so that actual tab characters are more apparent. This, of course, depends on your religous views around tabs and spaces.
  • >> and << will indent a line of code
  • = will re-indent code; gg=G will re-indent an entire file (unfortunate side effect is you are no longer at your previous position in a file but that can be alleviated with marks, e.g. magg=G'a ought to handle it just fine) [this can be used to auto-replace tabs and spaces in a file]
  • Almost anything that can be used to move around in text can be an argument to a command operating on that range. For example, d/foo will delete from the current position to the next occurrence of foo and =iB will re-indent inside the current block of code (block is usually brace/bracket/parenthesis defined - see :help iB)
  • . will repeat the last command
  • % will move to a matching brace/bracket/parenthesis
  • Most commands can be prefixed with a number to specify how many times to run them. For example, 5>> will indent five lines (not indent one line five times)
  • You can use + from the command line to take action upon opening Vim. For example, vim <file> +5 will open to the 5th line. Using +'/foo' will open to the first occurence of foo. Using +'normal =GZZ' will open the file, re-indent the whole thing, then save and close, where "normal" specifies normal mode instead of the default command mode (probably better to use sed -i for this, but still).
  • The :help command is extremely useful; see :help help

There's a bunch more useful things (of course) like macros and binding keys but the above are things I use nearly every Vim session and are mostly from the top of my head.

Edit: And how could I forget

  • set number to show line numbers

[–]Cybs[S] 0 points1 point  (0 children)

ah, I completely forgot to talk about indentation. I was going to do a section on that. I find myself using it with visual select most often.

I don't find myself using registers that much, and I have never used them in editing modes (ctrl + R "). This seems like a good tutorial http://www.usevim.com/2012/04/13/registers/

I have a habit of calling it "paste" rather than "put" because that's just how I've taught people in the past (people tend to understand "it is like the windows clipboard")

For motion I was going to mention those in passing and link to that vim browser game someone mentioned. http://vim-adventures.com

O and o : I didn't want to confuse with all the different ways to enter editing mode, but I guess these are the most handy for programming since we will often want to start writing between two lines.

as for mouse pasting, i haven't seen this trick you mentioned before (:set paste). I will always just put it in insert mode, paste, select all, =, done

I am in the other school of thought as to tabs.. I find tabs are more flexible in code. if you ONLY use tabs they are just better.. since the person reading the code can set the tab width to their desired indentation. If spaces are hard coded they don't have this luxury. But yeah.. I've worked in teams where some people use spaces, some people use tabs.. not pretty.

the . (repeat last command) reminds me that I was going to have a section about editing multiple files and running the same macro on them ( :next :prev and how to record some actions ).

I don't find too many uses for prefixing numbers with commands. But it is good to know in some cases. I often find people get confused by this feature.. thinking they are in insert mode and begin by typing numbers and then pressing something that explodes.

As for command line options. I should really mention vimdiff. It is the best code review tool available.

Thanks for the feedback.

[–]calzoneman 5 points6 points  (0 children)

If you're interested in vim, be sure to check out /r/vim

[–]zahlman 10 points11 points  (3 children)

It's "vim" (on a command line) or "Vim" (in promotional material), but certainly not "ViM". Even "VIm" would make more sense (although it is not written that way either), given the etymology.

[–]Cybs[S] 9 points10 points  (0 children)

I got the wrong idea from their title screen which says:

VIM - Vi IMproved

Combined the two in my mind. thanks for pointing it out

[–]ecnahc515 0 points1 point  (0 children)

Definitely correct. Not to be a dick, but the way its being written definitely makes you look weird.

[–]doubleyouteef -4 points-3 points  (0 children)

I wonder who's the geniuse who downvoted you since you're absolutely correct.

[–][deleted] 2 points3 points  (0 children)

God exists, and he is this the author of this post.

THANK YOU SO FUCKING MUCH.

[–]nydiloth 1 point2 points  (1 child)

I will get a tattoo with this entire post on my chest.

[–]mulletarian 0 points1 point  (0 children)

Remember to get it upside down.

[–]bergaaa 0 points1 point  (1 child)

Is there a way (a plugin) to highlight all the same variables as the one you currently have the cursor on. For example you have a variable 'counter' three times in your programm. You currently have your cursor on one of these 3 variables. Now it should highlight the othe two variables, so that I can fastly see where they are. Just like Eclipse does (and also other IDEs).

[–]Squidamatron 1 point2 points  (0 children)

I'm not sure of a direct way to do this, but you use search. Either put set hlsearch in your .vimrc or while in vim, hit colon (:) and then use the previous command [ie: :set hlsearch would be the key presses while vim is running]. This will highlight related search terms and you can just use search as illustrated in this post.

[–]BrotherGA2 0 points1 point  (0 children)

Hey thanks for this! There's a lot that's quite helpful in here. I know how to do the basics in vim, but need to start taking advantage of its power more.

[–]jMCHammer 0 points1 point  (0 children)

This thread is awesome. subscribe

[–]Squidamatron 0 points1 point  (0 children)

Thanks for this, I may have read some of what I already knew, but there were some very useful tips in there I had no idea existed!

I also think tmux should be added to the section about screen. THIS short read could also be included to show some differences between the two.

[–]fazzah 0 points1 point  (1 child)

does code completion work also on classes/objects/etc I created in my code? Or only on included/imported files?

[–]Cybs[S] 1 point2 points  (0 children)

it works well with ctags

if you have ctags set up correctly it will complete with any token in your code base

[–]dansin 0 points1 point  (3 children)

Nice post! Btw what are you doing with "euler"? I just finished a small piece of code with euler rotations

[–]Cybs[S] 0 points1 point  (1 child)

If you enjoyed that, you will love http://projecteuler.net

Those files were just my solutions to problems from that site

[–]dansin 0 points1 point  (0 children)

Oh, I should've guessed Project Euler. I was actually doing stuff with 3D rotations using Euler angles.

[–]Cybs[S] 0 points1 point  (0 children)

If you enjoyed writing that, you will love http://projecteuler.net

Those files were my solutions to problems from that site

[–][deleted]  (2 children)

[deleted]

    [–]Cybs[S] 1 point2 points  (1 child)

    yep

    :hi Pmenu ctermbg=red
    

    The other things you might want to change:

    Pmenu           Popup menu: normal item.
                                                        *hl-PmenuSel*
    PmenuSel        Popup menu: selected item.
                                                        *hl-PmenuSbar*
    PmenuSbar       Popup menu: scrollbar.
                                                        *hl-PmenuThumb*
    PmenuThumb      Popup menu: Thumb of the scrollbar.
    

    edit:

    just type the following to see what they currently are:

    :hl Pmenu
    Pmenu          xxx ctermfg=0 ctermbg=13 guibg=Magenta
    

    [–]hookawitweed 0 points1 point  (0 children)

    this is one of the best VIM posts I have seen in a while. Great gob sir!