Saving Read-only Files in Vim – The sudo tee trick

How many times you had the following situation: You open a file with Vim and make some changes. When you try to save the file, you realize that you didn’t run Vim with sudo?!

There is a solution for this. The next time when you get into this situation, you can use the following command in order to save the changes you made:

:w !sudo tee % >/dev/null

There’s just one problem – it’s a bit hard to remember it. That’s why, if you’re going to need it often, add the following line to your .vimrc:

cmap w!! w !sudo tee % >/dev/null

This way, when you get to the same situation again, you can just type :w!! to save the file, even if you did not run Vim with sudo.


Over the years, Vim got a reputation that it’s really difficult to learn it. I’ve heard many times from guys who are convinced it will take them months to reach proficiency. That’s simply wrong.

That’s why I’ve started to write a book: Mastering Vim Quickly (from WTF to OMG in no time) which will teach you Vim the way I learned it – easily and quickly.

If you want to get updates and sample content from the book, leave me your email below, and I’ll make sure to keep you updated. You could also go to Mastering Vim Quickly page and check it out.


If you liked the post, we should get connected - follow me on Twitter

Why Vim – 21 Reasons to Learn Vim

 

After writing about the history of Vim, I wrote a post where I told you what you can expect from my upcoming book Mastering Vim Quickly.

Today, I will try to mention as many as possible reasons to learn Vim. So, if you’re a beginner with Vim and you’re not sure should you keep learning it, or if you only think about learning Vim, this post if for you.

This post is not going to cover awesome Vim features as reasons to learn Vim, so if this is what you expect, don’t lose your time on reading.

So, here is the list of reasons why to learn Vim:

  1. Vi is guaranteed to exist on all Unix systems and exists on most Linux ones as well. That kind of broad coverage makes learning it worth it – because, if you know Vim then you know Vi as well.

  2. Vim is free and has a vibrant community

  3. Vim works over telnet & SSH connections. There’s no better way to edit files remotely via terminal than using Vim. Sysadmins know this.

  4. Vim’s configuration is portable. You just need to copy a few directories and files, and that’s all.

  5. Vim compiles/has precompiled binaries for almost every OS out there. You will never have to post “I need a [some text editor] alternative for [some OS]” anymore. You’ll have Vim instead!

  6. It’s also usually very fast and lightweight even when editing huge files of source code. Seriously huge.

  7. If you’re a programmer who edits a lot of text, then it’s important to learn an serious text editor like Vim. Serious text editors are highly optimized to perform the kinds of tasks that you will be doing a lot. For example, adding the same bit of text to the end of every line is trivial in Vim, but ridiculously tiresome otherwise.

  8. If you plan to configure anything in a Unix based system likelihood is you going to be editing a fair few config files, therefore you’ll want to learn a terminal based editor, of which Vim is one of the best.

  9. Vim allows you to easily code without taking your hands off the keyboard.

  10. Your fingers very rarely have to leave the home row, which means you’ll be able to edit text very, very quickly.

  11. It starts up faster than any IDE or text editor I’ve used, and it has many powerful features.

  12. It helps you focus on the coding process itself, you won’t be using the mouse at all to deal with it, that’ll save you a lot of time when you’re just writing code.

  13. It’s fun! Editing text is like a game to me now. I sincerely enjoy it – which is pretty ridiculous, when you think about it.

  14. If you’re working remotely, there is no choice to use Eclipse or similar GTK based IDE. You can do everything with Vim: source control, SQL, debug, compile, browsing – really fast browsing even over 1 GB source code. Visual Studio or Eclipse couldn’t handle all of this.

  15. There are a lot of good plugins out there which add a lot of functionality.

  16. Vim is a really good tool once you familiarize yourself with it.

  17. It has a reputation for the quality and the completeness of its docs. That’s true. Vim is thoroughly documented. You will find most of the  answers to your questions in its help system as soon as you will manage to stick the proper keywords in your help queries.

  18. You might want to learn Vim because many people think it is cool.

  19. You don’t have to press Ctrl or Alt all the time in order to run commands or shortcuts.

  20. It’s much quicker to use vi for a sudo edit: $ sudo vi. For example, if you run text editor 100 times a day, with typing sudo emacs, you’ll have to make 300 keypresses more! Okay, this is a joke :) chill out emacs guys (and yes, I know that there’s something like alias :) )

  21. Vim is addictive. You will find yourself wishing you could use Vim commands in all your computing, and cursing whenever you can’t. For example, I use Google Chrome with Vimium extension, so I surf the Internet without a mouse or touchpad. How cool is that!

On the end, have have to add that Vim’s usefulness depends on what you’re working with. If you’re an Java/C#/etc developer, you’ll most probably be more comfortable with an IDE.

