Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Locked thread
Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed
| is the statement separator, similar to ; in many languages. g invokes everything after it on each line matching the pattern. The thing after it is two statements: a replace operation on that line, and a let to increment the variable. The number increments because only the stuff after g// is run for each match, not the stuff before it. Here is that line translated to a more verbose pseudocode:
code:
let n = 1
for line in buffer.match('<p>'):
    line.replace('<p>', '<p id=' + n + '">")
    let n = n + 1

Adbot
ADBOT LOVES YOU

The Laplace Demon
Jul 23, 2009

"Oh dear! Oh dear! Heisenberg is a douche!"

ArcticZombie posted:

code:
let n=1 | g/<p>/s/<p>/\='<p id="'.n.'">'/ | let n=n+1

See :help sub-replace-expression or :help sub-replace-\=.

:s/<p>/\='<p id="'.n.'">'/ is replacing each <p> with the concatenation of the string <p id=", the contents of the vimscript variable n and the string ">.
. is the concatenation operator in vimscript (see :help expr-.).

The vertical bar is basically just the equivalent of a semicolon in C; it allows multiple expressions on the same line. See :help :bar. I say basically because it has different behavior when following certain commands, :g (a.k.a. :global) being one of them.

Also check out :help :g. That command has two arguments (separated by / or whatever). The first is the pattern it matches, the second is the Ex command it runs on those lines. In this case, it matches any line containing <p> and replaces it as explained above.

Then follows the nonstandard behavior of the vertical bar used after :g. Instead of just moving on to the next match, it first runs the command after the bar and thus increments n. Only after that command finishes does it move on to the next matching line.

And to leave no stone unturned, :let declares an internal vimscript variable. n is still going to be there after the command finishes running, so you can use it afterwards if you'd like.

Hope that helps! :)

fidel sarcastro posted:

In fact, the whole thing sounds like it's behaving like a C-style for loop but a quick search says Vim doesn't have any support for them?

:help for / :for :confused:

EDIT:
For example:
:for i in range(10) | echo i | endfor

The Laplace Demon fucked around with this message at 16:43 on Aug 11, 2013

ArcticZombie
Sep 15, 2010
Thanks fellas. I don't often use vim, in fact I only used it in this instance because of the ability to set a variable and increment it like that which you can't do with just plain regex in other editors.

jony neuemonic
Nov 13, 2009

The Laplace Demon posted:

:help for / :for :confused:

EDIT:
For example:
:for i in range(10) | echo i | endfor

Whoops, I apparently mistook "doesn't support the exact same syntax" for "doesn't support for loops." Edited my post.

DreadCthulhu
Sep 17, 2008

What the fuck is up, Denny's?!
Need a recommendation for convenient bindings for quickfix list commands. Mostly open, close, next, previous etc. What do you guys use?

Been playing with Ack.vim + AG and would love to make that navigation snappier.

Experimenting with the following:

code:
nnoremap <silent> [c :cprev<CR>
nnoremap <silent> ]c :cnext<CR>
nnoremap <silent> [C :cfirst<CR>
nnoremap <silent> ]C :clast<CR>
and this plugin for toggling the lists.

DreadCthulhu fucked around with this message at 01:56 on Aug 18, 2013

Deus Rex
Mar 5, 2005

DreadCthulhu posted:

Need a recommendation for convenient bindings for quickfix list commands. Mostly open, close, next, previous etc. What do you guys use?

Been playing with Ack.vim + AG and would love to make that navigation snappier.

Experimenting with the following:

code:
nnoremap <silent> [c :cprev<CR>
nnoremap <silent> ]c :cnext<CR>
nnoremap <silent> [C :cfirst<CR>
nnoremap <silent> ]C :clast<CR>
and this plugin for toggling the lists.

I'm a big fan of tim pope's vim-unimpaired:

https://github.com/tpope/vim-unimpaired

DreadCthulhu
Sep 17, 2008

What the fuck is up, Denny's?!
Oh man that's got a LOT of stuff in it, I'll have to do some cherry picking.

DreadCthulhu
Sep 17, 2008

What the fuck is up, Denny's?!
Just wanted to re-emphasize how amazing that PragProg book for Vim is. If you don't know where to start or where to go, seriously consider checking it out. Just finished reading it after over a year, and I still need to go back and review a lot of its tricks.

DreadCthulhu
Sep 17, 2008

What the fuck is up, Denny's?!
Something totally awesome I just discovered, not sure how I lived without this:

Want to work on two or more repos in one vim? Open a new tab with :tabnew, :lcd %repo_path% and now all the windows spawned in that new tab will inherit the same :pwd and you can CTRL-P your worries away. For bonus points, if you already have a tab with a bunch of open windows whose pwd is all over the place, just :windo lcd %repo_path% and you're golden.

smug forum asshole
Jan 15, 2005
anyone using powerline for vim? https://github.com/Lokaltog/powerline

jony neuemonic
Nov 13, 2009


I'm using airline, same idea though. They're both really good.

Chin Strap
Nov 24, 2002

