How to show constantly current working directory in vim - vim

A very simple question, is there some way to show constantly the current working directory at the bottom of the file opened by vim? Thanks.
Note this is a different question as here:
How can I permanently display the path of the current file in Vim?
There we want to show the full path of the current file viewed, but what I am thinking is to show both, maybe two lines on the bottom of file: one for full path currently viewed; the other for the current working directory. These two could be different as we can open a file that's not in the current working directory.

You can do it by adding these lines in your .vimrc
set laststatus=2
set statusline=%!getcwd()
The first setting is to configure statusline to be always visible, second one is to add a current working directory to statusline.
More on
:h laststatus
:h statusline
:h getcwd()
Edit: This is an answer to OP's changed question:
It is not possible to have two-line statusline (at least to my knowledge). But it is possible to add e.g. some formatting. So
set laststatus=2
set statusline=%F
set statusline+=%=
set statusline+=%{getcwd()}
To further study I recommend to read help I mentioned before. Also this question might be of some interest.

Related

Can not switch to previous page in gvim

I have a very weird problem which did not exist in my environment before but now happened. When editing files, I'm used to doing this way:
gvim . #open current directory browsing
scroll up/down to select file, enter then edit.
ctrl-6 #back to previous directory
However one day I found the step3 was failed, it said "No alternate file".
My .vimrc file only contains:
colorscheme darkblue
set number
set autoindent
set nowrap
set ignorecase
set cursorline
I tried to clean all content in .cshrc but the same.
Can anyone tell me what's wrong with my gvim ?
Thanks in advance.
This looks to be a change in behavior introduced in Vim 7.4. The netrw view is no longer stored as an alternate file. See this discussion.
The Vim maintainers seemed split on what the correct behavior should be, but Bram himself offered up this advice with a mapping:
I do realize that editing the directory of the current file is
something I often do, but I never bothered to set up a mapping for
it. Typing ":e %:h" is not too difficult, but CTRL-O to jump back to
the netrw %directory view was easier.
I now added a mapping:
map ,d :e %:h<CR>
Let's see if I can get used to that.
Another related discussion can be found here.
You are likely using netrw to edit a directory. This is the equivalent of :Explore. Sadly netrw has a bad habit of not maintaining the alternative buffer, #. You maybe able to upgrade netrw or use :Rexplore (:Rex for short) to resume exploring.
Another option is to just use :e with wildcards and tab completion to explore files. Use <c-d> to list out completions as well.

set tags=tags in gvimrc not working, unless set it explicitly

I have a problem to set my tags file correctly. It use to work without problem after I reinstalled the system. error message like
E433: No tags file
E426: tag not found: Pids
accurs when I press ctrl+].
I have this line in my .gvimrc file
set tags=~/projectdirectory/tags
and tags-exuberant installed properly.
It works fine when I type :set tags=~/projectdirectory/tags in gvim
I also tried use set tags=~/projectdirectory/tags;/
All other .gvimrc settings function well. How this could be possible?
UPDATE:
I have solved the problem, it is because I have multiple tags setting in ~/.gvimrc, vim take the last one in current session.
You can check the actual effective value (after starting GVIM) via
:verbose set tags?
The option might have been overwritten by a later :set command, or a plugin.
Even if you only use GVIM, it's recommended to put the general settings into ~/.vimrc (which is also sourced in GVIM), and keep ~/.gvimrc reserved for GUI-specific settings. An important difference between the two is that the latter is only sourced at the very end, so it's unsuitable for configuring plugins.

Vim Statusline - Virtualenv Function

