How to detect if "&t_Co" is "defined" in my .vimrc? - terminal

When I :echo &t_Co in my Vim, it shows my terminal colors. If I :echo &t_Co in my gVim, nothing is showed (since I'm not in terminal).
I would like to use a condition in my vimrc to detect this situation (the &t_Co with no value in gVim, how do I test it? I know I can use has("gui_running") to see if I'm in gVim or not, but I would like to know how to test a variable like &t_Co that isn't showing nothing).

if !empty(&t_Co)
" t_Co is set to a non-empty value
endif
(exists('&t_Co') doesn't help because it does exist in GVim, it's just empty.)

Related

Print terminal command output directly into vim - macOS

Whenever I enter a terminal command in vim (e.g., !echo hello), I am momentarily kicked out to view the result of that terminal command and then prompted with Press ENTER or type command to continue. This is a bit jarring. I would like to stay within vim and with the command output printed out at the bottom.
I know vim :read will take the terminal output and actually put it into my buffer, but that is not what I am looking to do. Here is some reading if you are interested in a tangent.
It looks like when I run vim in Screen I get what I am looking for, but I am trying to get this to work with tmux and the stock Mac terminal.
After a ton of research, probably too much, I got it! It looks like vim will send commands to the terminal, which in turn defines the behavior of how the terminal commands are processed from vim. Put this into your .vimrc file:
set t_ti= t_te= " show results from terminal commands within vim!
From what I understand, this just makes sure to send nothing to the terminal, which yields my desired results!
Side note: the above addition to your .vimrc file will also prevent the vim buffer from clearing when exiting vim (e.g., :wq). I am okay with this! It is kind of nice sometimes to see what you were just working on :).

VIM, setting directory to where you returned from shell

I'm somewhat new to VIM and am playing with ways to open files (OS X).
Upon returning to VIM after temporarily exiting into shell (ctrl+z, followed by fg), I think it would be convenient if VIM set its current working directory to wherever I was when I returned.
Is this possible? I do know how to change the working directory manually from within VIM.
thank you,
Check if your version of Vim has support for client-server functionality: :echo has('clientserver')
If so, you can write an alias to tell Vim to change its cwd:
In your .bashrc:
alias fg="$VIM --remote-send ':cd $PWD<CR>'; fg"
Where $VIM is vim or gvim or macvim.
You need the colon and the <CR> because you're sending Vim keystrokes rather than commands (<CR> is a special notation for "Enter")
I'm not sure whether --remote-send works when Vim is suspended - this approach might be better if you use something like screen or tmux to run vim and your shell at the same time.
Otherwise, it's a bit trickier.
There's a :shell command that is similar to suspending and resuming, but I'm assuming it forks a child process instead of returning you to Vim's parent process. If you're ok with that, there's a ShellCmdPost autocmd you can attach to to load information. You can use that in association with an alias that writes the $CWD to a file to load the required directory and change to it.
In your .bashrc:
alias fgv="echo $PWD > ~/.cwd; exit"
In your .vimrc:
autocmd ShellCmdPost * call LoadCWD()
function! LoadCWD()
let lines = readfile('~/.cwd')
if len(lines) > 0
let cwd = lines[0]
execute 'cd' cwd
endif
endfunction
Looking through the list of autocommands, I was not able to find any that detect when Vim has been suspended and has just been resumed. You could remap ctrl-z to first set a flag variable then do an unmapped ctrl-z. Then write a shell script like before that writes its $CWD to a specific file. Then you can set up an autocmd to watch that file for modification and in the handler, check the flag variable, and if it's set, reset it, read the file, and change to that directory. A bit complex, but it would work.
That would look like this. You'll have to load the file when you start Vim so that it can monitor it for changes. You may want to set hidden so that you can keep the buffer open but hide it.
In your .bashrc:
alias fg="echo $PWD > ~/.cwd; fg"
In your .vimrc:
set hidden
edit ~/.cwd
enew
nnoremap <C-Z> :let g:load_cwd = 1<CR><C-Z>
autocmd FileChangedShell ~/.cwd call LoadCWD()
function! LoadCWD()
if g:load_cwd
let g:load_cwd = 0
let lines = readfile('~/.cwd')
if len(lines) > 0
let cwd = lines[0]
execute 'cd' cwd
endif
endif
endfunction

Under what circumstances is $MYVIMRC not set?

