How to redirect users to current page after login

How can you redirect users to the page that they were on before logging in? Right now, the login takes you to the homepage. I found this piece of code below, but not sure if it works or where to put it. Thanks!

<a href="<?php echo wp_login_url(get_permalink()); ?>" title="Login">Login to view</a>