I have just add virtualenv to vim. I want to have the active virtualenv show in the statusline so I know which environment I am in.
From Virtualenv help
g:virtualenv_stl_format
Format string for the statusline
Example:
let g:virtualenv_stl_format = '[%n]'
To sue the statusline flag, this must appear in your 'statusline' setting
%{virtualenv#statusline()}
So I went searching for the statusline and found
writing a valid statusline
They are going way beyond what I am trying to do and I really don't understand. I just want to simply add %{virtualenv#statusline()} to whatever line it is I have now, how do I do that?
The plugin I am referencing is this one https://github.com/jmcantrell/vim-virtualenv#readme
In its simplest form, your statusline could be reduced to this line in your ~/.vimrc (note the =):
set statusline=%{virtualenv#statusline()}
If you want that information to be displayed at the end of your current statusline, paste the following line in your ~/.vimrc (note the +=):
set statusline+=%{virtualenv#statusline()}
Or to place it at the beginning (note the ^=):
set statusline^=%{virtualenv#statusline()}
If you want to place this information at some arbitrary position in your custom statusline, you'll simply need to edit the corresponding line(s) in your ~/.vimrc. But you already know how to do that since you already have a custom statusline, do you?
If you use the default statusline, you'll need to replicate it as per the example given in :help statusline:
set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P
and place the virtualenv snippet at the desired location, say after the status flags:
set statusline=%<%f\ %h%m%r%{virtualenv#statusline()}%=%-14.(%l,%c%V%)\ %P
But all that is clearly explained in :help statusline.

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

I know CTRLg displays the current file you're working on. Is there a way to modify my .vimrc such that the filename/path is always displayed?
In your statusline, add a %F to display the full path:
:help statusline
" Add full file path to your existing statusline
set statusline+=%F
Note, %F will be the full path. To get a path relative to the working directory, use %f.
If your statusline is not already visible, you may first need to configure it to be always visible, via laststatus=2
set laststatus=2
See :help laststatus for what the options mean. 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
add this in vimrc,
and you will see the file name at the bottom always.
I found 2 ways to display the file path in the Title bar of the gnome-terminal while editing a file with Vim.
The simpler (and better) way: Add the following line to your ~/.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.
If you are using vim-airline, put in .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.
The only way I found to get the full path of the file I'm working in is: :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. I've mapped F7 like so:
map <F7> <Esc>:echo expand('%:p')<Return>
I've always used :f, but the answer and links from #MichaelBerkowski are amazing!
:f shows the path, line count, modified state, current cursor position, and more...
I didn't know about CTRLG but it appears to be about the same.
The statusline is very powerful and handy I think. Strait out of the box it will display filename, cursor position and some flags. But you want to do the same as me and replace the filename-part with the full path to the file.
So while editing my .vimrc my statusline could look something like this as default:
.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:
:set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P
So include this in your .vimrc and change %f to %F. I also added added the filetype flag (%y) to my statusline since I find it convenient. 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:
The documentation
An excellent book to learn the basics
There is lots here on stackoverflow!
PS. I run vim 7.3
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
set statusline +=%{fnamemodify(resolve(expand('%:p')),':~')}\ %*

Horizontal line in vim

What does a horizontal line in vim mean? While editing a remote file I see a horizontal line in the current line.
I don't see it while editing local files
Edit:
cursorline is not shown until I save the file(:w). When I type :w and enter password, cursorline appears. Why does it have such a behavior? When I edit file in remote machine cursorline is turned off and is not shown.
As others have answered, the effect is probably being caused by the cursorline option.
You can track down what is script made the most recent change to an option by running the command set optname? under the verbose command:
:verbose set cursorline?
You will probably just find that the Netrw plugin set it; Netrw handles local directory browsing and remote directory/file access like your scp:// example. Netrw adjusts cursorline (and cursorcolumn) for its own purposes (e.g. directory listings), but it tries to restore the value to the “user value”. Unfortunately, its idea of the “user value” is captured when part of the Netrw code loads and is not updated afterwards.
My guess is that, somehow (via some other plugin, or bit of configuration), cursorline is set when Netrw loads (and captures its value), but it is later reset by the time you start editing the first file. Then, when you later save the file (:w), Netrw restores the “captured” value. Unfortunately, there does not appear to be any good way to update this “captured” value of the cursorline option (there is no “external” access to the script variable it uses, and it does not “recapture” if you manually reload the file).
What you can do, however, is explicitly load the bit of Netrw that “captures” cursorline when your desired value is active. You could do that with the following two commands early in your ~/.vimrc (possibly at the very top, if necessary—it needs to be before the first time autoload/netrw.vim would ever be used):
set nocursorline
runtime autoload/netrw.vim " will 'capture' cursorline and cursorcolumn values
Netrw will still set/reset cursorline (and cursorcolumn), but as long as the value you normally want matches the value that is active right before Netrw is loaded, then you will not notice it.
I don't quite know the simplest solution to get get netrw to really capture the right value, but at very least (since for me, nocul is right,) adding:
let g:netrw_cursorline=0
To the end of my ~/.vimrc seems to have fixed the problem for me. Hope this helps someone!
It's the cursorLine. Its appearance is defined in your colorscheme. The one on the remote machine is probably different than yours or there's a mismatch between your client/server's $TERM.
Yup, ":set cursorline" or ":set nocursorline" to turn the line on or off.
The command "vim scp://...." copies the remote file onto your local machine (i.e. machine where the "vim" process runs), then opens the file in "vim" for you to edit, then, if you have modified the file, copies the file back onto the remote system. As such, syntax highlighting etc. is determined only by "vim" on your local machine.
Files with the same syntax type (":se syn" to show current syntax highlighting scheme) are highlighted the same way. Do the files, in which you see the difference, have the same syntax type?
Version 142 of netrw.vim has fixed this bug, at least for me (vim 7.3, old netrw.vim version was 140, running under cygwin).
You can get the latest version here: http://www.vim.org/scripts/script.php?script_id=1075

Resources