README.md (30545B)
1 [](https://melpa.org/#/vterm) 2 3 # Introduction 4 5 Emacs-libvterm (_vterm_) is fully-fledged terminal emulator inside GNU Emacs 6 based on [libvterm](https://github.com/neovim/libvterm), a C library. As a 7 result of using compiled code (instead of elisp), emacs-libvterm is fully 8 capable, fast, and it can seamlessly handle large outputs. 9 10 ## Warning 11 12 This package is in active development and, while being stable enough to be used 13 as a daily-driver, it is currently in **alpha** stage. This means that 14 occasionally the public interface will change (for example names of options or 15 functions). A list of recent breaking changes is in 16 [appendix](#breaking-changes). Moreover, emacs-libvterm deals directly with some 17 low-level operations, hence, bugs can lead to segmentation faults and crashes. 18 If that happens, please [report the 19 problem](https://github.com/akermu/emacs-libvterm/issues/new). 20 21 ## Given that eshell, shell, and (ansi-)term are Emacs built-in, why should I use vterm? 22 23 The short answer is: unparalleled performance and compatibility with standard 24 command-line tools. 25 26 For the long answer, let us discuss the differences between `eshell`, `shell`, 27 `term` and `vterm`: 28 - `eshell`: it is a shell completely implemented in Emacs Lisp. It is 29 well-integrated in Emacs and it runs on Windows. It does not support command line 30 tools that require terminal manipulation capabilities (e.g., `ncdu`, `nmtui`, 31 ...). 32 - `shell`: it interfaces with a standard shell (e.g., `bash`). It reads an input 33 from Emacs, sends it to the shell, and reports back the output from the shell. 34 As such, like `eshell`, it does not support interactive commands, especially 35 those that directly handle how the output should be displayed (e.g., `htop`). 36 - `term`: it is a terminal emulator written in elisp. `term` runs a shell 37 (similarly to other terminal emulators like Gnome Terminal) and programs can 38 directly manipulate the output using escape codes. Hence, many interactive 39 applications (like the one aforementioned) work with `term`. However, `term` 40 and `ansi-term` do not implement all the escapes codes needed, so some 41 programs do not work properly. Moreover, `term` has inferior performance 42 compared to standalone terminals, especially with large bursts of output. 43 - `vterm`: like `term` it is a terminal emulator. Unlike `term`, the core of 44 `vterm` is an external library written in C, `libvterm`. For this reason, 45 `vterm` outperforms `term` and has a nearly universal compatibility with 46 terminal applications. 47 48 Vterm is not for you if you are using Windows, or if you cannot set up Emacs 49 with support for modules. Otherwise, you should try vterm, as it provides a 50 superior terminal experience in Emacs. 51 52 Using `vterm` is like using Gnome Terminal inside Emacs: Vterm is fully-featured 53 and fast, but is not as well integrated in Emacs as `eshell` (yet), so some of 54 the editing keybinding you are used to using may not work. For example, 55 `evil-mode` is currently not supported (though, users can enable VI emulation in 56 their shells). This is because keys are sent directly to the shell. We are 57 constantly working to improve this. 58 59 # Installation 60 61 ## Requirements 62 63 Before installing emacs-libvterm, you need to make sure you have installed 64 1. GNU Emacs (>= 25.1) with [module 65 support](https://www.gnu.org/software/emacs/manual/html_node/elisp/Dynamic-Modules.html). 66 You can check that, by verifying that `module-file-suffix` is not `nil`. 67 2. cmake (>= 3.11) 68 3. libtool-bin (related issues: 69 [#66](https://github.com/akermu/emacs-libvterm/issues/66) 70 [#85](https://github.com/akermu/emacs-libvterm/issues/85#issuecomment-491845136)) 71 4. OPTIONAL: [libvterm](https://github.com/neovim/libvterm) (>= 0.1). This 72 library can be found in the official repositories of most distributions 73 (e.g., Arch, Debian, Fedora, Gentoo, openSUSE, Ubuntu). Typical names are 74 `libvterm` (Arch, Fedora, Gentoo, openSUSE), or `libvterm-dev` (Debian, 75 Ubuntu). If not available, `libvterm` will be downloaded during the 76 compilation process. Some distributions (e.g. Ubuntu < 20.04, Debian < 11) 77 have versions of `libvterm` that are too old. If you find compilation errors 78 related to `VTERM_COLOR`, you should not use your system libvterm. See 79 [FAQ](#frequently-asked-questions-and-problems) for more details. 80 81 ## From MELPA 82 83 `vterm` is available on [MELPA](https://melpa.org/), and it can be installed as 84 a normal package. If the requirements are satisfied (mainly, Emacs was built 85 with support for modules), `vterm` will compile the module the first time it is 86 run. This is the recommended way to install `vterm`. 87 88 `vterm` can be install from MELPA with `use-package` by adding the following 89 lines to your `init.el`: 90 91 ```elisp 92 (use-package vterm 93 :ensure t) 94 ``` 95 96 To take full advantage of the capabilities of `vterm`, you should configure your 97 shell too. Read about this in the section [shell-side 98 configuration](#shell-side-configuration). 99 100 ## Manual installation 101 102 Clone the repository: 103 104 ```sh 105 git clone https://github.com/akermu/emacs-libvterm.git 106 ``` 107 108 By default, vterm will try to find if libvterm is installed. If it is not found, 109 emacs-libvterm will download the latest version available of libvterm (from 110 [here](https://github.com/neovim/libvterm)), compile it, and use it. If you 111 always want to use the vendored version as opposed to the one on you system, set 112 `USE_SYSTEM_LIBVTERM` to `no`. To do this, change `cmake ..` with `cmake 113 -DUSE_SYSTEM_LIBVTERM=no ..` in the following instructions. 114 115 Build the module with: 116 117 ``` sh 118 cd emacs-libvterm 119 mkdir -p build 120 cd build 121 cmake .. 122 make 123 ``` 124 125 And add this to your `init.el`: 126 127 ``` elisp 128 (add-to-list 'load-path "path/to/emacs-libvterm") 129 (require 'vterm) 130 ``` 131 132 Or, with `use-package`: 133 134 ```elisp 135 (use-package vterm 136 :load-path "path/to/emacs-libvterm/") 137 ``` 138 139 ## vterm and Ubuntu 140 ### 20.04 141 Using `vterm` on Ubuntu requires additional steps. The latest LTS version 142 (20.04) ships without CMake installed and Emacs27 is not yet available from Ubuntu's package repository. 143 144 The basic steps for getting vterm to work on Ubuntu 20.04 are: 145 * Ensure Emacs27 is installed 146 * Install cmake, libtool, and libtool-bin 147 148 There are a few options for installing Emacs27 on Ubuntu 20.04: 149 * Compile Emacs27 from source 150 * Install Emacs27 from Snap 151 * Install Emacs27 from Kevin Kelley's PPA 152 153 In any case, if you have an older Emacs version you will need to purge it before proceeding: 154 155 #### Purge Emacs 156 ```sh 157 sudo apt --purge remove emacs 158 sudo apt autoremove 159 ``` 160 161 #### Installing Emacs27 from Kevin Kelley PPA 162 ```sh 163 sudo add-apt-repository ppa:kelleyk/emacs 164 sudo apt install emacs27 165 ``` 166 167 ##### If you get an error about emacs27_common during the install process: 168 ```sh 169 Errors were encountered while processing: 170 /tmp/apt-dpkg-install-RVK8CA/064-emacs27-common_27.1~1.git86d8d76aa3-kk2+20.04_all.deb 171 ``` 172 run 173 ```sh 174 sudo apt --purge remove emacs-common 175 sudo apt --fix-broken install 176 ``` 177 178 #### Installing Emacs27 from Snap 179 I hesitate to include SNAP here, because I ran into a number of GTK Theme parsing errors, and Fontconfig errors when I tested it, and reverted to installing from Kevin Kelley's PPA. YMMV 180 ```sh 181 sudo snap install emacs --classic 182 ``` 183 184 #### Install CMake and Libtool 185 In Ubuntu 20.04 CMake (v3.16.3-1ubuntu1) and Libtool can be installed with 186 ```sh 187 sudo apt install cmake 188 sudo apt install libtool 189 sudo apt install libtool-bin 190 ``` 191 192 ### 18.04 193 194 Using `vterm` on Ubuntu 18.04 requires additional steps. 195 18.04 ships with a version of CMake that is too old for `vterm` and GNU 196 Emacs is not compiled with support for dynamical module loading. 197 198 It is possible to install GNU Emacs with module support from Kevin Kelley's PPA. 199 The binary in Ubuntu Emacs Lisp PPA is currently broken and leads to segmentation faults 200 (see [#185](https://github.com/akermu/emacs-libvterm/issues/185#issuecomment-562237077)). 201 In case Emacs is already on the system, you need to purge it before proceeding 202 with the following commands. 203 ```sh 204 sudo add-apt-repository ppa:kelleyk/emacs 205 sudo apt update 206 sudo apt-get install emacs26 207 ``` 208 209 A way to install a recent version of CMake (>= 3.11) is with linuxbrew. 210 ```sh 211 brew install cmake 212 ``` 213 214 215 In some cases, `/bin/sh` needs to be relinked to `/bin/bash` for the compilation 216 to work (see, 217 [#216](https://github.com/akermu/emacs-libvterm/issues/216#issuecomment-575934593)). 218 219 Pull requests to improve support for Ubuntu are welcome (e.g., simplifying the 220 installation). 221 222 Some releases of Ubuntu (e.g., 18.04) ship with a old version of libvterm that 223 can lead to compilation errors. If you have this problem, see the 224 [FAQ](#frequently-asked-questions-and-problems) for a solution. 225 226 ## GNU Guix 227 228 `vterm` and its dependencies are available in GNU Guix as 229 [emacs-vterm](https://guix.gnu.org/packages/emacs-vterm-0-1.7d7381f/). 230 The package can be installed with `guix package -i emacs-vterm`. 231 232 ## Shell-side configuration 233 234 Some of the most useful features in `vterm` (e.g., [directory-tracking and 235 prompt-tracking](#directory-tracking-and-prompt-tracking) or [message 236 passing](#message-passing)) require shell-side configurations. The main goal of 237 these additional functions is to enable the shell to send information to `vterm` 238 via properly escaped sequences. A function that helps in this task, 239 `vterm_printf`, is defined below. This function is widely used throughout this 240 readme. 241 242 For `bash` or `zsh`, put this in your `.zshrc` or `.bashrc` 243 ```bash 244 vterm_printf(){ 245 if [ -n "$TMUX" ] && ([ "${TERM%%-*}" = "tmux" ] || [ "${TERM%%-*}" = "screen" ] ); then 246 # Tell tmux to pass the escape sequences through 247 printf "\ePtmux;\e\e]%s\007\e\\" "$1" 248 elif [ "${TERM%%-*}" = "screen" ]; then 249 # GNU screen (screen, screen-256color, screen-256color-bce) 250 printf "\eP\e]%s\007\e\\" "$1" 251 else 252 printf "\e]%s\e\\" "$1" 253 fi 254 } 255 ``` 256 This works also for `dash`. 257 258 For `fish` put this in your `~/.config/fish/config.fish`: 259 ```bash 260 function vterm_printf; 261 if begin; [ -n "$TMUX" ] ; and string match -q -r "screen|tmux" "$TERM"; end 262 # tell tmux to pass the escape sequences through 263 printf "\ePtmux;\e\e]%s\007\e\\" "$argv" 264 else if string match -q -- "screen*" "$TERM" 265 # GNU screen (screen, screen-256color, screen-256color-bce) 266 printf "\eP\e]%s\007\e\\" "$argv" 267 else 268 printf "\e]%s\e\\" "$argv" 269 end 270 end 271 ``` 272 273 # Debugging and testing 274 275 If you have successfully built the module, you can test it by executing the 276 following command in the `build` directory: 277 278 ```sh 279 make run 280 ``` 281 282 # Usage 283 284 ## `vterm` 285 286 Open a terminal in the current window. 287 288 ## `vterm-other-window` 289 290 Open a terminal in another window. 291 292 ## `vterm-copy-mode` 293 294 When you enable `vterm-copy-mode`, the terminal buffer behaves like a normal 295 `read-only` text buffer: you can search, copy text, etc. The default keybinding 296 to toggle `vterm-copy-mode` is `C-c C-t`. When a region is selected, it is 297 possible to copy the text and leave `vterm-copy-mode` with the enter key. 298 299 If no region is selected when the enter key is pressed it will copy the current 300 line from start to end. If `vterm-copy-exclude-prompt` is true it will skip 301 the prompt and not include it in the copy. 302 303 ## `vterm-clear-scrollback` 304 305 `vterm-clear-scrollback` does exactly what the name suggests: it clears the 306 current buffer from the data that it is not currently visible. 307 `vterm-clear-scrollback` is bound to `C-c C-l`. This function is typically used 308 with the `clear` function provided by the shell to clear both screen and 309 scrollback. In order to achieve this behavior, you need to add a new shell alias. 310 311 For `zsh`, put this in your `.zshrc`: 312 ```zsh 313 314 if [[ "$INSIDE_EMACS" = 'vterm' ]]; then 315 alias clear='vterm_printf "51;Evterm-clear-scrollback";tput clear' 316 fi 317 ``` 318 For `bash`, put this in your `.bashrc`: 319 ```bash 320 if [[ "$INSIDE_EMACS" = 'vterm' ]]; then 321 function clear(){ 322 vterm_printf "51;Evterm-clear-scrollback"; 323 tput clear; 324 } 325 fi 326 ``` 327 For `fish`: 328 ``` 329 if [ "$INSIDE_EMACS" = 'vterm' ] 330 function clear 331 vterm_printf "51;Evterm-clear-scrollback"; 332 tput clear; 333 end 334 end 335 ``` 336 These aliases take advantage of the fact that `vterm` can execute `elisp` 337 commands, as explained below. 338 339 If it possible to automatically clear the scrollback when the screen is cleared 340 by setting the variable `vterm-clear-scrollback-when-clearing`: When 341 `vterm-clear-scrollback-when-clearing` is non nil, `C-l` clears both the screen 342 and the scrollback. When is nil, `C-l` only clears the screen. The opposite 343 behavior can be achieved by using the universal prefix (i.e., calling `C-u C-l`). 344 345 # Customization 346 347 ## `vterm-shell` 348 349 Shell to run in a new vterm. It defaults to `$SHELL`. 350 351 ## `vterm-environment` 352 353 to add more environment variables there is the custom vterm-environment which has 354 a similar format than the internal Emacs variable process-environment. 355 You can check the documentation with C-h v process-environment for more details. 356 357 ## `vterm-term-environment-variable` 358 359 Value for the `TERM` environment variable. It defaults to `xterm-256color`. If 360 [eterm-256color](https://github.com/dieggsy/eterm-256color) is installed, 361 setting `vterm-term-environment-variable` to `eterm-color` improves the 362 rendering of colors in some systems. 363 364 ## `vterm-kill-buffer-on-exit` 365 366 If set to `t`, buffers are killed when the associated process is terminated (for 367 example, by logging out the shell). Keeping buffers around it is useful if you 368 need to copy or manipulate the content. 369 370 ## `vterm-module-cmake-args` 371 372 Compilation flags and arguments to be given to CMake when compiling the module. 373 This string is directly passed to CMake, so it uses the same syntax. At the 374 moment, it main use is for compiling vterm using the system libvterm instead of 375 the one downloaded from GitHub. You can find all the arguments and flags 376 available with `cmake -LA` in the build directory. 377 378 ## `vterm-copy-exclude-prompt` 379 380 Controls whether or not to exclude the prompt when copying a line in 381 `vterm-copy-mode`. Using the universal prefix before calling 382 `vterm-copy-mode-done` will invert the value for that call, allowing you to 383 temporarily override the setting. When a prompt is not found, the whole line is 384 copied. 385 386 ## `vterm-use-vterm-prompt-detection-method` 387 388 The variable `vterm-use-vterm-prompt-detection-method` determines whether to use 389 the vterm prompt tracking, if false it use the regexp in 390 `vterm-copy-prompt-regexp` to search for the prompt. 391 392 ## `vterm-enable-manipulate-selection-data-by-osc52` 393 394 Vterm support copy text to Emacs kill ring and system clipboard by using OSC 52. 395 See https://invisible-island.net/xterm/ctlseqs/ctlseqs.html for more info about OSC 52. 396 For example: send 'blabla' to kill ring: printf "\033]52;c;$(printf "%s" "blabla" | base64)\a" 397 398 tmux can share its copy buffer to terminals by supporting osc52(like iterm2 xterm), 399 you can enable this feature for tmux by : 400 set -g set-clipboard on #osc 52 copy paste share with iterm 401 set -ga terminal-overrides ',xterm*:XT:Ms=\E]52;%p1%s;%p2%s\007' 402 set -ga terminal-overrides ',screen*:XT:Ms=\E]52;%p1%s;%p2%s\007' 403 404 The clipboard querying/clearing functionality offered by OSC 52 is not implemented here, 405 And for security reason, this feature is disabled by default." 406 407 This feature need the new way of handling strings with a struct `VTermStringFragment` 408 in libvterm. You'd better compile emacs-libvterm with `cmake -DUSE_SYSTEM_LIBVTERM=no ..`. 409 If you don't do that, when the content you want to copied is too long, it would be truncated 410 by bug of libvterm. 411 412 ## `vterm-buffer-name-string` 413 414 When `vterm-buffer-name-string` is not nil, vterm renames automatically its own 415 buffers with `vterm-buffer-name-string`. This string can contain the character 416 `%s`, which is substituted with the _title_ (as defined by the shell, see 417 below). A possible value for `vterm-buffer-name-string` is `vterm %s`, according 418 to which all the vterm buffers will be named "vterm TITLE". 419 420 This requires some shell-side configuration to print the title. For example to 421 set the name "HOSTNAME:PWD", use can you the following: 422 423 For `zsh` 424 ```zsh 425 autoload -U add-zsh-hook 426 add-zsh-hook -Uz chpwd (){ print -Pn "\e]2;%m:%2~\a" } 427 ``` 428 For `bash`, 429 ```bash 430 PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }"'echo -ne "\033]0;${HOSTNAME}:${PWD}\007"' 431 ``` 432 For `fish`, 433 ```fish 434 function fish_title 435 hostname 436 echo ":" 437 pwd 438 end 439 ``` 440 See [zsh and bash](http://tldp.org/HOWTO/Xterm-Title-4.html) and [fish 441 documentations](https://fishshell.com/docs/current/#programmable-title). 442 443 ## `vterm-always-compile-module` 444 445 Vterm needs `vterm-module` to work. This can be compiled externally, or `vterm` 446 will ask the user whether to build the module when `vterm` is first called. To 447 avoid this question and always compile the module, set 448 `vterm-always-compile-module` to `t`. 449 450 ## Keybindings 451 452 If you want a key to be sent to the terminal, bind it to `vterm--self-insert`, 453 or remove it from `vterm-mode-map`. By default, `vterm.el` binds most of the 454 `C-<char>` and `M-<char>` keys, `<f1>` through `<f12>` and some special keys 455 like `<backspace>` and `<return>`. Sending a keyboard interrupt is bound to `C-c 456 C-c`. 457 458 ## Fonts 459 460 You can change the font (the _face_) used in a vterm with the following code: 461 462 ``` emacs 463 (add-hook 'vterm-mode-hook 464 (lambda () 465 (set (make-local-variable 'buffer-face-mode-face) 'fixed-pitch) 466 (buffer-face-mode t))) 467 ``` 468 469 Where instead of `'fixed-pitch` you specify the face you want to use. The 470 example reported here can be used to force vterm to use a mono-spaced font (the 471 `fixed-pitch` face). This is useful when your default font in Emacs is a 472 proportional font. 473 474 In addition to that, you can disable some text properties (bold, underline, 475 reverse video) setting the relative option to `t` (`vterm-disable-bold`, 476 `vterm-disable-underline`, or `vterm-disable-inverse-video`). 477 478 ## Blink cursor 479 480 When `vterm-ignore-blink-cursor` is `t`, vterm will ignore request from application to turn on or off cursor blink. 481 482 If `nil`, cursor in any window may begin to blink or not blink because `blink-cursor-mode` 483 is a global minor mode in Emacs, you can use `M-x blink-cursor-mode` to toggle. 484 485 ## Colors 486 487 Set the `:foreground` and `:background` attributes of the following faces to a 488 color you like. The `:foreground` is ansi color 0-7, the `:background` attribute 489 is ansi color 8-15. 490 491 - vterm-color-black 492 - vterm-color-red 493 - vterm-color-green 494 - vterm-color-yellow 495 - vterm-color-blue 496 - vterm-color-magenta 497 - vterm-color-cyan 498 - vterm-color-white 499 500 ## Directory tracking and Prompt tracking 501 502 `vterm` supports _directory tracking_. If this feature is enabled, the default 503 directory in Emacs and the current working directory in `vterm` are synced. As a 504 result, interactive functions that ask for a path or a file (e.g., `dired` or 505 `find-file`) will do so starting from the current location. 506 507 And `vterm` supports _prompt tracking_. If this feature is enabled, Emacs knows 508 where the prompt ends, you needn't customize `term-prompt-regexp` any more. 509 Then you can use `vterm-next-prompt` and `vterm-previous-prompt` 510 moving to end of next/previous prompt. The default keybinding is `C-c C-n` and `C-c C-p`. 511 512 And `vterm-beginning-of-line` would move the point to the first character after the 513 shell prompt on this line. If the point is already there, move to the beginning of the line. 514 The default keybinding is `C-a` in `vterm-copy-mode`. 515 516 And `vterm--at-prompt-p` would check whether the cursor is at the point just after 517 the shell prompt. 518 519 Directory tracking and Prompt tracking requires some configuration, as the shell has to be 520 instructed to share the relevant information with Emacs. The following pieces of 521 code assume that you have the function `vterm_printf` as defined in section 522 [shell-side configuration](#shell-side-configuration). 523 524 For `zsh`, put this at the end of your `.zshrc`: 525 526 ```zsh 527 vterm_prompt_end() { 528 vterm_printf "51;A$(whoami)@$(hostname):$(pwd)"; 529 } 530 setopt PROMPT_SUBST 531 PROMPT=$PROMPT'%{$(vterm_prompt_end)%}' 532 ``` 533 534 For `bash`, put this at the end of your `.bashrc`: 535 536 ```bash 537 vterm_prompt_end(){ 538 vterm_printf "51;A$(whoami)@$(hostname):$(pwd)" 539 } 540 PS1=$PS1'\[$(vterm_prompt_end)\]' 541 ``` 542 543 For `fish`, put this in your `~/.config/fish/config.fish`: 544 545 ```fish 546 function vterm_prompt_end; 547 vterm_printf '51;A'(whoami)'@'(hostname)':'(pwd) 548 end 549 functions --copy fish_prompt vterm_old_fish_prompt 550 function fish_prompt --description 'Write out the prompt; do not replace this. Instead, put this at end of your file.' 551 # Remove the trailing newline from the original prompt. This is done 552 # using the string builtin from fish, but to make sure any escape codes 553 # are correctly interpreted, use %b for printf. 554 printf "%b" (string join "\n" (vterm_old_fish_prompt)) 555 vterm_prompt_end 556 end 557 ``` 558 Here we are using the function `vterm_printf` that we have discussed above, so make 559 sure that this function is defined in your configuration file. 560 561 562 Directory tracking works on remote servers too. In case the hostname of your 563 remote machine does not match the actual hostname needed to connect to that 564 server, change `$(hostname)` with the correct one. For example, if the correct 565 hostname is `foo` and the username is `bar`, you should have something like 566 ```bash 567 HOSTNAME=foo 568 USER=baz 569 vterm_printf "51;A$USER@$HOSTNAME:$(pwd)" 570 ``` 571 572 ## Message passing 573 574 `vterm` can read and execute commands. At the moment, a command is 575 passed by providing a specific escape sequence. For example, to evaluate 576 ``` elisp 577 (message "Hello!") 578 ``` 579 use 580 ``` sh 581 printf "\e]51;Emessage \"Hello\!\"\e\\" 582 # or 583 vterm_printf "51;Emessage \"Hello\!\"" 584 ``` 585 586 The commands that are understood are defined in the setting `vterm-eval-cmds`. 587 588 As `split-string-and-unquote` is used the parse the passed string, double quotes 589 and backslashes need to be escaped via backslash. A convenient shell function to 590 automate the substitution is 591 592 `bash` or `zsh`: 593 ```sh 594 vterm_cmd() { 595 local vterm_elisp 596 vterm_elisp="" 597 while [ $# -gt 0 ]; do 598 vterm_elisp="$vterm_elisp""$(printf '"%s" ' "$(printf "%s" "$1" | sed -e 's|\\|\\\\|g' -e 's|"|\\"|g')")" 599 shift 600 done 601 vterm_printf "51;E$vterm_elisp" 602 } 603 ``` 604 `fish`: 605 ```sh 606 function vterm_cmd --description 'Run an Emacs command among the ones been defined in vterm-eval-cmds.' 607 set -l vterm_elisp () 608 for arg in $argv 609 set -a vterm_elisp (printf '"%s" ' (string replace -a -r '([\\\\"])' '\\\\\\\\$1' $arg)) 610 end 611 vterm_printf '51;E'(string join '' $vterm_elisp) 612 end 613 ``` 614 615 Now we can write shell functions to call the ones defined in `vterm-eval-cmds`. 616 617 ```sh 618 find_file() { 619 vterm_cmd find-file "$(realpath "${@:-.}")" 620 } 621 622 say() { 623 vterm_cmd message "%s" "$*" 624 } 625 ``` 626 627 Or for `fish`: 628 ```fish 629 function find_file 630 set -q argv[1]; or set argv[1] "." 631 vterm_cmd find-file (realpath "$argv") 632 end 633 634 function say 635 vterm_cmd message "%s" "$argv" 636 end 637 ``` 638 639 This newly defined `find_file` function can now be used inside `vterm` as 640 641 ```sh 642 find_file name_of_file_in_local_directory 643 ``` 644 If you call `find_file` without specifying any file (you just execute `find_file` in your shell), 645 `dired` will open with the current directory. 646 647 As an example, say you like having files opened below the current window. You 648 could add the command to do it on the lisp side like so: 649 650 ``` elisp 651 (push (list "find-file-below" 652 (lambda (path) 653 (if-let* ((buf (find-file-noselect path)) 654 (window (display-buffer-below-selected buf nil))) 655 (select-window window) 656 (message "Failed to open file: %s" path)))) 657 vterm-eval-cmds) 658 ``` 659 660 Then add the command in your `.bashrc` file. 661 662 ```sh 663 open_file_below() { 664 vterm_cmd find-file-below "$(realpath "${@:-.}")" 665 } 666 ``` 667 668 Then you can open any file from inside your shell. 669 670 ```sh 671 open_file_below ~/Documents 672 ``` 673 674 ## Shell-side configuration files 675 676 The configurations described in earlier sections are combined in 677 [`etc/`](./etc/). These can be appended to or loaded into your user 678 configuration file. Alternatively, they can be installed system-wide, for 679 example in `/etc/bash/bashrc.d/`, `/etc/profile.d/` (for `zsh`), or 680 `/etc/fish/conf.d/` for `fish`. 681 682 When using vterm Emacs sets the environment variable INSIDE_EMACS in the subshell to ‘vterm’. 683 Usually the programs check this variable to determine whether they are running inside Emacs. 684 685 Vterm also sets an extra variable EMACS_VTERM_PATH to the place where the vterm library is installed. 686 This is very useful because when vterm is installed from melpa the Shell-side configuration files are 687 in the EMACS_VTERM_PATH inside the /etc sub-directory. After a package update, the directory name changes, 688 so, a code like this in your bashrc could be enough to load always the latest version of the file 689 from the right location without coping any file manually. 690 691 ``` 692 if [[ "$INSIDE_EMACS" = 'vterm' ]] \ 693 && [[ -n ${EMACS_VTERM_PATH} ]] \ 694 && [[ -f ${EMACS_VTERM_PATH}/etc/emacs-vterm-bash.sh ]]; then 695 source ${EMACS_VTERM_PATH}/etc/emacs-vterm-bash.sh 696 fi 697 ``` 698 699 ## Frequently Asked Questions and Problems 700 701 ### How can I increase the size of the scrollback? 702 703 By default, the scrollback can contain up to 1000 lines per each vterm buffer. 704 You can increase this up to 100000 by changing the variable 705 `vterm-max-scrollback`. If you want to increase it further, you have to edit the 706 file `vterm-module.h`, change the variable `SB_MAX`, and set the new value for 707 `vterm-max-scrollback`. The potential maximum memory consumption of vterm 708 buffers increases with `vterm-max-scrollback`, so setting `SB_MAX` to extreme 709 values may lead to system instabilities and crashes. 710 711 ### How can I automatically close vterm buffers when the process is terminated? 712 713 There is an option for that: set `vterm-kill-buffer-on-exit` to `t`. 714 715 ### The package does not compile, I have errors related to `VTERM_COLOR`. 716 717 The version of `libvterm` installed on your system is too old. You should let 718 `emacs-libvterm` download `libvterm` for you. You can either uninstall your 719 libvterm, or instruct Emacs to ignore the system libvterm. If you are compiling 720 from Emacs, you can do this by setting: 721 ```emacs-lisp 722 (setq vterm-module-cmake-args "-DUSE_SYSTEM_LIBVTERM=no") 723 ``` 724 and compile again. If you are compiling with CMake, use the flag 725 `-DUSE_SYSTEM_LIBVTERM=no`. 726 727 ### `<C-backspace>` doesn't kill previous word. 728 729 This can be fixed by rebinding the key to what `C-w` does: 730 ```emacs-lisp 731 (define-key vterm-mode-map (kbd "<C-backspace>") 732 (lambda () (interactive) (vterm-send-key (kbd "C-w")))) 733 ``` 734 735 ### `counsel-yank-pop` doesn't work. 736 737 Add this piece of code to your configuration file to make `counsel` use 738 the correct function to yank in vterm buffers. 739 ```emacs-lisp 740 (defun vterm-counsel-yank-pop-action (orig-fun &rest args) 741 (if (equal major-mode 'vterm-mode) 742 (let ((inhibit-read-only t) 743 (yank-undo-function (lambda (_start _end) (vterm-undo)))) 744 (cl-letf (((symbol-function 'insert-for-yank) 745 (lambda (str) (vterm-send-string str t)))) 746 (apply orig-fun args))) 747 (apply orig-fun args))) 748 749 (advice-add 'counsel-yank-pop-action :around #'vterm-counsel-yank-pop-action) 750 ``` 751 752 ### How can I get the local directory without shell-side configuration? 753 754 We recommend that you set up shell-side configuration for reliable directory 755 tracking. If you cannot do it, a possible workaround is the following. 756 757 On most GNU/Linux systems, you can read current directory from `/proc`: 758 ```emacs-lisp 759 (defun vterm-directory-sync () 760 "Synchronize current working directory." 761 (interactive) 762 (when vterm--process 763 (let* ((pid (process-id vterm--process)) 764 (dir (file-truename (format "/proc/%d/cwd/" pid)))) 765 (setq default-directory dir)))) 766 ``` 767 A possible application of this function is in combination with `find-file`: 768 ```emacs-lisp 769 (advice-add #'find-file :before #'vterm-directory-sync) 770 ``` 771 This method does not work on remote machines. 772 773 ### How can I get the directory tracking in a more understandable way? 774 775 If you looked at the recommended way to set-up directory tracking, you will have 776 noticed that it requires printing obscure code like `\e]2;%m:%2~\a` (unless you 777 are using `fish`). 778 779 There is another way to achieve this behavior. Define a shell function, on a 780 local host you can simply use 781 782 ``` sh 783 vterm_set_directory() { 784 vterm_cmd update-pwd "$PWD/" 785 } 786 ``` 787 On a remote one, use instead 788 ``` sh 789 vterm_set_directory() { 790 vterm_cmd update-pwd "/-:""$USER""@""$HOSTNAME"":""$PWD/" 791 } 792 ``` 793 Then, for `zsh`, add this function to the `chpwd` hook: 794 795 ``` sh 796 autoload -U add-zsh-hook 797 add-zsh-hook -Uz chpwd (){ vterm_set_directory } 798 ``` 799 For `bash`, append it to the prompt: 800 801 ``` sh 802 PROMPT_COMMAND="${PROMPT_COMMAND:+$PROMPT_COMMAND; }vterm_set_directory" 803 ``` 804 Finally, add `update-pwd` to the list of commands that Emacs 805 is allowed to execute from vterm: 806 807 ``` emacs-lisp 808 (add-to-list 'vterm-eval-cmds '("update-pwd" (lambda (path) (setq default-directory path)))) 809 ``` 810 811 ### When evil-mode is enabled, the cursor moves back in normal state, and this messes directory tracking 812 813 `evil-collection` provides a solution for this problem. If you do not want to 814 use `evil-collection`, you can add the following code: 815 816 ```emacs-lisp 817 (defun evil-collection-vterm-escape-stay () 818 "Go back to normal state but don't move 819 cursor backwards. Moving cursor backwards is the default vim behavior but it is 820 not appropriate in some cases like terminals." 821 (setq-local evil-move-cursor-back nil)) 822 823 (add-hook 'vterm-mode-hook #'evil-collection-vterm-escape-stay) 824 ``` 825 826 827 ## Related packages 828 829 - [vterm-toggle](https://github.com/jixiuf/vterm-toggle): Toggles between a 830 vterm and the current buffer 831 - [multi-libvterm](https://github.com/suonlight/multi-libvterm): Multiterm for emacs-libvterm 832 833 ## Appendix 834 835 ### Breaking changes 836 837 Obsolete variables will be removed in version 0.1. 838 839 #### October 2020 840 841 * `vterm-disable-bold-font` was renamed to `vterm-disable-bold` to uniform it 842 with the other similar options. 843 844 #### July 2020 845 846 * `vterm-use-vterm-prompt` was renamed to `vterm-use-vterm-prompt-detection-method`. 847 * `vterm-kill-buffer-on-exit` is set to `t` by default. 848 849 #### April 2020 850 851 * `vterm-clear-scrollback` was renamed to `vterm-clear-scrollback-when-clearning`. 852 * `vterm-set-title-functions` was removed. In its place, there is a new custom 853 option `vterm-buffer-name-string`. See 854 [vterm-buffer-name-string](vterm-buffer-name-string) for documentation.