Is it possible to make the surround plugin work on `TeX-style quotes'
and ``TeX-style double quotes''
? (Normally, if `this is quoted'
| and `this'
, va'
will select from the first closing mark to the second (if the cursor is at |).)
-
See also the related question vi.stackexchange.com/questions/747/…– HotschkeCommented Aug 14, 2017 at 8:56
2 Answers
You can create a customize surround by defining a variable g:surround_{key code}
So for example you could do
let g:surround_96 = "`\r'"
Where 96 is the key code for `
. You can make this more readable by putting by letting vim determine the key code by using
let g:surround_{char2nr("`")} = "`\r'"
\r
will be replaced by the relevant text. If you only want this to work in tex files you can put it in an autocmd
autocmd FileType tex let b:surround_{char2nr("`")} = "`\r'"
Usage would be ysiw`
. Read :help surround-customizing
-
thanks! Two follow up quæstions: (1) I suppose to delete with
ds'
one has to replace of of the marks to match the other first? (2) What is the difference between usingautocmd FileType …
(in .vimrc?) and putting the command in.vim/after/tex.vim
?– ToothrotCommented Oct 10, 2015 at 10:42 -
@Lawrence (1) Most likely. (2) autocmd FileType is closer to placing the commands in
.vim/ftplugin/tex.vim
I don't think tex.vim would even get sourced in that location. The after director is normally for overriding command in the standard runtime (or plugins you don't want to edit)– FDinoffCommented Oct 10, 2015 at 13:31
An alternative choice of characters are
q
andQ
which do not overwrite a builtin surrounding (`
):autocmd FileType tex let b:surround_{char2nr("q")} = "`\r'" autocmd FileType tex let b:surround_{char2nr('Q')} = "``\r''"
(IMPORTANT) However, you cannot delete or change custom surroundings of
vim-surround
. Intuitively one would trydsq
orcsQq
which do not work. Also dot-repeat is not as convenient since you have to enter againq
.If you want to have this, consider switching to the plugin
vim-sandwich
. The default mappings are little bit different:saiwl'
would add`text'
,srbl"
would change it to``text''
, andsdb
would remove them again.sa
,sr
,sd
for add, replace and delete surrounding.l'
for latex single quotation marks,l"
for latex double quotation marks. It also defines a text-object (defaultib
), to allow for convenient selection withvib
what the OP has asked. It can be repeated to extend the selection withvibib...
.A convenience feature of
vim-surround
which is not well known is following shortcut: use the change command to actually add a surrounding to a word, WORD, sentence or paragraph with one of the special target specifiers[wWsp]
. To surround the current paragraph with a quotation the command iscspQ
.:h surround-targets
The letters w, W, and s correspond to a |word|, a |WORD|, and a |sentence|, respectively. These are special in that they have nothing to delete, and used with |ds| they are a no-op. With |cs|, one could consider them a slight shortcut for ysi (cswb == ysiwb, more or less).
A p represents a |paragraph|. This behaves similarly to w, W, and s above; however, newlines are sometimes added and/or removed.