Deleting the last 2 lines of the buffer in vi - regex

I am currently using vi on a file called Nick. I want to delete the last 2 lines in the buffer. How would I do this? I tried using y and /d however I am not sure of the correct syntax.

If you are doing this interactively:
:$-1 (to go to the second to last line)
dG (to delete everything from where the cursor is now)

Gdk will do it, followed by Ctrl-O if you want to return to where you were before you did it.
(That's G to move to the end of the document, dk to delete the lines between the current location and the result of the motion k i.e. up one line, and Ctrl-O just returns you to your previous location.)
If I was actually doing this and didn't have time to think about it, I'd probably use G to go to the end of the file, Shift-V to enter Visual Line mode, k to select the previous line in addition to the current one, then d to delete them both.

Shortest way I know of is
:$;-d
which says 'delete lines in the range $;-'. The range is given in reverse, from the last line $ to one before the last line - (same as -1).

Related

vim delete till one character before the end of the line

I can press $ to go to the end of the line so if I want to delete everything till EOL I can do d$ (or D). What if I want to delete till EOL column - 1 character (or - n chars more broadly)?
Example
I have a row 1234.567890 where . represents the cursor and want 1234.0 (. represents cursor again).
How about
d/.$<CR>
This deletes until one character before EOL.
If there's no identical character in between (or the number of those is easily determined), the f and t commands are very useful, because they just involve two keystrokes (the command and the target character, possibly prepended by a [count]), and (unlike the more generic /...<CR>) they are limited to the current line. For your example, that would be dt0 then.
For more complex scenarios, before I stop and lengthily contemplate possible solutions, visual mode is a quick alternative that lets you iteratively fine-tune the area before applying the command. I think it's a great pragmatic addition to the original command set of vi. For your example, that would be v$hd.
There is no built-in movement for that, but you can create
your own easily with :omap. It will work with any
operator.
:onoremap <silent> q :<C-U>normal! v$hh<CR>
Now dq will do what you want, as well as cq to change
until the before-last char, vq, yq and so on.
This works by replacing q with a call to :normal to
initiate a visual selection, up to the end of the line
(v$) and back two characters (that's because $ selects
until the newline itself). The <C-U> clears any possible
range.

How to display m[,n] lines around current line in pdb?

The Python 2.7 debugger pdb allows to display lines o to p with l [o[,p]], but one has to figure out which line the debugger stand on and then substract and add. Is there a way to display an m lines before and n lines after the current line without arithmetics?
As far as I know, no. The l(ist) command will display 11 lines around the current line, but you can't display m lines before and n after the current line.
https://docs.python.org/2/library/pdb.html
l(ist) [first[, last]]
List source code for the current file. Without arguments, list 11 lines around the current line or continue the previous listing. With one argument, list 11 lines around at that line. With two arguments, list the given range; if the second argument is less than the first, it is interpreted as a count.

Add trailing zeroes to line in notepad++

I have a file containing (hundreds) of blocks of numbers like below;
This one is fine (16x20, correct number of rows and columns)
11111111111111111110
16666616666666661110
16111616111111162610
16111646111663132610
16162616261623132610
16162313261623132610
16162313261623132610
16162313261623132610
16162313261623132610
16162313261623132610
16162313261623132610
16162313261626132610
16166313661116632610
16111111111116132610
16666666666666136610
11111111111111111110
This one needs to be padded with trailing zeroes so it is (16x20)
111111111111111111
166616666666663661
166611111111111661
166666366663661661
113161111111161611
1316166666616161
1616162262616161
11616166112616161
16616166116616161
16616162262616161
16616166266616161
16616111161116161
1661666666666616111
1661666166163366661
1641666166166613661
1111111111111111111
I would like to pad them with zeroes so they are all like the first example. I'm aware of the regular expressions feature in notepad++ but am struggling to get it to work. I appreciate any help given.
You could do it via a macro.
First append a large number of zeroes to the end of each line using a macro.
Caret on the first entry
click record macro
press end
type out 20 zeroes
press down arrow
click stop recording
play the macro until all lines look like this
11111111111111111100000000000000000000000000000000000000000000
16661666666666366100000000000000000000000000000000000000000000
16661111111111166100000000000000000000000000000000000000000000
16666636666366166100000000000000000000000000000000000000000000
11316111111116161100000000000000000000000000000000000000000000
131616666661616100000000000000000000000000000000000000000000
161616226261616100000000000000000000000000000000000000000000
1161616611261616100000000000000000000000000000000000000000000
1661616611661616100000000000000000000000000000000000000000000
1661616226261616100000000000000000000000000000000000000000000
1661616626661616100000000000000000000000000000000000000000000
1661611116111616100000000000000000000000000000000000000000000
166166666666661611100000000000000000000000000000000000000000000
166166616616336666100000000000000000000000000000000000000000000
164166616616661366100000000000000000000000000000000000000000000
111111111111111111100000000000000000000000000000000000000000000
Then...
Caret on first line
click record
press home key
press the right arrow key 20 times
hold shift and press end key
press delete key
press down arrow
click stop recording
play the macro until all lines are processed
You could save the entire process as a single macro so its just a single click in the future.
I can give you a macro solution
go to the beginning of your text
select Macro/Start Recording
press end, press 0 16 times then press Home and down arrow key
select Macro/End Recording
You now have a macro to add sixteen zeros to the end of all lines.
Playback this macro on all lines.
You now have appended zeroes to all lines.
Pressing Alt key and using mouse select the required block(columns) of text you want and paste it into another empty notepad tab
help on column mode editing is there inside notepad ? / help contents menu
Good luck
You can use the plugin ConyEdit to do this.
With ConyEdit running in the background, follow these steps:
use the command line cc.aal 00000000000000000000 to append after lines with twenty zero character.
use the command line cc.gc 1/\d{20}/ to get the first column of regex match.
Looking to do this manualy and not progomaticly ?
Open Findreplace
Copy from the last to rhe first WITHOUT NUMBERS on a line so...
in this example
111111111111111111 <---from here
to here ---> 166616666666663661
166611111111111661
paste that into the fine ( yes your effecticly copying the return wich some applications allow you to manualy input others wont )
then in the replace box, type '0' then your return
Hit that magic replace all :D
This will then add a 0 every time it hits a new line, then add a new... new line....
edit : quickly reviewing another method a second to recover for alternate options :P give me 10
edit 2:
Ah ok somthing like this will work :P just tested it.
use [0-9] in the find replace. so if im looking for 123123123123 ( wich is 12 long ) and i need to buff i up to 20,
Your FIND must be in ()
so..
the find would be
([0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9] )
and the replace is referd to as \1 no the regex, this was my mistake
\100000000
tested and confirmed !dont forget YOU NEED MATCH ALL on, WRAP off!
And so on for your other numbers, Not sure if you can loop this with macros nd stuff :P but hope it helps more than you have now
two good resources.
http://blog.creativeitp.com/posts-and-articles/editors/understanding-regex-with-notepad/comment-page-1/
http://regexpal.com/
base on OP's comment: you could try an editor called vim/gvim
open your file in vim, then type:
:%s/.*/\=printf("%-20s",getline("."))/|%s/ *$/\=substitute(submatch(0)," ","0","g")/
don't forget pressing <Enter> after the above typing.
then you will see the text has been changed into what you want.
of course vim macro can work as well, however, I feel command better... :)

