简体   繁体   English

每个文件的Vim映射

[英]Vim per file mappings

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. 但是Vim总是抱怨map ,说它是无效的。 I also tried nnoremap with no success. 我也尝试了nnoremap ,但没有成功。 What should I do to fix this (I want it in this file only)? 我该怎么做才能解决此问题(仅在此文件中需要它)?

Modelines are only for options. 模式行用于选项。

If you want this mapping only for that file, add this snippet to your ~/.vimrc : 如果只需要该文件的映射,请将此代码段添加到~/.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. 如果是这样,那么您在这里确实是在追逐错误的兔子,并且在XY问题墙上也确实崩溃

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 : 因此,您可以简单地将此通用映射放入~/.vimrc

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

Note 1: See :help c_% for the meaning of % and in that context. 注1:请参见:help c_%的含义% ,并在这方面。

Note 2: The bar needs to be escaped when used in a mapping, see :help map_bar . 注意2:在映射中使用时,该栏需要转义,请参见:help map_bar

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM