Surround Visual Text with more than 1 character - vim

How would you surround the following text with 3 backticks ``` using tpope's Vim Surround.
All I can is 1 backtick using S` in visual mode:

This is not what you asked but this can be done without surround:
(from visual mode)
c
```
<C-r>"
```
<Esc>
See :help ctrl-r.

Define a custom surround:
(Insert following in your .vimrc or file specific config ~/.vim/after/ftplugin/markdown.vim )
" Custom surrounds
let b:surround_{char2nr('c')} = "```\r```"
now visual select and Sc will give you desired surround.
Or use a snippet solution; for example using Ultisnips define a snippet like so:
snippet code
\`\`\`${1}
${0:${VISUAL}}
\`\`\`
endsnippet
now visual select your desired lines then hit snippet expansion key ( mine is Tab ) type code and hit Tab again. that's it.

Here another ultisnips solution.
snippet code "add backtics codes" w
`!v repeat(nr2char(96),3)` ${1:markdown}
${0:${VISUAL:type here}}
`!v repeat(nr2char(96),3)`
endsnippet
If you do not want "markdown" after the first line just get rid of it. I am showing this solution only to show how to avoid backslashing so much.

Related

Vim escape backslash paste

I am having the following text in vim:
Hello, \/Good\/Bye.
Now I want to change it to:
Hello, -Good-Bye.
So I use: Visual Select, Yank the Line. then
%s/<Ctrl+R>+"/-/gc
When I push CTRL+R+" The line clones into the search, but It \/ will not work. I need to change it to \\/ . Is there a quick function or keystroke to automatically escape the chars? Please let me know. Thank you!
As phd wrote in comments, you can use \V and escape() to do it. For example, write the following function:
function! Regesc(text)
return '\V' . escape(a:text, '/\')
endf
Then after yanking some text, use it like this:
:%s/Ctrl+R=Regesc(##)CR/-/gcCR
This will display and run the following command, depending of what you yanked:
:%s/\VHello, \\\/Good\\\/Bye./-/gc
You could use another delimiter after your visual selection
:'<,'>s,\%V\\/\%V,-,gc
\\ ......... scaped backslash
\%V ........ visual selection area
So you have to put your pattern \\/ inside a visual delimiter

Vim - `=` (equalprg) doesn't use expandtab.

I am a python developer and I work for a company where the python convention for indentation is two spaces rather than four.
So I notice this problem when I paste code from vim:
This is the text I am trying to paste:
def four_spaces():
print "hello"
This text is copied in mac from Chrome using <cmd> c. Note that I am using vim in iTerm in the Mac OS X.
Below are the steps for me to pasting this in vim using as I am using a mac:
:set paste Turn Paste mode on
a Append mode
<Cmd> v Pasting in Mac
Result shown in Snippet 4. Note that it's 4 spaces
v kk *Visual mode to select the above
= Fix indentation
Result shown in Snippet 7. Note the ^I
Snippet 4 (listmode on)
def four_spaces():$
print "hello"$
Snippet 7 (listmode on)
def four_spaces():$
^Iprint "hello"$
This is what my .vimrc file looks like for those interested:
https://gist.github.com/anonymous/20b2a1f43125c0d39932bf430c8137dc
Problem:
The problem I am facing with right now is that I have a tab character rather than 2 space when I use =. This is problematic because python relies on tab indentation and it cannot allow tab characters and spaces to co-exist together. I am trying to figure out how come = doesn't use expandtab.
Question:
How can I paste using <Cmd> v and such that:
Vim formats my code properly to 2 spaces
OR how can I get vim's equalprg (=) to use expandtab?

Vim Ultisnips, visual token and system clipboard

I am quite new to vim but I found it to be powerful tool (especially with plugins such as UltiSnips and YouCompleteMe). I quite like the idea of creating personal snippets in conjunction with visual token to perform quick text transformations. However I would like to be able to select some text in web browser, copy it to clipboard and simply type my_snippet in vim to expand my_snippet with text from clipboard as visual token. It would be quite useful but I have no idea how to arrange it. It would be great if somebody can instruct me how to set up vim to do this.
I will try to give you an example, an issue I had for some time. I am constantly trying out new plugins so, copying the github project name like:
psliwka/vim-smoothie
And transforming it on:
Plug 'psliwka/vim-smoothie'
Is something problematic, because along with typing the word Plug at the beggining of the line I have to type the single quotes and remove three spaces and one caridge return
psliwka /
vim-smoothie
So my snippet has to wrap it all in one go. After some research I ended up with this:
snippet plug "Insert a new plugin on my vimrc" w
Plug '${1:`!v substitute(#+, ' \|\n', '', 'g' )`}'
endsnippet
Some Explanation: The !v part of the snippet means "vim interpolation", so I can use some vim commands. The substitute() command rules are:
where-substitue, what-substitute, substitution, flags
| | | |
#+ ' \|\n' '' g
clipboard space or enter nothing globally

Vim search replace to end of code block

I'd like to search and replace to the end of the code block I'm in.
Essentially I want to search over the motion ]}.
I tried
:]}s/=/+=/gc
and it didn't work. "Parse error"
I'm working with VsVIM in Visual Studio if that makes a difference.
The : ranges do not take motion arguments. You could do this via visual mode:
v]}:s/=/+=/gc
Note that when you hit : in visual mode, it will embed the ranges for you.
You can select the block with
{
Shift-v
}
then make:
:s/src/dst/g
I'm not sure if that will work in VsVim but you could do something like this in Vim:
[{v]}:s/=/+=/gc
Note that you can use a numeric range too:
:56,75s/=/+=/gc
As hepta said, you could do visual mode then your replace.
You could also do :.,.5s/old/new/gc
Or :.,/}/-1s/old/new/gc
See vim.wikia.com/wiki/Ranges

vim: insert word with vim-surround

I use surround vim plugin.
when I write a latex source for make a word to \bf similar to
hello w*orld
( * is the cursor position)
I use
ysiw}a\bf<space>
and get
hello {\bf *world}
exist a more simple way? or how I can insert \bf automatically?
I solved added this in my ~/.vimrc
let g:surround_42 = "{\\bf \r}"
and call over the word in normal mode
ysiw*
EDIT
is valid for the complete line with
yss*

Resources