vim regex : multi-line edit and controlling the cursor position

A text file is formatted like this:
Section 4 Area B Unit 20
stuff i don't need...
stuff i don't need...
45990 - Title of Project that I want to save
line of text I need to keep
line of text I need to keep
2010-11 this line (starting with 2010) is not needed
stuff i don't need
Section 589 Area C Unit 1005
stuff i don't need...
stuff i don't need...
45990 - Title of Project that I want to save
line of text I need to keep
line of text I need to keep
2010-11 this line (starting with 2010) is not needed
stuff i don't need
and these sections repeat by the hundreds. The "stuff i don't need" lines are actually about 30 or so. I need to keep the association of the "Section..." line, "Title..." line and "line of text I need to keep" related to each other. So I was hoping to first destruct the text document down (linewise) to the stuff I need before operating on it further (character-wise). So I wrote this:
g!/\Section\s\d*\sArea\s\h\sUnit\s\d*\n\|^\s\{3}\zs\d*\s-\_.*\ze2010-11/d
After deleting I get the "Section.." line and the "Title..." line, but never the subsequent lines underneath the "Title.." line. Those subsequent lines vary from 4 to 8 lines, but the "2010-11" line is consistent and always what I no longer want.
You can see I tried using zs and ze to select what I do not want deleted. I think the selection is working because if I change the command to "2011-12" then there is no match and the (OR) half of the command does not return a result.
I think the fault might be the cursor position(?), but I'm not sure and my effort to fix that has failed.
Can anyone see my error?
Thanks!
Give this a whirl.
:silent! g/^Section/+ , /^\s\+\d\+ -/- d
:g/^\s\+2010/ , -/\nSection\|\%$/ d
:g finds every line matching start of the pattern, ! will revert the selection and command will get applied to these lines.
Would something like g/^Section.../normal! j2dd3jd} do?
If not you can use a search for the Title line inside normal!
You may need to enclose it in "exec" but may be much simpler to write a function.
Do you really need to use vim? Seems like job for Perl to me.
There are many ways to do t, I'm sure. I think this sequence of commands should work (ignoring comment lines that begin with double quote):
" global delete of line below 'Section' to line before 'Title'
g/^\s*Section/+1;/Title/-1delete
" global delete from date line to line before 'Section'
g/^\s*\d\d\d\d-\d\d/;/^\s*Section/-1delete
" go to top line of buffer
gg
" delete last chunk, from final date to last line
/^\s*\d\d\d\d-\d\d/;$delete

