1. 79
  1.  

  2. 27

    One little tip I picked up from Practical Vim which I never see discussed is setting global marks.

    Open up your vimrc

    vim ~/.vimrc
    

    Create a global mark

    mV
    

    Now anytime you’re in a vim session, do this:

    `V
    

    and you will immediately be taken to your vimrc.

    1. 4

      +1 for Practical Vim - it’s one of my favorite technical books ever. Little bites of practice to help you build your skills. Can’t say enough good things about it.

      1. 4

        I hadn’t know that was an option. For comparison, this is the mapping I had already defined to jump to my .vimrc, using the proper cross-platform $MYVIMRC variable:

        " edit vimrc
        nnoremap <Leader>ev :edit $MYVIMRC<CR>
        " edit gvimrc
        nnoremap <Leader>eg :edit $MYGVIMRC<CR>
        

        Edit: Oh, and the article itself suggests defining a command :Vimrc for the same task:

        command! Vimrc :vs $MYVIMRC
        
        1. 3

          Thank you

          1. 3

            thank you

          2. 15

            This is by far the best “unknown-vim-stuff” post I’ve read, with the most actual things I didn’t know that will be actually practically useful pretty much straightaway. Thanks!

            1. 6

              I’ve been using Vim for eight years and am still discovering new things.

              I’ve been using vi/Vim for 33 years and am still discovering new things :-)

              1. 5

                Niiiice article. Lives up to its title, too: :g/regex/norm somenormalcommands was new to me, and is probably about to change my life.

                I believe it it is traditional to reply to Vim articles with some tips of ones own, a sort of ritual exchange of vimrc nuggets. My personal favourite mapping is ergonomic: mapping <Space> to the role of : (the colon key). It feels right, to give this most important interaction the most important key. I started out by mapping ; to : to avoid the shift key, but starting commands with the spacebar is even better.

                " <Space> to enter command mode.
                " (It's not worth letting <Space> be PgDn, because PgUp doesn't work, because
                " the terminal passes <S-Space> as <Space>)
                nnoremap <Space> :
                vnoremap <Space> :
                
                1. 5

                  Neovim has an extensive API for composing Vim with external programs. So you can have a Python script send commands to a Neovim instance, or control it through a server. I’ve seen some cool concept demos with this, where people autocomplete based on what’s in their browser. It just seems like a lot of fun!

                  I was aware that Neovim implemented or planned to implement an API like this, but haven’t looked into it at all myself. Having really smart autocompletion seems like a really cool feature and I’d be interested in seeing that browser demo if you could dig it up.

                  1. 7

                    Here it is! https://github.com/thalesmello/webcomplete.vim

                    The two main things I want to try are 1) setting up an old tablet as a giant control pad with buttons that affect the current vim instance, and 2) representing a complex data structure as a set of Vim buffers, where saving a buffer updates the structure in memory. That would probably use the BufWriteCmd event. If you configure that one, it overrides the saving itself!

                  2. 3

                    ctrl-A and ctrl-X Increments and decrements the next number in the line

                    A good idea is to remap these to - and +. It’s normal mode, what else should these keys do? :)

                    1. 1

                      I like this idea, but I think this key combination is reserved for zooming, on my machine even in the terminal application and in the web-browser.

                      1. 1

                        Go to the beginning of the next or previous line, of course.

                      2. 2

                        I was getting started with shellcheck recently and liked it. I started wondering how I might be able to get some form of “integration” with vim using whatever builtin facilities were available. I found this SO answer. I hadn’t known about :read (or :0read for that matter), or expand(’%’) before.

                        I like to use tabs, so I swapped tabnew for new, then wrapped it up in a command in my .vimrc.

                        command Schk execute 'tabnew | 0read ! shellcheck' expand('%')

                        Now I can run shellcheck on a bash script I’m working on with the results appearing in a new tab.

                        Nice that the building blocks for something like this are exposed in a functional (if ultimately not highly discoverable) way.

                        1. 2

                          disabling q: and ctrl-A / ctrl-X is something I should do. :) I live in fear of ctrl-A/X ing some magic number some day. I know I’ve done it before and caught it.

                          1. 5

                            That fear is not unreasonable. If you’re using <C-x> and <C-a>, then you probably also want to set the following:

                            set nrformats-=octal
                            

                            Explanation from Practical Vim (which I recommend every Vim user buys):

                            What follows 007? No, this isn’t a James Bond gag; I’m asking what result would you expect if you added one to 007.

                            If you answered 008, then you might be in for a surprise when you try using Vim’s <C-a> command on any number with a leading zero. As is the convention in some programming languages, Vim interprets numerals with a leading zero to be in octal notation rather than in decimal. In the octal numeric system, 007 + 001 = 010, which looks like the decimal ten but is actually an octal eight. Confused? If you work with octal numbers frequently, Vim’s default behavior might suit you. If you don’t, you probably want to add the following line to your vimrc:

                            set nrformats=
                            

                            This will cause Vim to treat all numerals as decimal, regardless of whether they are padded with zeros.

                            1. 5

                              You don’t have to change nrformats yourself if you explicitly opt into Vim’s updated defaults (available as of Vim 8):

                              " opt into Vim 8’s new defaults
                              " see `:help defaults.vim`
                              unlet! skip_defaults_vim
                              if filereadable($VIMRUNTIME . '/defaults.vim')
                              	source $VIMRUNTIME/defaults.vim
                              endif
                              

                              The file $VIMRUNTIME/defaults.vim will adjust the nrformats setting for you:

                              " Do not recognize octal numbers for Ctrl-A and Ctrl-X, most users find it
                              " confusing.
                              set nrformats-=octal
                              
                          2. 2

                            I also often use '[ and '] which move the cursor to the beginning/end of the last selected block. Especially useful after pasting some text, as they move to beginning/end of the pasted block then.

                            1. 1

                              Am I the only one that loathes vim commands that use [ and ] brackets? I find their location on the keyboard very inconvenient, so I end up with a remapping mess trying to reassign to other keys that are not so awkwardly placed :(

                            2. 2

                              These last few posts about vim have made me start over with a blank .vimrc and go from there. It’s been very refreshing.

                              1. 1

                                Anyone know how to get close to 5 consecutive G’s with vim? I want to make an http://reddit.com/r/ggggg joke.

                                So far I’m up to gggUG which capitalizes the whole file. Maybe it’s not possible to do anything useful with 5 g’s…

                                1. 4

                                  For the entire file, if a line has a single capital G in it, capitalize all lowercase g’s: ggvG:g/G/s/g/G/g

                                2. 1

                                  This is a great article. In no way detracting from it, I would like to argue a point made right at the beginning of the article: VIM fails at discoverability. The author mentions that after eight years of VIM he is still discovering new things because VIM is not discoverable.

                                  VIM is as discoverable as one needs it to be, especially in the age of internet search engines (and I had been using VIM since before Alta Vista or Google). The :h help pages are excellent, and failing that one can google just about anything that once needs. Python development setup? How to replace multiple lines of text? C functions’ documentation? Just google it.

                                  Today, many (most?) editors offer “distraction free mode” which hides the UI. VIM is set up like that out of the box! And when one actually needs a feature, chances are that they are not the first to need that feature and VIM either offers it or a package does. Just google it.

                                  1. 1

                                    Thanks a lot for this! I’ve been using vim for a long time and I learned a lot here.

                                    1. 1

                                      Nine things about Vim itself, one of which will directly improve my life (g ^A)! Love it!

                                      1. 1

                                        g] is wonderful, I’ve wanted to know how to do this and assumed it would be an ex command, so I hadn’t looked it up yet.