Question

I tried to put the following in my file header:

#!/bin/sh
# vim:set ts=2 sts=2 sw=2 expandtab:
# vim:map <leader>t :w\|:!./script.sh <cr>:

But Vim always complains about the map, saying that it's invalid. I also tried nnoremap with no success. What should I do to fix this (I want it in this file only)?

Was it helpful?

Solution

Modelines are only for options.

If you want this mapping only for that file, add this snippet to your ~/.vimrc:

augroup ThisFile
  autocmd!
  autocmd BufRead,BufNew /path/to/file nnoremap <buffer> <leader>t :w\|:!./script.sh <cr>:
augroup END

edit

It looks like you want a mapping for executing the current file. If so, you are really chasing the wrong rabbit, here, and also crashing real hard in the XY Problem wall.

You can use % as a synonym for "the file associated with the current buffer" so, assuming your script is executable, this command would execute it:

:w|!./%<CR>

Therefore, you could simply put this generic mapping in your ~/.vimrc:

nnoremap <leader>t :w\|!./%<CR>

Note 1: See :help c_% for the meaning of % and in that context.

Note 2: The bar needs to be escaped when used in a mapping, see :help map_bar.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top