Get name of the current file in Vim

后端 未结 6 1436
清歌不尽
清歌不尽 2020-12-14 00:52

I\'m trying to find the name of the file I\'m editing inside of Vim. So I can use it to map F5 to compile this file. Would of course be great if I could recognize

相关标签:
6条回答
  • 2020-12-14 01:32

    Well, it's % as a substitution code inside things like :w. So :w blah% writes a copy of your file with 'blah' prepended. Or !!echo % replaces the current line with it. Dunno if that addresses your needs or not.

    0 讨论(0)
  • 2020-12-14 01:32

    Use % as a substitute for your filename. But personally, I really think you can push :make to do exactly what you want.

    I used to do exactly what you want to avoid: just hack out a two-line Makefile for every single folder I was in. I enjoyed :make's quickfix support so much that whipping out a two-line Makefile just seemed like a small overhead in the big picture.

    ... I've since learned better. You can use an autocmd or ftplugin to set the makeprg option, configuring :make to use your command of choice based on the current filetype. In itself, that saves you a bit of work.

    The problem starts when we work a tool whose error messages don't look just like GCC's. To get around this, vim also provides the errorformat option. errorformat is very sophisticated. If you really want to learn about it, start with :h error-format. In the long term, the sophistication is really great. It means that, once you learn how to use it, you can parse virtually any compiler output and format it for viewing with quickfix (:cope or :cl).

    In the short term, it can be a bit of a bad thing, because it means that the first couple of times you hack an error format together yourself, you're going to be spending a fair bit of time puzzling over the vimdocs and re-loading and building files that will trigger the various error messages your compiler can produce. That's definitely not what you want if you're only briefly working outside your comfort zone, or your boss is over your shoulder urging you to fix this bug now.

    Hopefully (and likely), someone has already gone through this pain for you, and either configured errorformat appropriately for your default ftplugin, or released it as part of a vim script that they're just waiting for the community to install and adore.

    As a quick demonstration, here's a couple of lines I hacked together for my .vim/ftplugin/haskell.vim file:

    setl makeprg=ghc\ --make\ %
    setl errorformat=%E%f:%l:%c:,%C\ %.%m,%Z,%f:%l:%c:%m
    

    I use setl here so that these options are only configured for haskell source files; if I edit a different kind of source file simultaneously, it can use a different configuration instead.

    If you end up using :make, I also recommend configuring your switchbuf setting. I found it a bit annoying having to press ctrl-O after a build if quickfix gets excited and jumps you to the wrong file. I use:

    set switchbuf=useopen,usetab,newtab
    
    0 讨论(0)
  • 2020-12-14 01:38

    The other points are very useful. I would add that you probably want to use :p, to ensure that the file name is passed as a fully qualified path. Without it, Vim may pass it as a path that is relative to Vim's current path, which may or may not be the same path as the compiler uses. So one solution is:

    nnoremap <silent> <f5> :!javac %:p<cr>
    

    If you want to detect the file type automatically you could use AutoCommand

    autocmd FileType java       nnoremap <buffer> <silent> <f5> :!javac %:p<cr>
    autocmd FileType cpp        nnoremap <buffer> <silent> <f5> :!gcc %:p<cr>
    
    0 讨论(0)
  • 2020-12-14 01:45

    As others have already mentioned, % is expanded to the current file. If you want to get that string in vim script, use expand("%").

    But you may want to simply set makeprg to something like "compiler-command\ %" and run :make - you gain quick-fix support that way. (open errors/warnings window with :copen)

    0 讨论(0)
  • 2020-12-14 01:53

    You can use the % character in vim commands to get the current filename with the extension. For example,

    :!javac %

    to run javac on the current file.

    You can find out more with

    :help filename-modifiers

    0 讨论(0)
  • 2020-12-14 01:59

    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.

    0 讨论(0)
提交回复
热议问题