简体   繁体   English

如何在Vim中永久显示当前文件的路径?

[英]How can I permanently display the path of the current file in Vim?

I know CTRL g displays the current file you're working on. 我知道CTRL g会显示您正在处理的当前文件。 Is there a way to modify my .vimrc such that the filename/path is always displayed? 有没有办法修改我的.vimrc ,以便始终显示文件名/路径?

In your statusline, add a %F to display the full path: 在状态行中,添加%F以显示完整路径:

:help statusline

" Add full file path to your existing statusline
set statusline+=%F

Note, %F will be the full path. 注意, %F将是完整路径。 To get a path relative to the working directory, use %f . 要获取相对于工作目录的路径,请使用%f

If your statusline is not already visible, you may first need to configure it to be always visible, via laststatus=2 如果您的状态行尚未显示,则可能需要首先将其配置为始终可见,方法是laststatus=2

set laststatus=2

See :help laststatus for what the options mean. 请参阅:help laststatus了解选项的含义。 Normally, the statusline may be hidden, or hidden unless multiple buffers are open, but I find it extremely useful to have on all the time with customizations like this, well worth giving up one screen line reserve for it. 通常情况下,状态行可能会被隐藏或隐藏,除非打开多个缓冲区,但我发现一直使用这样的自定义项非常有用,非常值得为它放弃一个屏幕行保留。

set ls=2 设ls = 2

add this in vimrc, and you will see the file name at the bottom always. 在vimrc中添加它,您将始终在底部看到文件名。

I found 2 ways to display the file path in the Title bar of the gnome-terminal while editing a file with Vim. 我在使用Vim编辑文件时找到了两种在gnome-terminal标题栏中显示文件路径的方法。

The simpler (and better) way: Add the following line to your ~/.vimrc : 更简单(更好)的方法: ~/.vimrc添加到~/.vimrc

set title

Which will show you at the top: 哪个会在顶部显示:

filename.ext (~/path_to_directory_where_your_file_is/) - VIM

The more complicated way will show you the absolute file path. 更复杂的方式将显示绝对文件路径。 It's documented in a bit more detail in this blog post I recently wrote. 我最近写的这篇博客文章中有更详细记录

The only way I found to get the full path of the file I'm working in is: :echo expand('%:p') . 我发现获得我正在处理的文件的完整路径的唯一方法是:echo expand('%:p') You can re-map ctrl+g if you want, but I personally don't like shifting away from the standards too much. 如果你愿意,你可以重新映射ctrl + g,但我个人不喜欢过多地偏离标准。 I've mapped F7 like so: 我像这样映射F7:

map  <F7> <Esc>:echo expand('%:p')<Return>

I've always used :f , but the answer and links from @MichaelBerkowski are amazing! 我一直用:f ,但@MichaelBerkowski的答案和链接都很棒!

:f shows the path, line count, modified state, current cursor position, and more... :f显示路径,行数,修改状态,当前光标位置等...

I didn't know about CTRL G but it appears to be about the same. 我不知道CTRL G,但看起来差不多。

The statusline is very powerful and handy I think. 我认为状态线非常强大且方便。 Strait out of the box it will display filename, cursor position and some flags. 开箱即用的Strait它将显示文件名,光标位置和一些标志。 But you want to do the same as me and replace the filename -part with the full path to the file. 但是你想和我一样做,并用文件完整路径替换文件名 -part。

So while editing my .vimrc my statusline could look something like this as default: 因此,在编辑我的.vimrc我的状态行看起来像默认情况:

.vimrc                                                        26,16           7%

You could view your setting of the statusline with: 您可以使用以下方式查看状态行的设置:

:set statusline?

But if you have not made any alterations and no module has changed it it would be empty. 但是如果你没有进行任何改动而且没有任何模块改变它,它将是空的。 But by the examples in the help-section ( :help statusline ) you could find that the default is: 但是通过help-section( :help statusline )中的示例,您可以发现默认值为:

:set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P

So include this in your .vimrc and change %f to %F . 因此,请在.vimrc包含此内容,并将%f更改为%F I also added added the filetype flag ( %y ) to my statusline since I find it convenient. 我还添加了filetype标志( %y )到我的状态行,因为我发现它很方便。 So my resulting configuration looks like this: 所以我的结果配置如下:

:set statusline=%<%F\ %h%m%r%y%=%-14.(%l,%c%V%)\ %P

And the result would look something like this: 结果看起来像这样:

~/.vimrc [vim]                                                26,16           7%

Good reading: 好读:

PS. PS。 I run vim 7.3 我运行vim 7.3

If you are using vim-airline , put in .vimrc : 如果您使用的是vim-airline ,请输入.vimrc

let g:airline_section_c = '%<%F%m %#__accent_red#%{airline#util#wrap(airline#parts#readonly(),0)}%#__restore__#'

This is a modification of the airline default, changing %f by %F . 这是对航空公司默认值的修改,将%f更改为%F

If you want the path to include resolved symlinks, use the following: 如果希望路径包含已解析的符号链接,请使用以下命令:

set statusline +=%{resolve(expand('%:p'))}\ %*

To keep the '~' abbreviation for your home directory, include fnamemodify 要保留主目录的“〜”缩写,请包含fnamemodify

set statusline +=%{fnamemodify(resolve(expand('%:p')),':~')}\ %*

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

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