Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send escape sequences from within Vim?

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")
like image 866
felixphew Avatar asked Sep 06 '15 23:09

felixphew


People also ask

How do I escape a character in Vim?

As you type the command, use control-v then escape to enter the escape.

How do you do an escape sequence in a string?

Use escape sequences only in character constants or in string literals. An error message is issued if an escape sequence is not recognized. In string and character sequences, when you want the backslash to represent itself (rather than the beginning of an escape sequence), you must use a \\ backslash escape sequence.

How do you escape a sequence in Unix?

z/OS UNIX System Services User's GuideAll escape sequences are made from a backslash character ( \ ) followed by one to three other characters. Escape sequences are used inside strings, not just those for printf, to represent special characters. In particular, the \n escape sequence represents the newline character.

What is \r in escape sequence?

\r is a carriage return character; it tells your terminal emulator to move the cursor at the start of the line. The cursor is the position where the next characters will be rendered. So, printing a \r allows to override the current line of the terminal emulator.


1 Answers

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.

like image 190
Roy Zuo Avatar answered Sep 24 '22 05:09

Roy Zuo