Skip to main content
OverflowAI is here! AI power for your Stack Overflow for Teams knowledge community. Learn more
12 votes

Remember previous session

Vim has a built-in session mechanism, from :h session: A Session keeps the Views for all windows, plus the global settings. You can save a Session and when you restore it later the window layout ...
statox's user avatar
  • 50k
7 votes

How can I restore the cursor position when opening a file?

The vim-lastplace plugin does exactly what you want (I am the author). It improves on the above code snippet by handling commit messages intelligently. If you have debian-testing you can apt-get ...
Greg Dietsche's user avatar
7 votes

Can I open a file in an existing Vim instance from an external command?

While @craigp's answer is correct, I found it most convenient to simply add --remote-silent without worrying about server names,(this assumes you don't want to address named vim instances). This will ...
ideasman42's user avatar
  • 3,743
7 votes

How to save quickfix list per Neovim session using :mksession?

Quickfix lists (shortened to "qflist" from here on) are not saved with :mksession. Probably because the list is saved in a temporary file which is scrubbed when exiting the current vim ...
3N4N's user avatar
  • 5,754
6 votes
Accepted

Source vim session file: Track down unexpected sourcing of other file

See :h :mksession, the 10th point: If a file exists with the same name as the Session file, but ending in "x.vim" (for eXtra), executes that as well. You can use *x.vim files to specify ...
VanLaser's user avatar
  • 9,750
6 votes

Make Vim sessions save the content of unnamed buffers

You could use the following function. It is extremely hacky and I haven't tested it plenty but it seems to work. The idea is to create a command :Mksession which will work like the original :...
statox's user avatar
  • 50k
5 votes

How can I read a command-line argument in vimrc?

Vim version 8.1.2233 adds the read-only variable v:argv, which can be used to parse the command line arguments given to Vim.
Christian Brabandt's user avatar
5 votes
Accepted

Can I retain and restore all the GUI windows and its files on launch on Vim?

As I mentioned in my comments a session is associated with a single invocation of Vim so you can't do exactly what you're hoping to do. But I also mentioned something might be possible using shell ...
B Layer's user avatar
  • 19.9k
5 votes
Accepted

Remapping of j as gj etc in .vimrc is not applied in a session

Mappings defined with <buffer> are only effective in the buffer that's active when they are defined. Remove <buffer> to have them work more-or-less everywhere. See :help map-<buffer> ...
Antony's user avatar
  • 2,580
5 votes

Close session and all buffers with one command

You can use: :qa for quit all or :qa! The later forces to quit Vim even if some buffer modifications have nor been saved.
Vivian De Smedt's user avatar
4 votes

How does one perform a multi-line copy and paste between two sessions in Vim?

I know this is old but I came across it and thought a different option might be useful for other people searching for this question. I would yank to the system register and then open the new file and ...
ojunk's user avatar
  • 143
4 votes
Accepted

! is required but not allowed for custom command

E189 "..." exists (add ! to overwrite) is reported by :h :mksession if session file exists , add ! after mks should fix that: exe 'command Sdfs mks!' sessionlocation If you want to pass ! from Sdfs ...
dedowsdi's user avatar
  • 6,268
4 votes
Accepted

Change saved session's working directory

The behavior of :mksession is driven by the flags you have set in 'sessionoptions' (alias 'ssop'). The two flags that are likeliest to be factors here are curdir and sesdir. The former is simple, the ...
B Layer's user avatar
  • 19.9k
4 votes
Accepted

what is the purpose of the view folder in the .vim folder?

~/.vim/view is a default value (under *nix) for :h 'viewdir' option, which, in turn, is used by the :h :mkview and :h :loadview commands. :mkview is a rarely used :mksession-like command which saves ...
Matt's user avatar
  • 20.8k
3 votes

How to share the same buffer between multiple vim instances

If you are still looking for an answer and willing to try neovim, the neovim-remote does what you need. From the readme: Open files always in the same nvim process no matter which terminal you're ...
Marco Crivaro Nicolini's user avatar
3 votes
Accepted

Empty first tab lost when using sessions

