Chase Seibert
Chase Seibert

Reputation: 15851

Vimrc using a variable inside a cmap

I'm writing a piece of vimscript to manage sessions. I would like to let the user configure which directory they would like the sessions to be saved in, ie:

let g:sessions_dir = "~/.vim/sessions"

I have a number of commands that let the user create and load sessions. But I would also like to provide a cmap that let's the user tab through the sessions in that directory.

cmap GSo :wa<CR> :source ~/.vim/sessions

This works, and the user can hit TAB to cycle through their session files. However, I want to use that g:sessions_dir variable in the cmap.

Upvotes: 4

Views: 316

Answers (3)

Carson
Carson

Reputation: 8138

another example:

let g:work_dir='C:\...\'  " define a new global variable work_dir
:cmap <F1> Explore <C-r>=g:work_dir<CR>

when you click F1 on the command-line, then output :Ex C:\...\

or

:cmap WK_DIR Explore <C-r>=g:work_dir<CR>

Once you were typing WK_DIR on the command line, the result is the same as above.

Upvotes: 0

ZyX
ZyX

Reputation: 53674

You can use <C-r>= here:

cnoremap GSo :wa<CR> :source <C-r>=g:sessions_dir<CR>

(last <CR> is for <C-r>=, it won’t launch the command).

Upvotes: 3

Conner
Conner

Reputation: 31110

You can put the cmap within an execute. For example,

exe "cmap Gso :wa<CR> :so ".g:sessions_dir

Upvotes: 4

Related Questions