Vim Tips Wiki
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
FANDOM
Fan Central
BETA
Games
Anime
Movies
TV
Video
Wikis
Explore Wikis
Community Central
Start a Wiki
Don't have an account?
Register
Sign In
FANDOM
Explore
Current Wiki
Start a Wiki
Don't have an account?
Register
Sign In
Sign In
Register
Vim Tips Wiki
1,649
pages
Explore
Main Page
All Pages
Community
Interactive Maps
Community portal
To do
Editing
Align endif with corresponding if or ifdef directive
Back to page
Edit
Edit source
View history
Talk (0)
Edit Page
Align endif with corresponding if or ifdef directive
We recommend that you
log in
before editing. This will allow other users to leave you a message about your edit, and will let you track edits via your
Watchlist
.
Creating an account
is quick and free.
The edit appears to have already been undone.
Anti-spam check. Do
not
fill this in!
{{review}} {{TipImported |id=251 |previous=250 |next=253 |created=May 24, 2002 |complexity=basic |author=Kartik Agaram |version=5.7 |rating=0/2 |category1=C |category2=C++ }} If you try to impose any sort of alignment on your preprocessor directives, rather than just starting them on column 0, this mapping will align the #endif 'correctly' when you type '#en', start a new line, and bring you back to the correct alignment to edit code. <pre> inoremap <buffer> #en X<BS><Esc>?#if<CR>"zy0^Og0"zpDa#endif<CR>X<BS><Esc>?#end?-1<CR>^"zy0^O0"zpDa </pre> ==Comments== This tip is currently at -1/1 - I'm not sure why, since I use it a lot. In any case, here's an updated version that handles nested #if blocks. <pre> inoremap #en #endif<Esc>bbd0%y0<C-o>0PA </pre> I've stopped positioning the cursor on the next line because I found myself doing <Esc>dd too often. In a perfect world, we wouldn't have to deal with code with such a high density of preprocessor directives as to make indenting them, and therefore this tip, useful. ---- This would probably better implemented as an indentexpr (or an extension to an existing c++ indentexpr) instead of a map. (Although I'm not sure if you could use the same logic to do so.) But that would let you use the same logic for =. --[[User:Pydave|Pydave]] 19:16, April 30, 2012 (UTC)
Summary:
Please note that all contributions to the Vim Tips Wiki are considered to be released under the CC-BY-SA
Cancel
Editing help
(opens in new window)
Templates used on this page:
Template:Navigation
(
view source
)
Template:Review
(
view source
)
Template:TipImported
(
view source
)
Follow on IG
TikTok
Join Fan Lab