Skip to content
This repository
Tom Feist April 28, 2012
file 618 lines (352 sloc) 15.875 kb

NAME

vim_mode.pl

DESCRIPTION

An Irssi script to emulate some of the vi(m) features for the Irssi inputline.

INSTALLATION

Copy into your ~/.irssi/scripts/ directory and load with /SCRIPT LOAD vim_mode.pl. You may wish to have it autoload in one of the usual ways.

DEPENDENCIES

For proper :ex mode support, vim-mode requires the installation of uberprompt.pl Uberprompt can be downloaded from:

https://github.com/shabble/irssi-scripts/raw/master/prompt_info/uberprompt.pl

and follow the instructions at the top of that file for installation.

If you don't need Ex-mode, you can run vim_mode.pl without the uberprompt.pl script, but it is strongly recommended that you use it.

Irssi requirements

0.8.12 and above should work fine. However the following features are disabled in irssi < 0.8.13:

  • j k (only with count, they work fine without count in older versions)
  • gg, G

It is intended to work with at Irssi 0.8.12 and later versions. However some features are disabled in older versions (see below for details).

Perl >= 5.8.1 is recommended for UTF-8 support (which can be disabled if necessary). Please report bugs in older versions as well, we'll try to fix them. Later versions of Perl are also faster, which is probably beneficial to a script of this size and complexity.

SETUP

Vim Mode provides certain feedback to the user, such as the currently active mode (command, insert, ex), and if switching buffers, which buffer(s) currently match the search terms.

There are two ways to go about displaying this information, as described in the following sections:

Statusbar Items

Run the following command to add a statusbar item that shows which mode you're in.

/statusbar window add vim_mode

And the following to let :b [str] display a list of channels matching your search string.

/statusbar window add vim_windows

Note: Remember to /save after adding these statusbar items to make them permanent.

Note: If you would rather have these statusbar items on your prompt line rather than thte window statusbar, please follow these steps.

For technical reasons, uberprompt must occasionally call /statusbar prompt reset, which will remove or deactivate any manually added items on the prompt statusbar. To get around this, uberprompt provides two command hooks, uberprompt_load_hook and uberprompt_unload_hook. Both of these settings can contain one (or more, using /EVAL) commands to be executed when the prompt is enabled and disabled, respectively.

See the uberprompt documentation for further details.