What are most useful/used vim commands in C/C++ dev environment [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 13 years ago.
Here is the list of mine
Unlike me -- as I did it for illustrative purposes -- don't paste too many.
And most importantly, provide an explanation
Commands shouldn't be generic but relevant to C++/C environment. ctags & scope are welcome too
gi .....................init insert mode in last insertion position
'0 .....................open last edited file
gf .....................open file under cursor in same window
Ctrl-w q ...............close current window
:setlocal autoread .....Auto reloads the current buffer..especially useful while viewing log files
for i in range(1,255) | .put='10.0.0.'.i | endfor.... insert range ip's
g; and g, .......................to move (forward, backward) through the changelist
fx Move the cursor forward to the next occurrence of the character x on the current line (obviously, x can be any character you like). This is an extremely useful command. You can type ; to repeat the last f command you gave.
tx Same as above, but moves the cursor to right before the character, not all the way to it. (It's very useful, really.)
Fx Move the cursor backward to the next occurrence of the character x on the current line.
w Move the cursor forward by a word.
b Move the cursor backward by a word.
0 Move the cursor to the beginning of the current line.
^ Move the cursor to the first character on the current line.
$ Move the cursor to the end of the line
Visual search ....... you can simply yank the selected text with y and go to search mode
/, then you can paste the last yanked text with Ctrl+R 0
ci" - cuts the text in current quotes
ciw - cuts the current word. This works just like the previous one except that ( is replaced with w.
C - cut the rest of the line and switch to Insert mode.
zz -- it scrolls the screen to make this line appear in the middle
C - cut the rest of the line and switch to Insert mode.
de - delete from cursor to the end of the word (you can also do dE to delete until the next space)
df[space] -- delete up until and including the next space
bye -- copies current word
b and e move the cursor word-by-word
capital D (take a deep breath) Deletes the rest of the line to the right
cd %:h change to current directory
:r! <command> pastes the output of an external command into the buffer.
:%s/foo/bar(&)/g will look for foo, and surround the matched pattern with bar().
:s/.*/PREFIX & SUFFIX/ you want to add a prefix and a suffix simultaneously, you can do something like this:
gd....... keystroke stands for Goto Declaration
gD....... This takes you to the global declaration of the variable under the cursor
------------------
:make error
[make_error]
On pressing RETURN, the cursor moves to line number 6
Now, the command :cn will move the cursor to the line number 4.
To move back to the previous error, one can use the command :cN and the cursor will move back to the line 6.
After correcting the error on line 5 and adding "return 1;", one can run :make again and the output will be
---------
:%!grep sdf | sort -n -k3
1)select the whole content using '%'
2) pipe it to an external command using '!'
3) grep onyl the lines containing 'sdf'
4) sort these lines numerically (-n) on the third field (-k3)
d$ will delete from current position to end of line
d^ will delete from current backward to first non-white-space character
d0 will delete from current backward to beginning of line
dw deletes current to end of current word (including trailing space)
db deletes current to beginning of current word
:%s/pattern//gn........... For counting the number of times some pattern occurs, use:
CTRL-O Go to [count] Older cursor position in jump list
CTRL-I Go to [count] newer cursor position in jump list
zz - line that has a cursor is in the middle of the screen
zt - line that has a cursor is in the top of the screen
zb - line that has a cursor is in the buttom of the screen
set printoptions=number:y ...set numbers in a hardcopy
:hardcopy.... to print the file :w
shift d ...... Deleting from current position to end of line
vim -o ....... allows you to open two windows, split vertically horizontally
vim -O ....... allows you to open two windows, split vertically
CTRL+W CTRL-Q ......to close the current windows
qall.........How do I quit all windows
0 ...First position on line
Ctrl g ...where am I
:set wrapmargin=70
printexpr=system('lpr' . (&printdevice == '' ? '' : ' -P' . &printdevice) . ' ' . v:fname_in) . delete(v:fname_in) + v:shell_error
Tab block of code ....select your block of code (with [V]isual line mode normally), then press > or <.
If you want to tab more than once, 2> or 3> to repeat it.
News:
Guys, let's REOPEN THIS QUESTION, and go WIKI-CRAZY!
One set of commands I find useful is [[, ]], [], ][. They navigate through curly braces on the first column, so they effectively allow you to walk through functions if you use proper indentation.
The ones I overuse are the following :
:AV to vertically split the current window and open the header/source file associated to the respective source/header file (if not yet opened, otherwise we jump to its window instead)
:GSp and :GVSp to split the current window and open the requested file (which is somewhere in &path), or jump to the file if it is already opened
<m-x> to toggle comment on the current line
:Make to compile the current project in the background -- NB: a flag needs to be set in order to do so
<c-x>be to add .begin(), /container_name/.end() in whatever(container_name<curoser_here>)
#i which will expand into #include
:DOX which will add a doxygen comment to the current function prototype -- parameters const-/ref-ness, throw spec, return type are taken into account
:GOTOIMPL which will create a default body from the current function declaration (or jump to the already existing body when possible)
<c-w><m-down> : another way to navigate the tags base
for/if/... : expand into the relevant code snippet in insert mode (outside of string/comment context)
,for/,if/.. and ,,for/,,if/... to surround the current selection with the relevant code snippet, the selection will go into the control-statement body (one ,), or its condition (two ,)
tpl expands into template <<cursor>><+placeholder+>
all the text-object motions with =, d, c, ... + di,/vi,/... that acts on the current parameter
<c-x>v, <c-x>t to extract the selected variable/type (refactoring)
All the bracket opening characters + <m-del> to maintain balanced brackets
There are many other commands I use when I develop in C++, but less often -- just explore the links I've given.
Those may be usefull for programming
= - to intend text. To indent all file g CTRL+V G =
CTRL-P/CTRL-O - to complete text
}y - to paste block and indent it correctly in a new place. For example code block in clipboard is 2 level indentation and should be pasted in code, where it will be in 3 level indentation.
CTRL-X+f to complete file name
>>/<< - increase/decrease indentation
% - go the the corresponding open/close bracket
minibufexpl is good plugin to work at once on several files
>aB to indent a block. Not frequently used, but a gem nonetheless.
Using different views is usefull for side by side code comparison.
Note each view created by a split can hold a separate file.
To split the view horizontally
:split
To split the view vertically
:vsplit
To move between split views
^W<arrow> (Thats control W) (Arrow Key)
Once you have set up the tags file:
^] (Move over identifier you want to find: Hit Control ])
:tn Next Tag
:tp Previous Tag
:pop Pop back to the place you where when you hit ^]
You should take a look at SnippetsEmu. A great plugin that will save you a lot of regularly typed words in C.
Note: You're asking about commands and not plugins, but just wanted to mention this one anyway.
The best I've ever used is when I used a combination of combining running make and Quickfix. I pressed F6 to compile, and then F7 to move backwards through the errors and F8 to move forwards by using the line numbers in the gcc warning / error output. Quicker than alt-tab, press up.