Vim Statusline - Virtualenv Function - vim

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.

Related

How to change specific 'theme' colors in vim

I would like vim to roughly match the color scheme of a python file in textmate. Here is a comparison of the two:
For example, I want a comment to be blue instead of red. If I have the hex codes for each of these colors, is there a place that I can change this in the vimrc or somewhere else? For example, how would I pass a hex code into the vim colorscheme:
" syntax highlighting
hi Comment cterm=NONE ctermfg=#ddd gui=NONE guifg=#ddd
hi Constant cterm=NONE ctermfg=DarkGreen gui=NONE guifg=green3
I was able to achieve what you wanted in two different ways, the first one was much simpler, I just added
hi Comment guifg=#ddd
into my vimrc just after the colorscheme [colorscheme name].
The second method is better for a big amount of changes and it's much harder. It bases around changing your colorscheme. You would do as follows:
Check what's your current colorscheme, by typing the command :colorscheme while in vim. if it's one of the defaults (blue.vim, darkblue.vim, default.vim, delek.vim, desert.vim, elflord.vim, evening.vim, industry.vim, koehler.vim, macvim.vim, morning.vim, murphy.vim, pablo.vim, peachpuff.vim, ron.vim, shine.vim, slate.vim, torte.vim, zellner.vim), then you need to go to $VIMRUNTIME/colors and edit which is yours. Otherwise check your ~/.vim directory for "colors" or search where you store plugins for the name of your theme. For example if you use vim-plug then plugins and themes are stored in ~/.vim/plugged. Copy the folder to not mess up the original theme and use a different name.
2.After opening the folder of your theme open the only file in it - [theme].vim and search for the item you want to change, e.g. "Comment", change the hex value of the color, background etc.
Repeat to your liking to the time you'll get your theme looking how you want it to.
Set your colorscheme via colorscheme [name-you-picked-earlier]
The second option is also useful for creating full themes suited just to you.

Set text color to a string in statusline

I'll preface by saying that I am using Airline, plytophogy/vim-virtualenv plugin and gruvbox as the Airline theme.
I am trying to change the color of some string in the status line to, say, orange.
In my case that is the "#my_env" string that states my current working environment:
Currently, the line in my .vimrc that is responsible for this text is:
let g:airline_section_c='%t #%{virtualenv#statusline()}'
Based on this question, I tried to change my .vimrc line to
let g:airline_section_c='%t %#orange##%{virtualenv#statusline()}'
which seems to have highlighted the statusline from "#my_env" onwards:
It seems that using %#any_color# or even %##by itself produces the same effect.
I have read through all :h 'statusline' and did not seem to find an appropriate solution.
So my question is: Is there a way to set a color for a string in the statusline?
EDIT:
The question has been answered here
It seems that there is no way to change the text color of the screen without changing its entire formatting group (including the background).
Appending %#airline_c# to the end of the string to addition fixed the highlighting issue though:
This is what it looks like with
let g:airline_section_c='%t %#Special#%{virtualenv#statusline()}%#airline_c#'
where Special is a random highlighting group
In :help statusline, in the description of the formatting, you can find:
* - Set highlight group to User{N}, where {N} is taken from the
minwid field, e.g. %1*. Restore normal highlight with %* or %0*.
The difference between User{N} and StatusLine will be applied
to StatusLineNC for the statusline of non-current windows.
The number N must be between 1 and 9. See hl-User1..9
So if you change:
let g:airline_section_c='%t %#orange##%{virtualenv#statusline()}'
into:
let g:airline_section_c='%t %#orange##%{virtualenv#statusline()}%*'
It should stop the coloring after the virtualenv.

How to show constantly current working directory in 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.

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')),':~')}\ %*

How to change the characters used for empty space in Vim status line?

I’m using the following statusline setting in Vim:
set statusline=%<%f\ %h%m%r%{fugitive#statusline()}%=%-14.(%l,%c%V%)\ %P
It produces the following status line:
Had the same issue, and eventually realized that I was seeing it only when using certain colorschemes. A little more digging revealed that these colorschemes were setting StatusLine and StatusLineNC highlighting groups to identical values.
:help StatusLineNC notes that if StatusLine and StatusLineNC have equal values, then Vim will use ^^^ in the status line of the current window.
The fix was to comment out the lines that changed these values in the problematic colorschemes.
The fillchars option specifies the characters to fill status lines
and vertical separators. The stl: and stlnc: items in the option’s
value correspond to the status line of the current window and the
status lines of the other windows, respectively. See the current state
of the fillchars option (by :set fcs?), and try to set the filling
characters for statusline explicitly:
:set fillchars+=stl:\ ,stlnc:\ "
(Here the last quote symbol starts a comment and does not affect the
value set, it was added only to make preceding space noticeable.)
standard statusline is :set statusline=%<%f\ %h%m%r%=%-14.(%l,%c%V%)\ %P so I guess it's the fugitive-part that is causing this. What does it do? and in what format does it display data?
from :help 'statusline':
{ NF Evaluate expression between '%{' and '}' and substitute result.
Note that there is no '%' before the closing '}'.
You're statusline looks correct so have a look at the fugitive part...
UPDATE
Did a git clone http://github.com/tpope/vim-fugitive and tried the statusline above
which can be found in the documentation for fugitive and the statusline is correctly
displayed for me. Using xbuntu 11.04. So update to the latest and try again is my advice.
and use set ruler

Resources