I failed my TFLC Toxx, but I no longer need a double chin strap :buddy:
Pillbug

fidel sarcastro posted:

I'm using airline, same idea though. They're both really good.

Yeah I've found airline to be more lightweight and easier to deal with.

smug forum asshole
Jan 15, 2005
Thanks guys, airline seems great.

I discovered that powerline in vim would sometimes interfere with my ability to do things in git (couldn't get control of git's index.lock) because it was polling for git status and branchname or something. It made interactive rebase incredibly painful, especially when manipulating large ranges of commits. Maybe it's just because I was using an outdated version of powerline, but for now I'll just see how things go with airline.

jony neuemonic
Nov 13, 2009

I finally got motivated to sit down and rewrite my vim config. Despite getting over the "turn VIM into an IDE" bug and slimming it down by 1/3rd it's still 125 lines. :psyduck:

Sure runs a lot better, though.

Tiger.Bomb
Jan 22, 2012
Yeah Airline is nice. I held off for months but finally patched my fonts and they do look a lot nicer (I use inconsolata). Also have a vim plugin that sets my tmux status bar to match the vim one (But you can change its colour scheme if you want).

FamDav
Mar 29, 2008
How would I go about removing leading blank lines from a buffer in vim?

Bogan King
Jan 21, 2013

I'm not racist, I'm mates with Bangladesh, the guy who sells me kebabs. No, I don't know his real name.
A nice little video to show you just how far off a VIM master you really are.

Tiger.Bomb
Jan 22, 2012

FamDav posted:

How would I go about removing leading blank lines from a buffer in vim?

gg/.<CR>kdgg

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Ever since I learned about the find character command (f) I use ; a lot.

I learned about it in some video that I can't find now, I guess it was by the Practical Vim guy?

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

That drag visual block function at the end is nice!

Tiger.Bomb
Jan 22, 2012

Symbolic Butt posted:

Ever since I learned about the find character command (f) I use ; a lot.

I learned about it in some video that I can't find now, I guess it was by the Practical Vim guy?

vimcasts.com

Cat Plus Plus
Apr 8, 2011

:frogc00l:
I recently found out that you can now set 'relativenumber' and 'number' at the same time, and vim will show an absolute line number for current line and relative line numbers for the rest.

jony neuemonic
Nov 13, 2009

Cat Plus Plus posted:

I recently found out that you can now set 'relativenumber' and 'number' at the same time, and vim will show an absolute line number for current line and relative line numbers for the rest.



And if you use numbers.vim you also get relative numbering in normal mode, and absolute in insert mode as a bonus. :science:

Tiger.Bomb posted:

gg/.<CR>kdgg

Intuitive.

Marsol0
Jun 6, 2004
No avatar. I just saved you some load time. You're welcome.

fidel sarcastro posted:

And if you use numbers.vim you also get relative numbering in normal mode, and absolute in insert mode as a bonus. :science:

I get something similar with:
code:
" Relative Numbering
set number
set relativenumber
autocmd InsertEnter * :set relativenumber!
autocmd InsertLeave * :set relativenumber
autocmd WinEnter * :setlocal relativenumber
autocmd WinLeave * :setlocal relativenumber!

Symbolic Butt
Mar 22, 2009

(_!_)
Buglord

Tiger.Bomb posted:

vimcasts.com

Oh it was this one http://vimcasts.org/blog/2010/11/arrrrcamp-presentation-vim---walking-without-crutches/ thanks!

jony neuemonic
Nov 13, 2009

Marsol0 posted:

I get something similar with:
code:
" Relative Numbering
set number
set relativenumber
autocmd InsertEnter * :set relativenumber!
autocmd InsertLeave * :set relativenumber
autocmd WinEnter * :setlocal relativenumber
autocmd WinLeave * :setlocal relativenumber!

Slick, I'll have to tinker with that. Not that numbers is an overly large plugin but hey, one less thing to install.

Chin Strap
Nov 24, 2002

I failed my TFLC Toxx, but I no longer need a double chin strap :buddy:
Pillbug
As someone who is a really dedicated vim user, is there a reason to try eclim? I don't write Java. I'm not sure what IDE features from Eclipse I'm actually missing out on. My primary languages are Go, R, and Python. Is the stuff from Eclipse just more useful for Java/C++?

Maluco Marinero
Jan 18, 2001

Damn that's a
fine elephant.

Chin Strap posted:

As someone who is a really dedicated vim user, is there a reason to try eclim? I don't write Java. I'm not sure what IDE features from Eclipse I'm actually missing out on. My primary languages are Go, R, and Python. Is the stuff from Eclipse just more useful for Java/C++?

Thanks for bringing that up. I'm starting to work in Scala more and it just so happens that the Scala IDE from Typesafe is done with Eclipse so it'd be great if I can connect vim to that.

Cat Plus Plus
Apr 8, 2011

:frogc00l:

Chin Strap posted:

As someone who is a really dedicated vim user, is there a reason to try eclim? I don't write Java. I'm not sure what IDE features from Eclipse I'm actually missing out on. My primary languages are Go, R, and Python. Is the stuff from Eclipse just more useful for Java/C++?

