What Vim features and extension do you use? Which would you recommend for a Linux sysadmin?
closed as not constructive by Sam Cogan Jan 18 '12 at 21:16
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. If this question can be reworded to fit the rules in the help center, please edit the question.
This probably isn't going to be the prevailing opinion, but I use vanilla vim. I don't use any special features (other than syntax highlighting and line numbering), but it's great, because I can go to any Unix machine and feel right at home with whatever vi they happen to be running.
I feel that the power of vim for a sysadmin is that it's everywhere and you can instantly just start using it. That's why I have choosen not to customize my vim.
That said, here is a small .vimrc that I tend to copy around:
set background=dark syntax on set showmatch set ruler set softtabstop=4 set sw=4 set tabstop=4 set expandtab set autoindent set pastetoggle=<F8>
The tab-things are just because I use Python a lot, and whitespace matters there =)
I read a book about vim (Learning the vi editor) from O'Reilly, and that was all I needed. A couple of things that I got out of it that I use all the time...
1) storing macros. Hit the 'q' key, then the key you want to bind it to, then do the command, and hit 'q' again. Then, if you hit '@key' it'll execute that command. If you hit '@@', it'll execute the last command. I love this for "joining every other line" sometimes: your macro is '^Jj' (go to beginning of line, Join the line, move to the next one). Then, do '@j' (j is my macro key for this). Then, just hold down the '@' key. It'll join every line with the next one, move down, do it again, etc.
2) Splitting windows. Type in ':sp' (horizontal split) or ':vsp' (vertical split). It'll split the window. You can then move between windows with Ctrl+w [hjkl] to move to the right, down, up, or left, respectively. ZZ or :quit to close the window. :e to open another file. Especially useful for diffs (vertical split) or whatever else (copying / pasting for the easy).
3) If you code, folding is fantastic. Folding, basically, closes or opens segments between markers. Determining the markers can be done manually, by regex, by... whatever. Google "folding in vim" and you should get some good tutorials.
Off the top of my head, here are some of the options that I'll manually set if I don't have a .vimrc on a particular server:
set ic set sc set incsearch
- case insensitive search
- smart case-insensitive search, such that case-sensitivity is turned on if you search for a pattern using mixed-case characters
- turn on incremental search results, where matches are highlighted while the text is being entered
When pasting text into vim with auto-indent turned on, any leading whitespace in the pasted characters can lead to formatting problems where the texts indent level increases with each line. In that case, simply issue the following command:
and the text formatting will no longer suffer from the indentation problem.
set nopaste will disable this mode.
Visual mode is also incredibly useful, allowing one to select blocks of text that can then be yanked, deleted, transformed, etc.
To enter line-wise visual mode, hit
shift + v and the standard up/down movement keys will select lines of text.
To enter character wise visual mode, hit
ctrl + v and the left/right/up/down movement keys
will allow one to select blocks of text.
Another common feature that I use: selecting text then hitting
gq to format it into lines broken at 78 characters. This is particularly useful when I edit Wiki articles using the "Its All Text" Firefox plugin.
If we're talking features, you can't go past the ease of search and replace with
:s/../.../[g]. Of course, knowing Regular Expressions is an absolute must to get the most out of this feature.
And if you code with any regularity, the ability to use the '#' key to find the previous instance of a string is a massive time-saver.
Everyone's saying that they avoid using a .vimrc because they're on different servers all the time. I've never been on a server that didn't have
wget installed. Put your .vimrc on github, and it's as simple as:
wget -O ~/.vimrc http://github.com/lucasoman/Conf/raw/master/.vimrc
:set number to turn on line numbers and
:syntax on to allow highlighting will make vim a bit more friendly. Also, learn text objects.
I find tabs in vim quite useful. New file in tab is opened by :tabe and navigation between tabs is done using :tabprev and :tabnext. You can add the following lines to your .vimrc to be able to navigate between tabs with alt+arrow keys
map <silent><A-Right> :tabnext <CR> map <silent><A-Left> :tabprev <CR>
You can find more info about tabs here: www.linux.com/archive/articles/59533
au FileType c set foldmethod=indent au FileType c map <F5> :w<CR>:!make<CR> au FileType c map! <F5> <ESC>:w<CR>:!make<CR>
I've pretty much switched to TextMate as my workstation is a Mac, and most of my 'administration' is programming infrastructures with Ruby.
However, I still use Vim quite a bit when logged into servers. I use a couple plugins to make my life easier.
I use the Vim configuration shared by our CTO, with a couple of my own modifications (disable hlsearch, change the colorscheme, mainly).
These two commands will remember details about every file you open on the system. As well as that, it will also save any data you store in one of Vim's 'registers' (basically a clipboard).
" Reopen files where we left off if has("autocmd") au BufReadPost * if line("'\"") > 0 && line("'\"") <= line("$") \| exe "normal! g'\"" | endif endif " Remember for 100 files: (also save every register with <1000 lines), but not for files in /mnt or /media. Save to ~/.viminfo set viminfo='100,s1000,r/mnt,r/media,n~/.viminfo
I try not to use any vim-specific options, because I still stumble upon systems that only have vi. The only exception is Ctrl+V for "column-wise" text operations.
That being said... To execute the current file (not buffer) you just:
To run buffer to interpreter's standard input (without saving to a file first):
Can be also used with python, perl -w, etc.
A super useful technique is to filter a buffer through external command:
1G!Ggrep -v unwanted_regex
(copied my answer from other vi question).
I live and breathe by this command:
When the code wraps to the next line, it becomes unreadable and incomprehensible - not to mention that which line is which gets muddled.
Of all of vim's features, this is the only one I find myself really wanting from vi.
I'm a former user of TextPad, and had memorized many of that program's accelerator keys. One of the first things I did in Vim was learning the map function to keep consistency (F5 search, F8 replace, etc.). I created my personal colorscheme, and borrowed alot from others' vimrc files. Viva Vim! Configurable to any degree.