This is indeed a bug in Vim. I filed a GitHub issue, and someone on the vim-dev mailing list addressed it. Here's a patch that fixes it in Vim 8.0.52: diff --git a/src/ex_docmd.c b/src/ex_docmd.c ...
Jim Stewart's user avatar
3 votes

Ignore word spelling on a per-file/buffer basis

There's nothing built-in; I guess the reasoning is that spelling (at least for a certain language) is inherently global. Any differences should be reflected in the language / dialect chosen. You can ...
Ingo Karkat's user avatar
  • 17.9k
3 votes
Accepted

Using '-c' when calling vim from terminal

The issue is that you need to escape your space: alias viml='vim -c :SLoad\ zzz' Let's consider a simpler case: I have a file named a.vim in the current directory and my alias is: alias vv="vim ...
nobe4's user avatar
  • 16.1k
3 votes

How to save window, split, and buffer layout?

You can try the vim-workspace plugin, its session management features are automated, scoped for your current working directory, and is pretty simple (only need to run ToggleWorkspace once).
jaybay's user avatar
  • 91
3 votes

Is there a `:browse oldfiles` for vim sessions?

There's a single :h v:oldfiles list saved to / restored from :h viminfo. And it has nothing to do with :h session-file. On the other hand, "session" does not store "oldfiles"-sort ...
Matt's user avatar
  • 20.8k
3 votes

Is it by design that opening with -S doesn't account for changes in file permission?

Session is simply a VimScript file. You can read through it and see what is going on. What it does, it re-creates buffer(s) and after that it attempts to restore all previously saved options. By doing ...
Matt's user avatar
  • 20.8k
3 votes

Can the default filename for :mksession be changed?

Fortunately it is relatively easy to compile vim to use a different name if desired: edit src/feature.h to change #define SESSION_FILE "Session.vim" to whatever name you want. Then ...
D. Ben Knoble's user avatar
  • 26.4k
2 votes

Can I open a file in an existing Vim instance from an external command?

In addition to @craigp's answer, you can also open a file in a remote tab. vim --servername SERVER --remote-tab FILE
Tyhou's user avatar
  • 121
2 votes

Make Vim sessions save the content of unnamed buffers

Vim workarounds: write temporary notes/code in predefined system-wide or project-wide files (possibly with the help of some custom functions/mappings) use a Vim note-taking plugin In both cases ...
Jerome Dalbert's user avatar
2 votes

Make Vim sessions save alternate files

Since there is no option to do this directly, you can hack around like this: function! MkSession(filename) execute 'mksession! '.a:filename let l:alternate = fnameescape(expand('#')) if l:...
nobe4's user avatar
  • 16.1k
2 votes

Can capitalized marks be saved in session INSTEAD of viminfo?

Is there an easy setting to make this switch? Nope I know I could write some vimscript to save my session marks somewhere and reload them to overwrite whatever viminfo is doing, but I don't want ...
Peter Rincker's user avatar
2 votes
Accepted

Update a Vim Session file's loaded plugins without losing window positioning

With the help of @Matt, we were able to get my new plugins loaded, and markdown folds working. Open vim with the session file vim -S Session.vim Modify sessionoptions by removing options and folds ...
FilBot3's user avatar
  • 153
2 votes
Accepted

How to preserve options in Vim sessions?

Thanks for pointing to sessionoptions, this answered my question like this: Step by step: nvim file1.txt :vsp file2.txt A perfect environment to compare files. Note: it is necessary to apply each ...
Lionel Hamayon's user avatar
2 votes

When opening an existing session and an existing file at the same time, the focus goes to the last opened file in the session

You can cheat a little using this method: vim -S Session.vim -c ":e OTHER_FILE" This will source your Session.vim then load the file you want. From what I have read in the man page, the ...
Fabrice Hategekimana's user avatar
2 votes
Accepted

Simple way to store additionnal config in the scope of the actual directory

Off the top of my head perhaps something like this in your vimrc... let g:localconffile = '.myvimconf' func! LoadLocalConfig() abort let l:fname = expand('%:p:h') . '/' . g:localconffile if ...
B Layer's user avatar
  • 19.9k

Only top scored, non community-wiki answers of a minimum length are eligible