brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: :mksession saves your Vim windows and files, and vim -S brings them back

Vim
~/tips/tip-vim-mksession-restore-layout

:mksession

Saves the open files and window layout to a file so you can restore it later.

A Vim session with five files in splits is a small work area. Closing the terminal shouldn’t mean rebuilding it from memory, and Vim has a feature for that.

:mksession

This writes a script called Session.vim in the current folder. It describes the open buffers and the window layout, and depending on the sessionoptions setting also things like the current folder and some options. Use :mksession! mywork.vim to pick another name and overwrite an existing file.

To restore it, start Vim with the session file:

vim -S Session.vim

The docs say -S alone, as the last argument, works like -S Session.vim, so vim -S in that folder is enough. From inside Vim, :source Session.vim does the same.

The file is not updated by itself. When you open more files, run :mksession! again to refresh it. A caveat: a session stores file paths, not content, so unsaved changes are not in it. If a file moved or was deleted, Vim reports an error when loading. And keep Session.vim out of version control, for example by listing it in your .gitignore.

Further reading: Vim help: starting, :mksession

Going deeper: Guide: advanced tmux, a config that grows with you, formats, layouts and more

More Vim tips :%s/<Ctrl-r><Ctrl-w>/newname/g /oldname find . -name "*.conf" | vim – vim --clean file.txt :set textwidth=72 All Vim tips (45) All topics

Link