Мне удалось «решить» проблему с помощью хакерского способа создания пакетного файла, который вызывает команду сценария python, но это действительно немного хак, и я думаю, что будет лучший способ.
I'm trying to find the name of the file I'm editing inside of vim.
For the current buffer this will give you name of the file,
echo "You're editing " bufname("%")
(just put it in some file.vim, and source it ":so %". But I don't think this is what you need.
So I can use it to map F5 to compile this file, for testing purposes. Would of course be great if I could recognize the file format, and choose compiler accordingly, but really not necessary. If I find the name of the file, I could do that myself. But I really can't find any way to get the name of the file I'm editing.
You could do several things. If vim recognizes the filetype you're editing, you could map F5 to your compiler command and put that command in its specific ftplugin directory, so it will be valid only for that filetype. For example, you put
nmap <f5> :!compilername %<cr>
in cpp.vim, fortran.vim, python.vim, etc. The %
gives the name of the current file you're editing. This will execute the compiler just like if you called compilername file.cpp
in the command prompt.
Of course, compilername
will be different for each filetype; you're not going to compile fortran with cpp compiler, you put fortrancompiler %
in that case.
That is one option. You could also try according to extension set autocmd commands in your .vimrc
so it recognizes the filetype according to extension of the file. This is all standard usage of vim, nothing uncommon. Now, I'm not sure how you'd like to go about this, so I'll just point you to Vim Wiki where you can find all kinds of articles that cover this (and also a few tips for some compilers).
I know of the
:make
command, and have already mapped that, but for small scripts/testing programs, I really don't want to first have to write a simple makefile.
Yes, that seems like overkill. Personally (when I'm on Windows), I find simple batch file much easier to write for most things except big projects. If the program is in one or two files, I just compile it using some oneliner mapping.
Вы можете использовать символ% в командах vim, чтобы получить текущее имя файла с расширением. Например,
:! javac%
для запуска javac с текущим файлом.
Вы можете узнать больше с помощью
: help filename-modifiers
Ну, это % в качестве кода подстановки внутри таких вещей, как: w. Итак, : w blah% пишет копию вашего файла с добавлением «blah». Или !! echo% заменяет текущую строку им. Не знаю, отвечает ли это вашим потребностям или нет.
Как уже упоминали другие, %
раскрывается в текущий файл. Если вы хотите получить эту строку в скрипте vim, используйте expand ("%")
.
Но вы можете просто установить makeprg что-то вроде "compiler-command \%"
и запустите : сделайте
- таким образом вы получите поддержку быстрого исправления. (открыть окно ошибок / предупреждений с помощью : copen
)
: copen
) make - таким образом вы получаете поддержку быстрого исправления. (открыть окно ошибок / предупреждений с помощью : copen
) Остальные моменты очень полезны. Я бы добавил, что вы, вероятно, захотите использовать: p, чтобы гарантировать, что имя файла передается как полный путь. Без него Vim может передать его как путь относительно текущего пути Vim, который может быть или не совпадать с путем, используемым компилятором. Итак, одно из решений:
nnoremap <silent> <f5> :!javac %:p<cr>
Если вы хотите определять тип файла автоматически, вы можете использовать AutoCommand
autocmd FileType java nnoremap <buffer> <silent> <f5> :!javac %:p<cr>
autocmd FileType cpp nnoremap <buffer> <silent> <f5> :!gcc %:p<cr>