For right-aligned items (that is, after the input field:

1 /alias vm_add /^statusbar prompt add -after input -alignment right vim_mode
2 /alias vm_del /^statusbar prompt remove vim_mode
3 /set uberprompt_load_hook /^vm_add
4 /set uberprompt_unload_hook /^vm_del

For left-aligned items (before the prompt):

1 /alias vm_add /^statusbar prompt add -before prompt -alignment left vim_mode
2 /alias vm_del /^statusbar prompt remove vim_mode
3 /set uberprompt_load_hook /^vm_add
4 /set uberprompt_unload_hook /^vm_del

If you wish to add both vim_mode and vim_windows items, replace steps 1 and 2 above with the following (right-aligned):

1 /alias vm_add /^eval /^statusbar prompt add -after input -alightment right vim_mode ; /^statusbar prompt add -after input -alignment right vim_windows
2 /alias vm_del /^eval /^statusbar prompt remove vim_mode ; /^statusbar prompt remove vim_windows

and then complete stages 3 and 4 as above. Replace the -after and -alignment to suit your location preferences.

Note: It is also possible to place the items between the prompt and input field, by specifying -after prompt or -before input as appropriate.

Expando Variables

Vim mode augments the existing Irssi expando (automatic variables) with two additional ones: $vim_cmd_mode and $vim_wins.

$vim_cmd_mode is the equivalent of the vim_mode statusbar item, and $vim_wins is the counterpart of vim_windows.

They can be added to your theme, or inserted into your uberprompt using a command such as:

"/set uberprompt_format [$vim_cmd_mode] $*$uber] "

Quick Window Switching

As an alternative to using Esc,N for switching windows, you may wish to consider switching to using Alt+N. This is typically faster, and significantly easier when using vim_mode.pl. Additionally, you can use Alt+[qwertyuio] to access windows 11-19 (the letter corresponds to the number northwest of it on the keyboard).

For xterm users, the default configuration is to emit a only a small set of non-ASCII chars, such as superscript-2 when you hit Alt+2. There are more modern ways to input such characters, and they get in the way of the above bindings. The commands:

    echo '*VT100*metaSendsEscape: true' >> ~/.Xresources
    xrdb -merge ~/.Xresources

can be used to change the behaviour of xterm to allow the Alt-x bindings The xrdb command is only be necessary for the existing X session, as the .Xresources file is read by default in most X startup scripts.

If it is not, you can add that line to your ~/.xsession or ~/.xinitrc, whichever is in use. Furthermore, if you want to change this setting on a running xterm (for example, because you're running irssi directly, not from within GNU Screen or tmux (although you probably should be), you can use editres. It is one of those crufty old X11 applications that is not very well known or used in recent times. To use it, go to Commands > Get Tree, then click on the xterm, then click on the vt100 box, then go to Commands > Show Resource Box. From there the metaSendsEscape resource should be visible so you can click on it (if it isn't, fiddle with the odd scrollbar on the left of the window). Finally, enter true in the "Enter Resource Value" box and hit Apply.

FILE-BASED CONFIGURATION

Additionally to the irssi settings described in settings, vim_mode can be configured through an external configuration file named "vim_moderc" located in ~/.irssi/vim_moderc. If available it's loaded on startup and every supported ex-command is run. Its syntax is similar to "vimrc". To (re)load it while vim_mode is running use :so[urce].

Currently Supported ex-commands:

  • :map

USAGE

The following section is divided into the different modes as supported by Vim itself:

COMMAND MODE

It supports most commonly used command mode features:

  • Insert/Command mode.

    Esc and Ctrl-C enter command mode. /set vim_mode_cmd_seq j allows to use jj as Escape (any other character can be used as well).

  • Cursor motion:

    h l 0 ^ $ <Space> <BS> f t F T

  • History motion:

    j k gg G gg moves to the oldest (first) history line. G without a count moves to the current input line, with a count it goes to the count-th history line (1 is the oldest).

  • Cursor word motion:

    w b ge e W gE B E

  • Word objects (only the following work yet):

    aw aW

  • Yank and paste:
     C<y p P>
  • Change and delete:

    c d

  • Delete at cursor:

    x X

  • Replace at cursor:

    r

  • Insert mode:

    i a I A

  • Switch case:

    ~

  • Repeat change:

    .

  • Repeat

    ftFT: ; ,

  • Registers:

    "a-"z "" "0 "* "+ "_ (black hole)

  • Line-wise shortcuts:

    dd cc yy

  • Shortcuts:

    s S C D

  • Scroll the scrollback buffer:

    Ctrl-E Ctrl-D Ctrl-Y Ctrl-U Ctrl-F Ctrl-B

  • Switch to last active window:

    Ctrl-6/Ctrl-^

  • Switch split windows:

    <Ctrl-W j Ctrl-W k>

  • Undo/Redo:

    u Ctrl-R

Counts and combinations work as well, e.g. d5fx or 3iabc<esc>. Counts also work with mapped ex-commands (see below), e.g. if you map gb to do :bn, then 2gb will switch to the second next buffer. Repeat also supports counts.

REGISTERS

  • Appending to register with "A-"Z
  • "" is the default yank/delete register.
  • "0 contains the last yank (if no register was specified).
  • The special registers "* "+ both contain irssi's internal cut-buffer.

INSERT MODE

The following insert mode mappings are supported:

  • Insert register content: Ctrl-R x (where x is the register to insert)

EX-MODE

Ex-mode (activated by : in command mode) supports the following commands:

  • Command History:

    <uparrow> - cycle backwards through history

    <downarrow> - cycle forwards through history

    :eh - show ex history

  • Switching buffers:

    :[N]b [N] - switch to channel number

    :b# - switch to last channel

    :b <partial-channel-name>

    :b <partial-server>/<partial-channel>

    :buffer {args} (same as :b)

    :[N]bn[ext] [N] - switch to next window

    :[N]bp[rev] [N] - switch to previous window

  • Close window:

    :[N]bd[elete] [N]

  • Display windows:

    :ls, :buffers

  • Display registers:

    :reg[isters] {args}, :di[splay] {args}

  • Display undolist:

    :undol[ist] (mostly used for debugging)

  • Source files:

    :so[urce] - only sources vim_moderc at the moment, {file} not supported

  • Mappings:

    :map - display custom mappings

  • Saving mappings:

    :mkv[imrc][!] - like in Vim, but [file] not supported

  • Substitution:

    :s/// - i and g are supported as flags, only /// can be used as eparator, and it uses Perl regex syntax instead of Vim syntax.

  • Settings:

    :se[t] - display all options

    :se[t] {option} - display all matching options

    :se[t] {option} {value} - change option to value

MAPPINGS

Commands

  • :map {lhs} - display mappings starting with {lhs}
  • :map {lhs} {rhs} - add mapping
  • :unm[ap] {lhs} - remove custom mapping

Parameters

{lhs} is the key combination to be mapped, {rhs} the target. The <> notation is used

(e.g. <C-F> is Ctrl-F), case is ignored. Supported <> keys are:

  • <C-A> - <C-Z>,
  • <C-^>, <C-6>
  • <Space>
  • <CR> - Enter
  • <BS> - Backspace
  • <Nop> - No-op (Do Nothing).

Mapping ex-mode and irssi commands is supported. When mapping ex-mode commands the trailing <Cr> is not necessary. Only default mappings can be used in {rhs}.

Examples:

  • :map w W - to remap w to work like W
  • :map gb :bnext - to map gb to call :bnext
  • :map gB :bprev
  • :map g1 :b 1 - to map g1 to switch to buffer 1
  • :map gb :b - to map gb to :b, 1gb switches to buffer 1, 5gb to 5
  • :map <C-L> /clear - map Ctrl-L to irssi command /clear
  • :map <C-G> /window goto 1
  • :map <C-E> <Nop> - disable <C-E>, it does nothing now
  • :unmap <C-E> - restore default behavior of <C-E> after disabling it

Note that you must use / for irssi commands (like /clear), the current value of Irssi's cmdchars does not work! This is necessary do differentiate between ex-commands and irssi commands.

SETTINGS

The settings are stored as irssi settings and can be set using /set as usual (prepend vim_mode_ to setting name) or using the :set ex-command. The following settings are available:

  • utf8 - Support UTF-8 characters, boolean, default on
  • debug - Enable debug output, boolean, default off
  • cmd_seq - Char that when double-pressed simulates <Esc>, string, default '' (disabled)
  • start_cmd - Start every line in command mode, boolean, default off
  • max_undo_lines - Sze of the undo buffer. Integer, default 50 items.
  • ex_history_size - Number of items stored in the ex-mode history. Integer, default 100.
  • prompt_leading_space - Ddetermines whether ex mode prepends a space to the displayed input. Boolean, default on

In contrast to irssi's settings, :set accepts 0 and 1 as values for boolean settings, but only vim_mode's settings can be set/displayed.

Examples:

   :set cmd_seq=j   # set cmd_seq to j
   :set cmd_seq=    # disable cmd_seq
   :set debug=on    # enable debug
   :set debug=off   # disable debug

SUPPORT

Any behavior different from Vim (unless explicitly documented) should be considered a bug and reported.

NOTE: This script is still under heavy development, and there may be bugs. Please submit reproducible sequences to the bug-tracker at: http://github.com/shabble/irssi-scripts/issues/new

or contact rudi_s or shabble on irc.freenode.net (#irssi and #irssi_vim)

AUTHORS

Copyright © 2010-2011 Tom Feist <shabble+irssi@metavore.org> and

Copyright © 2010-2011 Simon Ruderich <simon@ruderich.org>

THANKS

Particular thanks go to

  • estragib: a lot of testing and many bug reports and feature requests
  • iaj: testing
  • tmr: explaining how various bits of vim work

as well as the rest of #irssi and #irssi_vim on Freenode IRC.

LICENCE

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

BUGS

  • count before register doesn't work: e.g. 3"ap doesn't work, but "a3p does
  • mapping an incomplete ex-command doesn't open the ex-mode with the partial command (e.g. :map gb :b causes an error instead of opening the ex-mode and displaying :b<cursor>)
  •  undo/redo cursor positions are mostly wrong

TODO

  • History:
    •  C< * /,?,n,N> to search through history (like rl_history_search.pl)
  • Window switching (:b)
    • Tab completion of window(-item) names
    • non-sequential matches(?)

See also the TODO file at github for many many more things.

WONTFIX

Things we're not ever likely to do:

  • Macros
Something went wrong with that request. Please try again.