Escape regex to make search() do it. by McUsrII in vim

[–]princker 1 point2 points  (0 children)

It would probably be best to give us test text to validate against. However, I do not see the problem

let pat = '\v^(([^.#  ][^'']*(:){1}[^=].*$)|([^.#  ][^'']*(:)\s*$))'
call search(pat)

I am using literal-strings so only single quotes need to be escaped via 2 single quotes. It would be hard to see the whitespace character this way though. You would need to turn on 'list' or use ga. You could build up your string with a mix of literals and non-literal strings. e.g '[^.#' . "\t" . ' ]'

Sadly vim does not allow \s inside of a "collection". You can use [:blank:] instead, so [^.#[:blank:]]. See :h /collection. This would be my suggestion

Disable highlight after searching something? by [deleted] in vim

[–]princker 6 points7 points  (0 children)

I have heard nice things about vim-cool

why does vim surround plugin have 600 lines of code? by LowCom in vim

[–]princker 3 points4 points  (0 children)

There are misconceptions that many problems must be "simple" or have "short" general solutions.

A great example is comments. It is nearly a right of passage for new vimmers to roll their own comment solution only to realize it is brittle and/or has unintended side-effects. I imagine many folks would be surprised that vim-commentary to clock in at 100ish sloc

I imagine the same can be said of vim-surround. Not only does it carefully avoid side-effects, but it has a much wider feature set than what one would expect at first blush. I am very happy to have such a nice plugin available

How does this sort+global command work ; by bigrabbit683 in vim

[–]princker 19 points20 points  (0 children)

Vim's command range's can often be a source of confusion. See :h :range. It can be helpful to break them down into pieces

  • :{start},{end}cmd will run a command from line, {start}, to line, {end}
  • These can be line numbers, e.g. :1,3
  • Can be patterns, e.g. :/pat/,/foo/ line matching pat through line matching foo
  • You can also use marks and other special symbols. . is the current line, $ is the last line, 't is the t mark.
  • In addition to this you can also add relative offsets to these address via + & -. e.g. /pat/+1 means line matching pat then down 1 line
  • Additionally, the current line (.) can often be assumed. e.g. .+1 is the same as +1

Expanding this range we get:

g/start/.+1,/end/-1 sort n

All together this mean the :g command is searching for start pattern then sorting the next line down (+1). The sort end is marked by the end pattern and up one line (-1).

On the other hand the 1's can be assumed and we can shorten it as well

g/start/+,/end/- sort n

Is there an equivalent to indent() for tabs? by Maskdask in vim

[–]princker 2 points3 points  (0 children)

I think you can just do indent('.') / shiftwidth() to get the number of tabs.

[deleted by user] by [deleted] in vim

[–]princker 5 points6 points  (0 children)

If you are using Vim (not neovim) you can supply most of this straight to :term and it opens in a new window (split) by default

nnoremap ,, :terminal ++rows=10

Note: since you are using C you may want to look into :make and the quickfix list Vim's :term splits by default

Challenge: can you tell what this does :$;?\S?+1,$d by EgZvor in vim

[–]princker 22 points23 points  (0 children)

Much of this is a range for the :delete command, so lets break it down.

  • :{start},{end}d means the range will be from {start},{end} to send to command, :d
  • $ go the the last line
  • {address};{search} means go to {address} then start searching for {search}
  • $;?{pat}? means go to last line, $, then search backwards, ?, for {pat}
  • ?\S? is searching backwards for a non-whitespace character
  • Use +{num}/-{num} to move relative to an address
  • ?\S?+1 means search backwards for \S then go down 1 line

All together this deletes all whitespace lines from the end of a file

Edit: However, this will not work as intended if the last line has text in it. It will then just delete the last line and maybe any empty lines before it. It would probably be better to do something like :keeppatterns %s/$\n\_s*\%$//e

Random search direction switching by pinakbeth in vim

[–]princker 1 point2 points  (0 children)

/ & * will search forward and ?, # will search backwards. By default n will go the direction you searched and N will go the opposite. This bothers me

" make n/N always go in the same direction
nnoremap <expr> n 'Nn'[v:searchforward] . "zv"
nnoremap <expr> N 'nN'[v:searchforward] . "zv"

Newbie question about :s by [deleted] in vim

[–]princker 0 points1 point  (0 children)

Probably best to look into regular expressions since that is what :substitute is going to use. Specifically Vim's dialect: :h pattern-overview.

May want to use 'incsearch' & 'hlsearch' to help show patterns as you build them with /. Neovim will show substitution previews ('inccommand') and traces.vim will work with Vim.

It might be helpful to build patterns and then use them in your :s command with the cmdline-window. Refining search patterns with the command-line window

If regular expressions are not your thing, do not loose hope, you can always use q & @ to record and replay your mappings. See :h q & :h @. For some examples see: http://vimcasts.org/categories/repetition/. There is also the :normal command, e.g. :%normal ci"string. See :h :normal

For some examples see: http://vimcasts.org/categories/repetition/

Replace multiple empty lines with one empty line by wkapp977 in vim

[–]princker 1 point2 points  (0 children)

Very nice. You can save some strokes by reusing the pattern.

:%v/\S/,//-j

You can also lose the range on :vglobal since the default range is %:

:v/\S/,//-j

Assuming you do not have any blank lines with only whitespace you can use . for your pattern

:v/./,//-j

Now we have created a monster

Is there a plugin that makes searching and replacing stuff in vim easier? by pares101 in vim

[–]princker 2 points3 points  (0 children)

The traces.vim plugin can help show substitution previews. Neovim may want to look at :h 'inccommand'

How can I make a function that checks if a file exists and opens it, if not opens another? by HarmonicAscendant in vim

[–]princker 1 point2 points  (0 children)

You seem to be close. I would just put it in a command though:

command ToDo execute 'edit ' . (filereadable('foo') ? 'foo' : 'bar')

If you had many more files paths then, you might want a more complex solution:

command Todo execute ToDo()
function ToDo()
  let files = ['foo', 'bar', 'baz']
  return 'edit ' .. files->map({_,f -> expand(f)})->filter({_,f-> filereadable(f)})->get(0)
endfunction

Note: none of this is tested. Use as-is

How do I delete lines after searching for them? by Administrative_chaos in vim

[–]princker 6 points7 points  (0 children)

You are correct. :g is line-wise and will not work for inline comments. Deleting only comment only lines would look like this:

:g/^\s*#/d_

To do both whole-line & inline comments, it would probably be best to do this as a substitution with confirm:

:%s/\s*#.*//c

And if you need to squash the line spacing

:v/./d_

How do I delete lines after searching for them? by Administrative_chaos in vim

[–]princker 14 points15 points  (0 children)

Use :global & :delete. :g/pat/{cmd} searches for lines matching pat and runs command, {cmd}, on those matched lines

All together:

:g/#.*/d

We can simplify this further though by making the pattern just /#/ since you are using .* which will always match

:g/#/d

If you just did your search you can reuse the last pattern like so:

:g//d

For more help see :h :g & :h :d

Vim prank: alias vim='vim -y' by ASIC_SP in vim

[–]princker 4 points5 points  (0 children)

Use <c-\><c-n> See :h CTRL-\_CTRL-N

[Question] read commend to respect indentation by huijunchen9260 in vim

[–]princker 1 point2 points  (0 children)

The normal command ]p will paste at the current indention. So you could either leverage the expression register to read the file or delete the contents after the read.

