DEV Community

jovica
jovica

Posted on

3 Little Known But Useful Vim Tips

I'll show you three tips which can be incredibly useful, yet, they are very little known.


Tip #1 - Open/edit/save archives

Running a command like:

$ vim archive.tar.gz

will open the archive.tar.gz, and list the contents of the archive. From there, you can open, edit and save changes in these files, without the need to extract them first.

How cool is that? :)

Supported archives: tar.gz, tgz, zip, jar, etc.

Tip #2 - Edit files remotely

Vim has the ability to edit files remotely, over the network. This feature comes with the netrw plugin. To achieve this, netrw uses the SSH protocol, and manages remote files via the scp command.

Here's how to do it:

vim scp://user@myserver[:port]//path/to/file.txt

Note the double / for the directory on the remote host, which is needed to correctly resolve the absolute path. [:port] is optional.

So with the command above you can open a file located on a remote host for editing.

What actually happens in the background is that Vim uses scp to download the requested file from a remote machine to a local /tmp directory, and then opens it for editing.

When you save your changes to the file, the changes are first applied to a local copy in /tmp directory. After that, the file is uploaded via scp to the remote host.

Tip #3 - Magic

Did you know that you can open the last edited file on the machine with cursor on the latest location?

Just start Vim, hit Ctrl+o+o - and there you go.

Keep hitting o and see what happens ;)


Now, if you wanna discover more Vim tips you've never heard of, take a look at my book Mastering Vim Quickly: From WTF to OMG in no time

Top comments (10)

Collapse
 
primercuervo profile image
Nicolas Cuervo

I always enjoy some good vim tips!

I blame my memory because I already knew about the 'ctrl+o' one, but otherwise I wouldn't have this feeling of rediscovery.

I've followed the twitter account from MasteringVim for a long while and it's fantastic. Thanks a lot for your work!

Collapse
 
jovica profile image
jovica

Thanks for the kind words Nicolas! :)

For the others, it's this account: twitter.com/masteringvim

Collapse
 
toyo_tomi profile image
Sacquer
Ctrl + i / Ctrl + o
Enter fullscreen mode Exit fullscreen mode

forward / backward

Give us more @jovica !

Collapse
 
janacm profile image
janac meena

Was not expecting the title to be accurate!

Collapse
 
maestromac profile image
Mac Siri

Interesting tips!

Collapse
 
andytower_rus profile image
Andrey Alferov

I knew 1 and 2 tips, but tip #3 is surprising me.

Collapse
 
alvarocaceresmu profile image
Álvaro Cáceres Muñoz

Amazing! Thanks for sharing

Collapse
 
ondrejs profile image
Ondrej

Cool tips, thanks!

Collapse
 
sksundram profile image
Shailesh Kumar

The third tip is dope!

Collapse
 
gabgab2003 profile image
DownloadPizza

I knew the zip, I could have guessed the scp. But holy Lord the ctrl-o is a godsend