I am working with a gvim installation on windows (version 7.3). After starting up, I expected the variable $MYVIMRC to be set, yet it is not. Is there a reaseon why it is not set.
There is a vimrc file since many setting are different from the default settings.
I checked the for the value of MYVIMRC with
:echo ">" . $MYVIMRC . "<"
The gvim executable is located in c:\workarea\Vim\vim73, the *_vimrc* (that is being read on gvim's start) in c:\workare\Vim. After I have started gvim, I can edit the _vimrc file with a
:e $VIM/_vimrc
With :version I found that the system vimrc file is under $VIM/vimrc. This file doesn't exist.
If you are running :echo $MYVIMRC and $MYVIMRC is not defined I bet it is because there is not a ~/.vimrc file (or $HOME/_vimrc on windows).
From :help $MYVIMRC
Four places are searched for initializations. The first that exists is
used, the others are ignored. The $MYVIMRC environment variable is
set to the file that was first found
(Second place of that four places is the users vimrc.)
((Just check :help $MYVIMRC for more info))
If there are settings different from the default ones you can check other variables to find where that settings are changed:
:echo $MYVIMRC
:echo $MYGVIMRC
:echo $VIM
:echo $VIMRUNTIME
You can check:
:help startup
:help gui-fork
to see how this works.
You can use as well the verbose command to see where some settings are defined. For example if you know is mapped:
:verbose map <F5>
Will return something like:
* :python debugger.run()
Last set from ~/.vim/bundle/vdebug/plugin/vdebug.vim
(That is from my particular vim configuration)
You wrote,
There is a vimrc file since many setting are different from the default settings.
That could be because there is a system vimrc file. Look about half-way down the output of :version to see where vim looks for the system vimrc file. There are also some less common ways of changing settings. See :help startup.

Why am I seeing only 8 colors in terminal (xfce-terminal)?

I'm running Xubuntu 13.04 and I want to use Vim as my default editor for everything. I have downloaded many vim color schemas and tried them out, but all of them don't look like the official screenshot.
For example, vim's own color schema - desert should look like this:
But in my vim, many colors won't display, for example the background.
So this means a fighting with xfce's Terminal and I can't force it to use 256 colors.
the command tput colors gives me
8.
At the same time the code for ((x=0; x<=255; x++));do echo -e "${x}:\033[48;5;${x}mcolor\033[000m";done shows me nice colors. it seems i missed something. If I run
**$ echo $TERM**
I get xterm. It should be 'xterm-256color'
When I try
set term=xterm-256color
and
export TERM=xterm-256color
Then: echo $TERM
I get the message
xterm-256color.
But after signout/signup, I'm still not getting the right colors in Vim. And I see the Xterm is changed to xterm again.
I added:
if $TERM == "xterm-256color" set t_Co=256 endif
and
t_Co=256
to my .vimrc file and it didn't seem to help. Then I customized the xterm entries; added this to ~/.Xdefaults:
*customization: -color
XTerm*termName: xterm-256color
Add this to ~/.xsession to apply to new terminals:
if [ -f $HOME/.Xdefaults ]; then
xrdb -merge $HOME/.Xdefaults
fi
When I changed in preferences of terminal, emulate terminal environment, the 'xterm' to 'xterm-256color'
I get the message:
'*** VTE ***: Failed to load terminal capabilities from '/etc/termcap'
When I check /usr/share/vte/termcap/xterm, the file xterm-256color is missing. Same in folder xterm0.0. I tried to find this file on internet to download and put in the folder, but I couldn't find it.
This is driving me crazy the whole day... Have anyone suggestions?
Quick (Temporary) Way
Enter this whenever you open a new terminal:
export TERM=xterm-256color
Works for as long as the window is open.
Works-but-dirty Way
Append the line above to ~/.bashrc.
The problem with that, though, is that editing $TERM in .bashrc is a bad idea because doing that automatically makes any terminal using bash try to use it regardless of whether it actually supports 256 colors or not (like when SSH-ing or accessing the terminals with Ctrl+Alt+F1 to F6).
What I did, though is that since xfce4-terminal sets the $COLORTERM value to xfce4-terminal, I, instead appended the following to .bashrc:
if [ "$COLORTERM" == "xfce4-terminal" ] ; then
export TERM=xterm-256color
fi
That way, the relevant $TERM edit only happens if you're using xfce4-terminal, which just sets it to xterm anyway (and changing the emulation environment results in that "VTE" message).
References:
https://bbs.archlinux.org/viewtopic.php?id=175581
http://vim.wikia.com/wiki/256_colors_in_vim#Comments
http://promberger.info/linux/2008/04/05/getting-256-color-support-working-for-mutt-in-xfce4-terminal/
http://docs.xfce.org/apps/terminal/getting-started

set gvim font in .vimrc file

I am using gVim 7.2 on Windows 7. I can set the gui font as Consolas 10 (font size) from the menu. I am trying to set this in .vimrc file like below:
set guifont=Consolas\ 10
But it doesn't work. Does anyone know how to set this?
I use the following (Uses Consolas size 11 on Windows, Menlo Regular size 14 on Mac OS X and Inconsolata size 12 everywhere else):
if has("gui_running")
if has("gui_gtk2")
set guifont=Inconsolata\ 12
elseif has("gui_macvim")
set guifont=Menlo\ Regular:h14
elseif has("gui_win32")
set guifont=Consolas:h11:cANSI
endif
endif
Edit: And while you're at it, you could take a look at Coding Horror's Programming Fonts blog post.
Edit²: Added MacVim.
Try setting your font from the menu and then typing
:set guifont?
This should display to you the string that Vim has set this option to. You'll need to escape any spaces.
I am trying to set this in .vimrc file like below
For GUI specific settings use the .gvimrc instead of .vimrc, which on Windows is either $HOME\_gvimrc or $VIM\_gvimrc.
Check the :help .gvimrc for details. In essence, on start-up VIM reads the .vimrc. After that, if GUI is activated, it also reads the .gvimrc. IOW, all VIM general settings should be kept in .vimrc, all GUI specific things in .gvimrc. (But if you do no use console VIM then you can simply forget about the .vimrc.)
set guifont=Consolas\ 10
The syntax is wrong. After :set guifont=* you can always check the proper syntax for the font using :set guifont?. VIM Windows syntax is :set guifont=Consolas:h10. I do not see precise specification for that, though it is mentioned in the :help win32-faq.
Start a graphical vim session.
Do :e $MYGVIMRC Enter
Use the graphical font selection dialog to select a font.
Type :set guifont= Tab Enter.
Type G o to start a new line at the end of the file.
Type Ctrl+R followed by :.
The command in step 6 will insert the contents of the : special register
which contains the last ex-mode command used. Here that will be the command
from step 4, which has the properly formatted font name thanks to the tab
completion of the value previously set using the GUI dialog.
For Windows do the following:
Note down the font name and font size from the "Edit-Select Font..." menu of "gvim.exec".
Then do :e $MYGVIMRC
Search for "guifont" string and change it to
set guifont=<font name as noted>:h<font size>
Save the file and quit.
Next time when you execute gvim.exec, you will see the effect.
Although this is an old thread I thought that I would add a comment as I have come across it whilst trying to solve a similar issue; this might help anyone else who may find themselves here:
The backslash character is used to ignore the next character; once added to the font name in my gvimrc it worked; I am on a GNU/Linux machine which does not like spaces. I suspect that the initial post was an error due to the back slash being used on a windows machine.
In example:
:set guifont? ## From gvim command, would give the following:
set guifont=DejaVu Sans Mono for Powerline 11
Where as I needed to add this line to the gvimrc file for it to be read:
set guifont=DejaVu\ Sans\ Mono\ for\ Powerline\ 11
When I try:
set guifont=Consolas:h16
I get: Warning: Font "Consolas" reports bad fixed pitch metrics
and the following is work, and don't show the waring.
autocmd vimenter * GuiFont! Consolas:h16
by the way, if you want to use the mouse wheel to control the font-size, then you can add:
function! AdjustFontSize(amount)
let s:font_size = s:font_size + a:amount
:execute "GuiFont! Consolas:h" . s:font_size
endfunction
noremap <C-ScrollWheelUp> :call AdjustFontSize(1)<CR>
noremap <C-ScrollWheelDown> :call AdjustFontSize(-1)<CR>
and if you want to pick the font, you can set
set guifont=*
will bring up a font requester, where you can pick the font you want.
I had to end up doing
:set guifont=Courier:h10:cANSI
Ubuntu 14.04 LTS
:/$ cd etc/vim/
:/etc/vim$ sudo gvim gvimrc
After if - endif block, type
set guifont=Neep\ 10
save the file (:wq!). Here "Neep" (your choice) is the font style and "10" is respect size of the font. Then build the font - cache again.
:/etc/vim$ fc-cache -f -v
Your desired font will set to gvim.

Resources