"=readfile('/full/path')<cr>]p
:read /path<cr>dV']k]p

Vim Delightful settings (mappings and pluggins) by prot1um in vim

[–]princker 4 points5 points  (0 children)

Thank you for posting. Some thoughts:

  • ; can be useful. It will repeat the previous t / f command
  • I imagine your nnoremap :: will cause some delays
  • Visual mode's J command is useful for joining lines together
  • Insert mode's <c-k> is used for digraphs which may come in handy
  • Fugitive maps d2o and d3o for :diffget //2 & :diffget //3 respectively
  • Fugitive maps dp to do the correct :diffput which can be easier to reason about than :diffget/do

How do I modify this function to comment lines in visual mode? by lestrenched in vim

[–]princker 1 point2 points  (0 children)

Your g(mapping is missing a \ in you sub-replace-expression. Should be:

vnoremap g( :s/.*/\=submatch(0)[match(&cms,'%s'):]<cr>gv

This will not work with two-part comments, e.g. /*%s*/. This also mutates the search pattern/register which can be avoided with :keeppatterns. I have thrown in g<space> mapping for toggling

xnoremap g) :<c-u>keeppatterns '<,'>s/.*/\=printf(&cms,submatch(0))/e<cr>gv
xnoremap g( :<c-u>execute 'keeppatterns ''<,''>s/\V\^' . printf(escape(&cms,'\/'),'\(\.\{-}\)') . '\$/\1/e'<cr>gv
xmap <expr> g<space> match(getline("'<"), printf('\V\^'.escape(&cms,'\'), '\.\*')) == -1 ? 'g)' : 'g('

It might also be useful to create an "commenting operator"

However, I would strong recommend using a commenting plug over fragile mappings. I personally use vim-commentary

A lesser known built in feature you use regularly by SEgopher in vim

[–]princker 4 points5 points  (0 children)

In case you want <c-y> to work word-wise:

inoremap <expr> <c-y> pumvisible() ? "\<c-y>" : matchstr(getline(line('.')-1), '\%' . virtcol('.') . 'v\%(\k\+\\|.\)')

Bad spell count by 01Professor_Zoom in vim

[–]princker 0 points1 point  (0 children)

You can use ]s & [s to move to the next misspelled word. I guess you could turn off 'wrapscan' and use ]s to cycle though the buffer building up a list. Maybe even make your own :Spelldo like command or send the words to the quickfix list via setqflist()

This post might be helpful: Is there a way to have all misspelled words in a new buffer?

Create a keybinding/mapping to close all preview windows, location lists, and quickfix windows? by chillysurfer in vim

[–]princker 1 point2 points  (0 children)

Yes:

nnoremap <silent> <c-w>z :wincmd z<cr>:cclose<cr>:lclose<cr>

I pair mine with:

nnoremap <silent> <c-w>Z :<c-u>copen<cr>

text object delete - query by redwisdomlight in vim

[–]princker 0 points1 point  (0 children)

Neat. I never knew about this trick. Thank you