You're missing out on fun features like forgetting how to render text properly, randomly occurring internal errors, and "restart" option built-in. Eclipse is not very good.

For Python you could take a look at PyCharm, which recently got a free version (it also has Vim emulator, IdeaVim; haven't used it though).

Chin Strap
Nov 24, 2002

I failed my TFLC Toxx, but I no longer need a double chin strap :buddy:
Pillbug

Cat Plus Plus posted:

You're missing out on fun features like forgetting how to render text properly, randomly occurring internal errors, and "restart" option built-in. Eclipse is not very good.

For Python you could take a look at PyCharm, which recently got a free version (it also has Vim emulator, IdeaVim; haven't used it though).

I'm not all that interested in switching away from my highly custom vim without good reason. I see a lot of coworkers liking Eclipse, but they are all Java guys so that must explain it.

No Safe Word
Feb 26, 2005

Chin Strap posted:

As someone who is a really dedicated vim user, is there a reason to try eclim? I don't write Java. I'm not sure what IDE features from Eclipse I'm actually missing out on. My primary languages are Go, R, and Python. Is the stuff from Eclipse just more useful for Java/C++?

If you do decide to stick with Eclipse, I was using vrapper when I last used Eclipse heavily, and it was pretty good. It had one cool feature that I've not seen in any other vim plugin for an IDE - a toggle button to make it easy to turn off when others are coding in your IDE.

Dicky B
Mar 23, 2004

What is the quickest way to tranform this:
C++ code:
void butt(int fart, int *goon, const int *autism)
into this:
C++ code:
void butt(
	int fart,
	int *goon,
	const int *autism)

Lumpy
Apr 26, 2002

La! La! La! Laaaa!



College Slice

Dicky B posted:

What is the quickest way to tranform this:
C++ code:
void butt(int fart, int *goon, const int *autism)
into this:
C++ code:
void butt(
	int fart,
	int *goon,
	const int *autism)


code:
:s/,/,\r/
Should do it. On my phone so I can't run it to be 100% sure. You use an r instead of an n for line breaks in search and replace stuff.

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
You might want something like :s/[(,]/\0\r/g in order to break after the opening paren as well as between arguments, and insert all the breaks you want instead of just the first one.

Dicky B
Mar 23, 2004

Cool, I've ended up with :s/[(,]\s\?/\0\r\t/g which sorts the indentation out as well. Thanks for pointing me in the right direction!

leper khan
Dec 28, 2010
Honest to god thinks Half Life 2 is a bad game. But at least he likes Monster Hunter.
I know you can map a command on a search with g like '1,$ g/bad things/d'. Is there a way to map a macro across a regex as well?

Edit: forgot range in example

:ms:
:g/<pattern>/normal @q

leper khan fucked around with this message at 18:49 on Apr 7, 2014

Subjunctive
Sep 12, 2006

✨sparkle and shine✨

leper khan posted:

:g/<pattern>/normal @q

Could you unpack that? Is q the macro name?

MacGowans Teeth
Aug 13, 2003

Subjunctive posted:

Could you unpack that? Is q the macro name?

q is the register where the macro is stored. The normal command treats whatever comes after as if it had been typed on the current line in normal mode, so when you do that across a range, it applies the normal mode commands to each line in the range. I use :'<,'>norm (or numbered ranges) for . and & a lot, and it's great for macros, too. It's a pretty awesome command.

Destroyenator
Dec 27, 2004

Don't ask me lady, I live in beer

MacGowans Teeth posted:

q is the register where the macro is stored. The normal command treats whatever comes after as if it had been typed on the current line in normal mode, so when you do that across a range, it applies the normal mode commands to each line in the range. I use :'<,'>norm (or numbered ranges) for . and & a lot, and it's great for macros, too. It's a pretty awesome command.
Combining with visual mode can be pretty powerful too:
code:
:g/{/norm f{v%J
All lines matching open brace ( :g/{/ ) normal mode ( norm ) jump forward to brace ( f{ ) enter visual [highlighting] mode ( v ) jump to matching brach ( % ) join all highlighted lines ( J )

You can chain multiple calls if you have an enter key press in a register. I can't work out how to paste one into the command line without having pre-recorded it though.
code:
:g/something/norm V/searchtarget^Md
All lines matching something ( :g/something/ ) normal mode ( norm ) line highlighting ( V ) search forward for searchtarget ( /searchtarget^M ) delete all highlighted lines ( d )
Where ^M is a pasted enter, to record one to register q ( qq<Enter>q ) and paste with control-r q.

Adbot
ADBOT LOVES YOU

Plorkyeran
Mar 22, 2007

To Escape The Shackles Of The Old Forums, We Must Reject The Tribal Negativity He Endorsed

Destroyenator posted:

You can chain multiple calls if you have an enter key press in a register. I can't work out how to paste one into the command line without having pre-recorded it though.
Ctrl-V, Enter should work.

  • Locked thread