But if you work with Python, Perl, Ruby, C, web, etc. or you’re a system admin/devops, Vim is the way to go. Vim is fast, powerful and does everything you need.


Over the years, Vim got a reputation that it’s really difficult to learn it. I’ve heard many times from guys who are convinced it will take them months to reach proficiency. That’s simply wrong.

That’s why I’ve started to write a book: Mastering Vim Quickly (from WTF to OMG in no time) which will teach you Vim the way I learned it – easily and quickly.

If you want to get updates and sample content from the book, leave me your email below, and I’ll make sure to keep you updated. You could also go to Mastering Vim Quickly page and check it out.

[mc4wp_form]


If you liked the post, we should get connected - follow me on Twitter

The History of Vim

What is Vim?

If, for some strange reason, you don’t know what Vim is, here’s the short answer:

Vim is a highly configurable text editor built to enable efficient text editing. It is an improved version of the vi editor distributed with most UNIX systems. Vim is distributed free as charityware.  – http://www.vim.org/

 

You should also know that Vim (a contraction of Vi IMproved) is a text editor written by Bram Moolenaar and first released publicly in 1991. Based on the vi editor common to Unix-like systems, Vim is designed for use both from a command line interface and as a standalone application in a graphical user interface.

Simply saying, Vim is a powerful text editor for programmers, power users and command-line gurus.

 

The History of Vim

When Bram Moolenaar had bought an Amiga computer, he wanted to use the editor he learned on Unix. Since there was no good Vi for the Amiga, he started with the best that was available (a program called Stevie) and started improving it.

At first, his goal was to imitate Vi. Later new functionality was added, and  by version 1.22 Vim included more features than Vi. That time it was renamed from Vi IMitation to Vi IMproved. Over time the gap has grown wider, now it’s hard to think of a reason to use Vi instead of Vim. I would recommend all Vi users to switch to Vim.

Of course, ports to various operating systems have been done by different people. The port to Unix was a milestone, since Vim started competing with Vi. Currently Vim runs on more than a dozen different operating systems. That is awesome indeed.

Vim was made such that it works on many different operating systems. This wasn’t easy. Just supporting the most popular Unix versions is already a big task. Adding support for MS-DOS and MS-Windows gives the additional problem of using file names with a backslash in them. Machines such as the Amiga and the Macintosh have a different kind of operating system, which require specific solutions. – Bram Moolenaar for Free Software Magazine

 

The first version of Vim that was released to the general public was 1.14, which was published on November 2, 1991. Over time Vim has grown constantly. It has become a complex program. Adding functionality is taking much more effort now than before.

Here’s the timeline of Vim development:

