Vim Tricks for Sys Admins

I don’t always write in Vim, but I am trying to do so more often since I work in it a lot of the time anyway. You can find a near infinite number of lists online of the benefits of switching to Vim. Once you get to a certain point learning it, though, it can be hard to find more advanced tricks for mid level users. A lot of folks want you to use plugins to do some things that you can already do ootb in Vim. Most posts come standard with a few things, including a mention of “:w !sudo tee %” to save when you forget to sudo. While that’s useful sometimes, it isn’t something you should really need to often. I did some scrounging a while back and found some tricks that I’d like share. That said, I’m not a programmer. I’m a sysadmin who also writes fiction and poetry. Since I’m not coding all day, I get to keep things pretty simple.

The ‘:.! command’ will drop the output of a command to the location of your cursor. If you’re in the middle of your work and get a call, pop open a new window with ctrl + w + s, then run ‘:.! date” to time stamp your notes. To temporarily hop out of Vim into bash, run ‘:! /bin/bash’. Exiting the shell as you normally would will pop you back into your vim window. Running the command “! sudo -s” will drop you into a root command prompt.

As an admin, I need to compare files on a remote machine with my own quite a bit. Using the -c switch when opening vim allows you to run commands on launch. Combine that with scp and you’ve got a nifty way to look at two files side by side. Not only that, but the -c switch can handle up to ten commands! The command to compare your fstab side by side with one on another machine would
look something like this:

‘vim -c “:vsp scp://username@remoteserver.domain.local//etc/fstab” /etc/fstab’

Now that you’re editting fstab on another machine using a standard user account, being able to save using sudo is a bit more useful. You’re not limited to staying in that
file, though. Running ‘:E’ will drop you into the file explorer on the
remote machine.

Make a mistake a few minutes back while changing up the fstab and don’t want to
undo 100 times? View a previous version of the document using ‘:ealier 15m’.
Revert back to viewing the version you were working on with ‘:later’.

Finally, if you decide you like the Vi keybindings and want to use them
elsewhere, set Bash into vi mode with “set -o vi”.

Author: A. M. Langston

Poet and Novelist

2 thoughts on “Vim Tricks for Sys Admins”

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s