Open multiple files with vim

There are many instances when it’s useful to have multiple files open in vim, but if you aren’t familiar with this tool you can find yourself needlessly jumping around between multiple windows. If you are doing any type of real systems work on a Linux operating system I suggest that you familiarize yourself with vim. If you are not already using vim start by opening a command prompt and type vimtutor, once you’ve become familiar with how to navigate, search, and edit a document with vim this post will make more sense to you.

Use vim to see what is different between two files

There are several ways to find differences between two files on a Linux server or desktop. I like to use vim when I’m scanning a configuration file for recent changes from an earlier iteration ( assuming of course that there is a backup of the last known, good, configuration).

Comparing two files is a common task and there are several ways to view the differences between multiple files, but occasionally you may want to do this visually side by side.

Continue reading “Open multiple files with vim”

Search and replace with Vim and Sed

Using search and replace is a great way to save time when editing large files in Linx. Becoming proficient with this task will increase your efficiency and will reduce your time spent doing tedious and error-prone file edits by hand.

For the sake of this tutorial, I’m going to use a copy of the /etc/apt/sources.list file to illustrate some of the changes that we can make. From your users home directory copy the sources.list file to your home directory. Or you can copy the contents of my listed below into a new file.

This tutorial assumes that you are comfortable reading and writing files with vim. If not open a terminal and type:  vimtutor

sudo cp /etc/apt/sources.list /home/luke/sources.txt
sudo chown $USER:$USER sources.txt

Here are the contents of my sources.list file.

cat sources.txt
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial main restricted
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates main restricted
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates main restricted

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial universe
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial universe
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates universe
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates universe

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu 
## team, and may not be under a free licence. Please satisfy yourself as to 
## your rights to use the software. Also, please note that software in 
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial multiverse
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial multiverse
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates multiverse
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse
deb-src http://us-east-1.ec2.archive.ubuntu.com/ubuntu/ xenial-backports main restricted universe multiverse

deb http://security.ubuntu.com/ubuntu xenial-security main restricted
deb-src http://security.ubuntu.com/ubuntu xenial-security main restricted
deb http://security.ubuntu.com/ubuntu xenial-security universe
deb-src http://security.ubuntu.com/ubuntu xenial-security universe
deb http://security.ubuntu.com/ubuntu xenial-security multiverse
deb-src http://security.ubuntu.com/ubuntu xenial-security multiverse

 Vim

In vim, you can use the :substitute command, which is pretty much always abbreviated as just  :s  followed by a pattern that is separated by forward slashes like this.  :s/find/replace/ . This command tells vim to search the current line for “find” and replace it with with “replace”.

To perform a search of all lines change  :s to  :%s . The % in front of “s” tells Vim that we want to search every line and will replace the first instance of our search term with our replace term as this example will show.

vim sources.txt
:%s/xenial/yakkety/

Pressing enter will show give you a summary of how many changes are being made. You can review the file for accuracy and then save the file or quit without saving. Notice that every line is searched.

Vim global search and replace

To change every instance of a word and not just the first instance on a line we need to add the global option to our command.

vim sources.txt
:%s/ubuntu/OLD YELLER/g

Here you can see that every instance of “ubuntu” has been changed to “OLD YELLER”.

If you wanted to find and delete a word you can use the format  %s/<search term>// leaving the replace field empty. For example to remove comment’s from the file.

:$s/#//

Sed – Stream Editor

The command syntax for  sed actually isn’t much different from vim. By default  sed will output to standard output and will not make changes to a file unless you specify options to allow it to save changes.

Similar to our vim example to change every occurrence of “xenial” to “yakkety”:

sed 's/xenial/yakkety/' sources.txt

 sed global edit

As with vim, you can use the “g” option to make global changes. By default  sed will only change the first instance of a search term unless you append g to the end of the search line.

sed 's/ubuntu/OLD YELLER/g' sources.txt

Deleting words from a file works the same way as vim as well.

sed 's/ubuntu//g' sources.txt

 

sed with in-place editing

You can make changes to files with  sed-i . Using the -i option will create a backup copy of the original file before making edits, which will save you from inadvertently ruining a working configuration.

sed -i.bak 's/xenial/yakkety/' sources.txt

Learning to use search and replace in sed and vim will make your life as a Linux Administrator far easier and is well worth the time you will spend becoming comfortable with using these tools.

Change the Default Text Editor in Ubuntu

Change the Default Text Editor in Ubuntu

vimSo I’m a huge advocate of Ubuntu. It has long term support releases, more packages than you would ever need, free online unattended patching, and you always have an in-place upgrade path to the next LTS version. What more could you ask for? I’d like to ask that nano loose its privileged status as the default text editor!

When making changes to sudoers, passwd, or group files you should really be using the built-in tools visudo, vipw, and vigr. These tools will check your syntax prior to committing changes to the file that could break your system. By default Ubuntu opens these files in nano which I find bothersome. If like me you would rather use vim when making these changes here’s how to change default. Continue reading “Change the Default Text Editor in Ubuntu”