1988       Vim 1.0     Vi IMitation on the Amiga
1991       Vim 1.14    First public release (on Fred Fish disk #591)
1992       Vim 1.22   Port to Unix, renamed to Vi IMproved
1994       Vim 3.0     Multiple windows
1996       Vim 4.0     GUI
1998       Vim 5.0     Syntax highlighting
2001       Vim 6.0      Folding, multi-language
2006       Vim 7.0      On the fly spell checker, support for tabs

The list shows only the most important features, but each version brought us a lot of nice features. For example, if we take version 6.0: the major new feature was folding. But, there were other great features such as UTF-8 support, multi-language support (translated messages and menus), vertical window split, improved performance for syntax highlighting, etc.

Vim has grown to an big open source project, but it’s also important to mention that it’s charityware. The basic idea with charityware is that everybody who uses Vim is asked to donate to a charity. Thus the use of Vim is free, if you think it’s worth something, you can give that money to a good cause.  Moolenaar helped with establishing a foundation called ICCF Holland that works to support to a children’s center in Uganda.

 

Vim today

Unlike the other vi clones, Vim is still actively developed and has a large base of contributors. New features are still being developed and added to the text editor every year, making it even more powerful. Besides that, number of Vim plugins is rapidly growing.

At the moment, Vim 7.4 is the latest stable version. It is highly recommended to update your Vim if you didn’t, because many bugs have been fixed since 7.2 and earlier.

Vim has been my editor of choice since 2008, few months after Linux became my main desktop operating system. In a meanwhile, I have changed few different distributions of Linux, but Vim was always there.

I’ve used it to write many lines of code, edit lots of configuration files, and write tons of documentation. Although I’ve tried to use some of the modern text editors, I haven’t found any that match Vim’s efficiency.

I’m not the only one who, after using Vim almost every day for so many years, is still discovering new features, capabilities, and useful tips that further improve my productivity.

At the moment, my favorite work environment is Gentoo with i3 window manageroh-my-zsh and Vim. I have never been more productive.

Over the years, Vim got a reputation – that it’s really difficult to learn it. I’ve heard many times from guys who are convinced it will take them months to reach proficiency. That’s simply wrong.

That’s why I’ve started to write a book: Mastering Vim Quickly (from WTF to OMG in no time) which will teach you Vim the way I learned it – easily and quickly.

Sign up below for updates about the book.

[mc4wp_form]


If you liked the post, we should get connected - follow me on Twitter

How to install Apache (with SSL) + WebDav on Ubuntu

Installing Apache with SSL

$ sudo apt-get install apache2 libapache2-mod-auth-mysql

Then, setup and generate a cerfitecate for the web server:

$ sudo openssl genrsa -des3 -out server.key 1024

You’ll be asked to enter a pass phrase.

$ sudo openssl rsa -in server.key -out server.key.insecure

You’ll be asked for the pass parse you used in the previous step.

This command generates the certificate you will be asked to fill in some details:

$ sudo openssl req -new -key server.key -out server.csr
$ sudo openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt

The certificate now need coping in to the SSL directory:

$ sudo cp server.crt /etc/ssl/certs
$ sudo cp server.key /etc/ssl/private

Now wee need to enable to SSL site:

$ sudo a2enmod ssl
$ sudo a2ensite default-ssl

The web server needs to be restarted for the change to take place:

$ sudo /etc/init.d/apache2 restart

Now, you should now be able to access the server by typing https://ip_address in your browser.

You’ll get a certificate warning about it not being from a trust source you need to click on more details to check that it is the right computer you are connecting to.

Installing WebDAV

First you need to enable WebDav modules:

$ sudo a2enmod dav_fs
$ sudo a2enmod dav

You need to create a directory to share. For example, it can be in /home/user:

$ mkdir webdav

We need to give the web server and the user access to the WebDav directory:

$sudo chown www-data:user /home/user/webdav

Next we have to setup a password:

$ sudo a2enmod auth_digest

Create a directory where you will store your password files:

$ sudo mkdir /etc/password

Create the password file like this (WebDavCloud is the AuthName, and user1 is actual username for accessing the WebDav):

$ sudo htdigest -c /etc/password/digest-password WebDavCloud user1

You will be asked to type in a password. Select a strong password, you will use it for accessing to your WebDav directory.

Now we need to edit the default-ssl config files:

$ sudo vim /etc/apache2/sites-enabled/default-ssl

You need to find the part of the file that says:

CustomLog /var/log/apache2/ssl_access.log combined

and under that you need to place the following in to the file

Alias /webdav /home/user/webdav
#
<Directory /home/user/webdav/>
 Options Indexes MultiViews
 AllowOverride None
 Order allow,deny
 allow from all
</Directory>
#
<Location /webdav>
 DAV On
 AuthType Digest
 AuthName "WebDavCloud"
 AuthUserFile /etc/password/digest-password
 Require valid-user
</Location>

Now you need to restart the Apache:

$ sudo /etc/init.d/apache2 restart

You should now be able to access the WebDAV folder by going to https://ip_address/webdav

Note: If you would like to map your WebDav directory as a network drive from Windows, it’s most likely that you will have big problems. If you want to use WebDav from Windows, you will need to buy a commercial certificate and use it instead of self-generated certificate.

 


If you liked the post, we should get connected - follow me on Twitter

How to display load average in terminal titlebar

If you’ve managed a number of Linux servers, then you probably know what it’s like to have several terminal windows running top on your desktop.

There is a better way to do this. You can display load average in real time in your terminal titlebar.

Use this script:

#!/usr/bin/perl -w
use strict;
$|++;
my $host=`/bin/hostname`;
chomp $host;
while(1) {
open(LOAD,"/proc/loadavg") || die "Couldn't open /proc/loadavg: $!\n";
my @load=split(/ /,<LOAD>);
close(LOAD);
print "\033]0;";
print "$host: $load[0] $load[1] $load[2] at ", scalar(localtime);
print "\007";
sleep 2;
}

Save this as a script called tl, and copy it to your ~/bin directory.

When you’d like to have titlebar replaced with the name, load average, and current time of the machine you’re logged into, just run tl&

When your job is finished, just do: killall tl before logging out.
Or, if you want to automate this, you can try:

$echo 'killall tl > /dev/null 2>&1' >> ~/.bash_logout

That’s all.

 

 


If you liked the post, we should get connected - follow me on Twitter

How to “cd” into a directory with name like “-test”

If you’ll ever have a situation that you need to enter into a directory with a name like “-test” or any other starting with “-“, this is a post for you.

Solution: You need to use — argument.

So, one way to do this is:

cd -- -test

Other way to do this is to specify the path explicitly:

cd ./-test

Btw, if you would like to delete a directory called like this, you should do next:

rm -r -- -test

If you liked the post, we should get connected - follow me on Twitter