jovica.org

vim

5 articles
Home > Categories > vim

Vim Swap and Backup Files Dilemma

Sooner or later you’ll notice that, when you edit files, Vim creates files named like .filename.swp in the same location as the file you’re editing. These files are called swap files.

Swap files

Swap files store changes you’ve made to the buffer. If your Vim crashes, a swap file will allow you to recover those changes. Another important role of swap files is to act as a lock mechanism: if you open a file, which is already opened in another Vim session, you’ll be warned. That can be useful, especially on a system with multiple users.

Disabling swap files

You can disable swap files entirely by adding set noswapfile to your .vimrc. However, I’d recommend you not to disable them, unless you really know what you’re doing. Instead, you could organize swap files better.

Swap files organization

Usually the most annoying thing about swap files is that they’re created all around your file system, wherever you edit your files. To solve this, you can save all the swap files in one location.

First, create a directory for storing swap files, for example: $ mkdir ~/.vim/swp

Then, put this snippet in your .vimrc: set directory=$HOME/.vim/swp//

The directory option contains a list of directories where Vim will try to store swap files. The // at the end tells Vim to use the absolute path to the file to create the swap file. This will ensure that swap file name is unique, so there are no collisions between files with the same name from different directories.

Note: My book Mastering Vim Quickly: From WTF to OMG in no time helped thousands of people to drastically improve their Vim skills within an hour! If you'd like to do the same, look it up: ebook + screencasts | paperback

Backup files

Vim can make backups of files you edit, so you’re safe from losing data. I don’t use this Vim feature personally, and I would suggest you set up a better backup solution for your work.

Of course, this feature can be useful. Backups are controlled by the settings of two options: backup and writebackup. If interested, look these up in :help.

Just like for swap files, you can also keep backup files better organized, by creating a directory and adding it to your .vimrc:

set backupdir=~/.vim/.backup//

You're welcome to join my private email list or follow me on Twitter.

Copy/Move/Delete text without the cursor movement in Vim

## Copy lines without cursor movement

Your cursor is on line 10. You want to paste line 20 to one line below your current cursor position. Here’s how to do that using co[py] command: :20co.

You can also use ranges. Let’s say your cursor is on line 10. You want to paste text from line 20 to 25 under line 10. Here’s how: :20,25co10

It gets even better: :t is an alias of the co[py] command, so you could save some keystrokes. You could run the commands from above and achieve the same result if you’d replace co with t in them.

This command works with relative line numbers as well. For example, to paste the line, which is 10 lines above your current line, to a line below your current position: -10t.

One last example: if you’re at line 45, :35,t. will make a duplicate of lines 35 to your cursor (that is, from 35 to 45 inclusive) and put it after your current cursor.

So, imagine this case: you have a function and your cursor is one line below it. You also have relative line numbers enabled. You see that the function starts 15 lines above your current line. To make a copy of the entire function and place it after your current line, you could run: :-15,t.

Note: My book Mastering Vim Quickly: From WTF to OMG in no time helped thousands of people to drastically improve their Vim skills within an hour! If you'd like to do the same, look it up: ebook + screencasts | paperback

## Move lines without cursor movement (that much)

The usage of command m[ove] is similar to co[py] command. For example, to move a line 6 to line 28, you’d run: :6m28

It also supports ranges and relative lines.
Here’s an example using both: :-10,-5m+7

This command would take five lines which are located between lines 10 and 5 above your current position, and move them to 7th line under your current position.

After this command, the position of your cursor will change. In order to come back to the original location where your cursor was before running this command, simply hit ''. Surely, the cursor moves. But, this workflow saves you from moving your cursor around to perform the visual selection.

## Delete lines without cursor movement (that much)

Wouldn’t it be cool to be able to delete lines without moving your cursor?

Similar to :co[py] and :m[ove] commands, you can also run :d[elete] command to delete lines without jumping to those lines.

For example, to delete lines 5 to 10, run: :5,10d

However, this command would leave the cursor at the deleted line location, so you’d need to use '' to jump back to the previous position.


You're welcome to join my private email list or follow me on Twitter.