问题
So recently Apple have included support for displaying the working directory and file in the status bar of Terminal. The escape sequence that must be sent (to set the current file) is this:
ESC ] 6 ; Pt BEL
where Pt
is a file://
url pointing to the file currently being edited. So I figured I could get Vim to send this command as an escape sequence, but I'm having a bit of trouble. I have this so far:
au BufNewFile,BufReadPost,BufFilePost,BufWritePost * echo <escape sequence>
but I have a feeling that it won't quite work like that. Also, I have no idea how to get the current file as a file://
url, although I suspect netrw might be able to help me. Any ideas?
Edit
So far, I have this:
au BufNewFile,BufReadPost,BufFilePost * echo printf('\e]6;file://%s%s\a', $HOSTNAME, expand('%:p'))
but it still isn't working - $HOSTNAME
isn't getting expanded. Anyone have any tips on how to make this expand?
Edit 2
OK, so I've made some changes to the quotes and exported $HOSTNAME
, and now this is printing out fine. But instead of literally echoing the escape sequences, vim is printing them like ^[
, which makes them useless! And so here comes my real question: how do you make vim send literal escape sequences to the shell?
Success!
The final code is as follows:
set title
set t_ts=^[]6;
set t_fs=^G
auto BufEnter * let &titlestring = "file://" . hostname() . expand("%:p")
回答1:
You can use !echo -ne <ESCAPE SEQUENCE>
within vim. In order to use it with your title string which contains variables, execute
could be prefixed.
execute "silent !echo -ne " . EXPRESSION
But honestly it is not necessary to roll your own command. Instead, you can manipulate these two varialbes as explained here.
The 't_ts' and 't_fs' options are used to set the window title if the terminal allows title setting via sending strings. They are sent before and after the title string, respectively. Similar 't_IS' and 't_IE' are used to set the icon text. These are Vim-internal extensions of the Unix termcap, so they cannot be obtained from an external termcap. However, the builtin termcap contains suitable entries for xterm and iris-ansi, so you don't need to set them here.
来源:https://stackoverflow.com/questions/32429471/how-to-send-escape-sequences-from-within-vim