I want to set the &titlestring in my .vimrc to contain the short hostname. When I have this, it works fine:
let &titlestring = $USER . "#" . hostname() . " | " . expand("%:~")
if &term == "screen"
set t_ts=^[k
set t_fs=^[\
endif
if &term == "screen" || &term =~ "xterm"
set title
endif
But it prints the full hostname. In order to get the short hostname, I tried this:
let hostname=system('hostname -s')
let &titlestring = hostname . " | " . expand("%:~")
if &term == "screen"
set t_ts=^[k
set t_fs=^[\
endif
if &term == "screen" || &term =~ "xterm"
set title
endif
But then I get | ~/.vimrc echoed to the input line and Thanks for flying Vim in the titlebar. How do I get the short hostname in the titlebar?
I wouldn't launch an external command for that; the system() call in your .vimrc may explain the strange symptoms.
Why don't you extract the short hostname (first part, up to .) via substitute()?!
let &titlestring = $USER . "#" . substitute(hostname(), '\..*$', '', '') . " | " . expand("%:~")
Related
I've searched high and low but cannot get the correct syntax etc. I'd like to set my vim_rc file so I'm using the evening theme and 16pt Lucida console text as a default. I've tried many combinations and I am not having much joy on the web. At present, I'm receiving the following error:
Error detected while processing C:\Program Files
(x86)\Vim\_vimrc:
line 47:
E518: Unknown option: Console\ 14
and my vim file (based on web advice) looks like the following
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
if empty(&shellxquote)
let l:shxq_sav = ''
set shellxquote&
endif
let cmd = '"' . $VIMRUNTIME . '\diff"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
let cmd = substitute(cmd, '!', '\!', 'g')
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
if exists('l:shxq_sav')
let &shellxquote=l:shxq_sav
endif
endfunction
set expandtab
set tabstop=4
set softtabstop=4
set shiftwidth=4
set autoindent
set textwidth=80
set guifont=Lucida Console\ 14
You have to escape spaces with a backslash. You've already done so for the space after "Console". You just need to also escape the one before "Console".
set guifont=Lucida\ Console\ 14
in _vimrc file:
set guifont=Lucida_Console:h14
TDLR; I am unable to edit gvim using the _vimrc file
Basically, I want to change some default settings (like background color) in the GUI version of vim (gvim). However, when I make changes to my .vimrc file, my gvim does not react to the changes.
I already have
if has("gui_number")
set number
colorscheme desert
endif
in my .vimrc file, and from what I've read that should make changes in my GVIM, but it doesn't.
I even tried to type some random stuff in the .vimrc file to see if GVIM would display an error, which it did not.
So i think the problem may be my gvim isn't reading my .vimrc file at all. So this may be a pathing problem?
This below is my .vimrc file
source $VIMRUNTIME/vimrc_example.vim
jhgfjhgjhg (This here was to test for an error)
if has('gui_running')
set number
colorscheme desert
endif
set diffexpr=MyDiff()
function MyDiff()
let opt = '-a --binary '
if &diffopt =~ 'icase' | let opt = opt . '-i ' | endif
if &diffopt =~ 'iwhite' | let opt = opt . '-b ' | endif
let arg1 = v:fname_in
if arg1 =~ ' ' | let arg1 = '"' . arg1 . '"' | endif
let arg1 = substitute(arg1, '!', '\!', 'g')
let arg2 = v:fname_new
if arg2 =~ ' ' | let arg2 = '"' . arg2 . '"' | endif
let arg2 = substitute(arg2, '!', '\!', 'g')
let arg3 = v:fname_out
if arg3 =~ ' ' | let arg3 = '"' . arg3 . '"' | endif
let arg3 = substitute(arg3, '!', '\!', 'g')
if $VIMRUNTIME =~ ' '
if &sh =~ '\<cmd'
if empty(&shellxquote)
let l:shxq_sav = ''
set shellxquote&
endif
let cmd = '"' . $VIMRUNTIME . '\diff"'
else
let cmd = substitute($VIMRUNTIME, ' ', '" ', '') . '\diff"'
endif
else
let cmd = $VIMRUNTIME . '\diff'
endif
let cmd = substitute(cmd, '!', '\!', 'g')
silent execute '!' . cmd . ' ' . opt . arg1 . ' ' . arg2 . ' > ' . arg3
if exists('l:shxq_sav')
let &shellxquote=l:shxq_sav
endif
endfunction
Here is my .GVIMRC file
version 6.0
if &cp | set nocp | endif
let s:cpo_save=&cpo
set cpo&vim
map! <S-Insert> *
vmap "*d
map Q gq
vmap [% [%m'gv``
vmap ]% ]%m'gv``
vmap a% [%v]%
vmap gx <Plug>NetrwBrowseXVis
nmap gx <Plug>NetrwBrowseX
vnoremap <silent> <Plug>NetrwBrowseXVis :call netrw#BrowseXVis()
nnoremap <silent> <Plug>NetrwBrowseX :call netrw#BrowseX(expand((exists("g:netrw_gx")? g:netrw_gx : '<cfile>')),netrw#CheckIfRemote())
vmap <C-Del> "*d
vmap <S-Del> "*d
vmap <C-Insert> "*y
vmap <S-Insert> "-d"*P
nmap <S-Insert> "*P
inoremap u
let &cpo=s:cpo_save
unlet s:cpo_save
set backspace=indent,eol,start
set backup
set diffexpr=MyDiff()
set display=truncate
set guioptions=egmrLT
set helplang=En
set history=200
set hlsearch
set incsearch
set langnoremap
set nolangremap
set nrformats=bin,hex
set ruler
set runtimepath=~/vimfiles,C:\\Program\ Files\ (x86)\\Vim/vimfiles,C:\\Program\ Files\ (x86)\\Vim\\vim81,C:\\Program\ Files\ (x86)\\Vim\\vim81\\pack\\dist\\opt\\matchit,C:\\Program\ Files\ (x86)\\Vim/vimfiles/after,~/vimfiles/after
set scrolloff=5
set ttimeout
set ttimeoutlen=100
set undofile
set wildmenu
" vim: set ft=vim :
I would hope that my gvim would display lines and a different background, but nothing happens.
edit: should my _vimrc file look exactly like my gvimrc?
I figured it out guys.
So apparently I was supposed to create a _vimrc file in my home directory.
If you are on windows 7 and having the problem where you cant edit your _vimrc file in your VIM directory to change gvimrc, then its because you are trying to edit the wrong _vimrc!! You need to make a different user one in your home directory!
Windows 7:
Open vim, type in :e $HOME/_vimrc
And then put the new settings and save (then reopen GVIM)
I have a section in my .vimrc file that sets the title of the terminal to the name of the file that is opened with vim. However, this breaks down when using :edit to go from one file to the next from within vim, as it stays the same. I gather that this is because, by default, the .vimrc file is only run when vim is launched.
Is there a way to get vim to "watch" for the :edit, :e, and any other commands, and then run :so $MYVIMRC, which reloads the .vimrc?
Alternatively,is there some built in feature that set the terminal title to the file name which I've somehow overlooked?
Section mentioned in .vimrc
" Apply filename to terminal session title
"" Doesn't work when changing file using :edit
let path_list = reverse(split(expand("%:p"),"/"))
if len(path_list) > 0
let &titlestring = path_list[0]
if &term == "screen"
set t_ts=^[k
set t_fs=^[\
endif
if &term == "screen" || &term == "xterm"
set title
endif
endif
This does this job
autocmd BufEnter * :so $MYVIMRC
Update
As suggested by Doktor, it's better to do this in a function, and just call this upon BufEnter. Here's how that works.
function SetTitle()
let path_list = reverse(split(expand("%:p"),"/"))
if len(path_list) > 0
let &titlestring = path_list[1] . "/" . path_list[0]
if &term == "screen"
set t_ts=^[k
set t_fs=^[\
endif
if &term == "screen" || &term == "xterm"
set title
endif
endif
endfunction
autocmd BufEnter * :call SetTitle()
I'd like to refactor this condition in my vimrc:
if &term =~ "xterm" || &term =~ "screen" || &term =~ "builtin_gui"
" do some stuff...
endif
In Ruby I'd probably do something like:
names = ["xterm", "screen", "builtin_gui"]
if names.any? { |n| &term =~ n }
" do some stuff...
endif
I know Vimscript doesn't have anything like Ruby blocks, but is there any builtin function that would let me do something along these lines?
=~ a regexp match. So you can use full power of regexp. Either of these should work:
&term =~ 'xterm\|screen\|builtin_gui'
&term =~ join(names, '\|')`
Not really
let names = ["xterm", "screen", "builtin_gui"]
if len(filter(names, '&term =~ v:val')) > 0
endif
I can get the vim title to display on my window by doing this:
let &titlestring = expand("%:t") . " # " . hostname()
if &term == "screen"
set t_ts=^[k
set t_fs=^[\
endif
if &term == "screen" || &term == "xterm"
set title
endif
But the tabs will say "Default".
From the commandline I can do this:
echo -ne "\e]1;hello world\a"
And that'll show "Hello World" in my tabs.
Is there a way to have vim write this stuff to my tab instead of title instead?
This works for me:
" Set the title of the Terminal to the currently open file
function! SetTerminalTitle()
let titleString = expand('%:t')
if len(titleString) > 0
let &titlestring = expand('%:t')
" this is the format iTerm2 expects when setting the window title
let args = "\033];".&titlestring."\007"
let cmd = 'silent !echo -e "'.args.'"'
execute cmd
redraw!
endif
endfunction
autocmd BufEnter * call SetTerminalTitle()
Source: https://gist.github.com/bignimbus/1da46a18416da4119778
I don't have iTerm, so I can't test this, but try adding this to your .vimrc:
set t_ts=^[]1;
set t_fs=^G
Type CTRL-V Escape for ^[ and CTRL-V CTRL-G for ^G.
Just to piggy back on user2486953's comment above.
I was able to accomplish this with two super simple lines in my ~/.vimrc:
set title
set titlestring=%f
(Lower case 'f' gives me just the filename, whereas capital gives the full path too)
i.e. I didn't have to set anything with escape sequences like the accepted answer above. I am running gnome-terminal but I don't understand why iTerm2 would be any different for a VI setting.