3

What Vim features and extension do you use? Which would you recommend for a Linux sysadmin?

Alakdae
  • 1,213
  • 8
  • 21

13 Answers13

10

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.

Matt Simmons
  • 20,218
  • 10
  • 67
  • 114
  • 1
    gorramit.. +1 for beating me to it with 39 seconds ;) – Commander Keen May 31 '09 at 22:04
  • 1
    And you get your comment voted up for being a fellow browncoat – Matt Simmons May 31 '09 at 22:05
  • +1 - this is one of the things I struggle with in Linux/UNIX environments; how do you customise things to take advantage of nifty features, without leaving yourself feeling crippled when you're working on that server you only touch once every six months. – Murali Suriar May 31 '09 at 23:37
  • Murali: I've got a friend who uses Dvorak, and I use the same argument on him, but he doesn't get it ;-) – Matt Simmons May 31 '09 at 23:44
  • Well if he can use dvorak without the need to play find-wally on keyboard, it's not a problem, but otherwise - yes. – Xerxes Jun 01 '09 at 00:07
  • 2
    Surely it's not too hard moving a .vimrc around?! Whenever I need mine I just wget http://bluebottle.net.au/.vimrc and voila. – Alex J Jun 01 '09 at 06:26
  • @Murali: setup your environment when you configure the machine for the first time - it's one of my first steps when I setup new machines. – drybjed Jun 01 '09 at 08:34
  • @Maciej: for my personal stuff I do; but at work there are dozens of people who have (proxied and audited) root access to all the servers in the environment, so any change I make will affect them as well. – Murali Suriar Jun 01 '09 at 10:01
6

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 =)

Commander Keen
  • 1,253
  • 7
  • 11
  • I should say that vim is not everywhere - but I know it not only works on our HP-UX systems, but on the OpenVMS machines as well. Truly a portable platform! – Mei Jun 23 '09 at 18:24
4

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.

Outdoor
  • 81
  • 2
  • Macros are a feature of vi - storing macros using "q" is a vim special feature. – Mei Jun 23 '09 at 18:25
3

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

which provide:

  1. case insensitive search
  2. smart case-insensitive search, such that case-sensitivity is turned on if you search for a pattern using mixed-case characters
  3. 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:

set paste

and the text formatting will no longer suffer from the indentation problem.

Issuing a 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.

MKL
  • 161
  • 2
2

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

That said, :set number to turn on line numbers and :syntax on to allow highlighting will make vim a bit more friendly. Also, learn text objects.

Lucas Oman
  • 101
  • 2
2

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.

Gavin McTaggart
  • 1,826
  • 16
  • 14
  • If you use s/.../.../g a lot, you might consider including 'set gdefault' in your ~/.vimrc – drybjed Jun 01 '09 at 08:36
  • 2
    Hmmm. It's a good thought, but I think that would cause me more problems than it's worth. I use regxes in more than one program and in more than one language; I feel that it would break the 'rule of least surprise' to have this option enabled. – Gavin McTaggart Jun 01 '09 at 09:27
1

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

hartem
  • 196
  • 3
0

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.

wbogacz
  • 101
  • 2
  • 2
  • 6
0
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>
mateusza
  • 547
  • 4
  • 5
0

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).

jtimberman
  • 7,511
  • 2
  • 33
  • 42
0

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
Alex J
  • 2,804
  • 2
  • 21
  • 24
0

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):

:w !/bin/sh

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).

kubanczyk
  • 13,502
  • 5
  • 40
  • 55
  • True, filtering a buffer can be useful - but it is almost never needed unless you need to do some serious filtering. Your example for instance, can be done with :%/unwanted_regex/d – Mei Jun 23 '09 at 18:28
  • Oh does it? Never needed that syntax. I try to stick to tools I know by heart, like grep in this case. – kubanczyk Jun 23 '09 at 19:27
0

I live and breathe by this command:

:se nowrap

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.

Mei
  • 4,560
  • 8
  • 44
  • 53