Sunday, January 17, 2016

Getting started with vim, stop using Nano.

Come on guys, when I'm standing behind you, and you fire up nano, I'm not thinking pleasant things. Not to say bad things about nano, pico and their ilk (no really say bad things, why are you using nano and pico).

If you're not already using vim and you are an admin or operator of any kind, take a second right now breathe in, and say to yourself "I am going to learn vim, I am committed." No, for real do it. I'll wait. Get a book if you have to.

Now say, "I will never use nano again."

Vim is a modal editor that is found on nearly all *nix machines. If vim isn't there vi will be, which is it's predecessor. You will at first be in a command mode, from this mode you can do basic things like save the file with these strokes ":w filename.txt" (write to filename.txt). You can also do basic searches by using "/" and the search term you want. To enter insert mode you need only hit the "i"
, and you can get back to command mode by hitting escape. Vim will most likely require a little set up, and on some distros, one setting in particular may in fact make it unusable. If the backspace is wonky, perhaps acting like you hit the insert key in Microsoft Word, you need to open, .vimrc in your home folder, and if it's not their create it(this is the only time in your life you are allowed to use nano, just forget the "never use nano" thing for a second then remember it afterward).  So just add this one line at a minimum:

set backspace=indent,eol,start

Here is my roaming .vimrc:

set expandtab   "changes tabs to spaces DO NOT USE TABS
set shiftwidth=4 " 4 characters for indenting
set showmatch " showmatch: Show the matching bracket for the last ')'?
set backspace=indent,eol,start "set the backspace so it behaves like a backspace
set nowrap " don't wrap by default
syn on                                   "syntax on
set completeopt=menu,longest,preview
set confirm          
set number
set wrap
set linebreak
set nolist

And on a few machines, because of a great vim post I read, I also add these:

nnoremap <C-J> <C-W><C-J>
nnoremap <C-K> <C-W><C-K>
nnoremap <C-L> <C-W><C-L>
nnoremap <C-H> <C-W><C-H>

Those are for split jumping using only ctrl, and the vim movement keys(hjkl). I use the vertical split a lot, ":vsplit" or ":vs" I rarely use horizontal splits but if that's conducive to your style, maybe you have a vertical monitor or something, these entries help.

Regex in vim is one of my favorite things. I don't care how insane that sounds. So if I want to replace "whatever" in a post with "and a thing", and I want it case agnostic, and to do it no matter how many times it occurs on a line, or in the file:

:%s/whatever/and a thing/gi

It is important to note that vim uses POSIX regex and not Perl only the second best regex.

If you want to read more about vim, this is my favorite post on the internet about it.

No comments: