To go down four lines in vim where there are pre-existing lines I can do 4j.
Is there a way to do this when, for example, opening a new file?
You can run any command by doing :e +command ~/somepath/somefile for your example, that'd be :e +4j ~/somepath/somefile
Related
I'm using Janus MacVim by Carlhuda, and I wonder if there's a way to tweak Command-T to open a file (buffer) only once, instead of into multiple splits of the same buffer.
Eg: Assumming your directory/project has two files: A.txt and B.txt.
1) Cmd T, then select A.txt.
2) Work on A.txt, then Cmd T, split B.txt with Ctrl V.
3) Work on B.txt, then need to switch back to A: Cmd T, A.txt. Currently Command T would either open A buffer to current split, or create a new split of A. What I want is that the previously opened A buffer would be active again (the cursor would jump back to A) instead of a new split A got created.
So essentially if a buffer has already been opened, resume to that split buffer. Is there a tweak or shortcuts for this?
You probably want :drop or :tab drop instead of the default :tabe for opening files in the Command-T search buffer. This is configurable in your .gvimrc file:
function! CommandTAcceptSelectionTab()
ruby $command_t.accept_selection :command => 'tab drop'
endfunction
This one bothered the heck out of me, too!
There is a 'switchbuf' option but that works only for :sbuffer and few more commands but not for :split, :new and others.
As far as I know it needs some vimscript woodoo, which I used some time ago but do not use anymore and just use :sb with completion.
Suppose I have opened two different files in two vertical screens in vim. Is a there a single command to move the line under cursor to other screen?
Not to my knowledge. Delete it, switch panes and paste it.
dd<ctrl>ww</ctrl>p
Of course if this is something you do regularly you can write a macro for it, just like for any other sequence of commands in vim. For example to map CtrlX to this function, you could run this in your buffer or set it in your ~/.vimrc file:
:map <C-x> dd<C-w><C-w>p<C-w><C-w>
If you want to do this on a regular basis, just map it to a keystroke. e.g.
map <C-A> dd<C-W><C-W>P<C-W><C-W>
im trying to copy 300 lines from one file to another,
in source file i type "300yy", it says it has yanked 300 lines.
go to destination file and press p,
it pastes, but only the first 50 lines.
any idea why it isn't pasting the 300?
To see the current settings during a vim session, run:
:set viminfo?
As suggested in Vim Tips Wiki, you can adjust the viminfo setting (again, during a vim session) by running the ex-command:
:set viminfo='100,<1000,s100,h
or you can remove the : and set it as default in your .vimrc as:
set viminfo='100,<1000,s100,h
What the individual parts mean:
'100 Marks will be remembered for the last 100 edited files.
<1000 Limits the number of lines saved for each register to 1000 lines; if a register contains more than 1000 lines, only the first 1000 lines are saved.
s100 Registers with more than 100 KB of text are skipped.
h Disables search highlighting when Vim starts.
As Eugene and Zyx said adjusting your viminfo would be the easiest solution
:set viminfo-=<50,s10
An alternate solution would be use :read and/or :write
To read in from file-name.txt into the current buffer
:read file-name.txt
To append the range of line 1 to line 300 from the current buffer to file-to-append.txt
:1,300write >> file-to-append.txt
You can also use marks instead of line numbers such as the visual marks
:'<,'>write >> file-to-append.txt
Of course appending may not be able to fulfill your use case in which the viminfo changes will probably work best.
:help :write
:help :read
:help 'viminfo'
:help :set-=
Stay in the same session (open the new file doing :e path) and you won't have any limitation.
try vim -p file1 file2. It opens each file into a new tab (which is awesome), and it solves the copy/paste limit
Something that worked for me is, when in visual mode, copying with a command like :1,300y that copies from line 1 to 300. You can switch this to any range of lines that you would like as :37,456y to copy from line 37 to 456.
If your vim is not showing the lines, you can set the lines with the command :set numbers
If you want to use that yanked/copied lines in another file, i recommend opening multiples tabs and copying and pasting the info between them.
To do this you can open them in the terminal with the command vim -p file1 file2.
To navigete between them you can use the commands gt and gT to move to the next and previous tab respectively.
When dealing with a single file, I'm used to:
/blah
do some work
n
do some work
n
do some work
Suppose now I want to search for some pattern over all buffers loaded in Vim, do some work on them, and move on. What commands do I use for this work flow?
Use the bufdo command.
:bufdo command
:bufdo command is roughly equivalent to iterating over each buffer and executing command. For example, let's say you want to do a find and replace throughout all buffers:
:bufdo! %s/FIND/REPLACE/g
Or let's say we want to delete all lines of text that match the regex "SQL" from all buffers:
:bufdo! g/SQL/del
Or maybe we want to set the file encoding to UTF-8 on all the buffers:
:bufdo! set fenc=utf-8
The above can be extrapolated for Windows (:windo), Tabs (:tabdo), and arguments (:argdo). See help on :bufdo for more information.
We can do this using vimgrep and searching across the argslist. But first let's populate our argslist with all our buffers:
:bufdo :args ## %
Now we can search in our argslist
:vimgrep /blah/ ##
Where % == the current filepath and ## == the arglist.
I recommend watching these vimcasts if you want to learn more: Populate the arglist, Search multiple files with vimgrep
I have the following mappings (inspired by Vimperator) that make switching previous/next buffer easier.
nmap <C-P> :bp<CR>
nmap <C-N> :bn<CR>
This works really well with 'n'. When you're done working with your file, just hit CTRL-n before hitting n again and you're searching in the next buffer. Redo until you're through all buffers.
Another way of working with many files is the argument list.
It contains any files passed as parameters when you started vim (e.g: vim someFile.txt someOtherFile.py). The file within [brackets] is the current file.
:args
[someFile.txt] someOtherFile.py
:n will bring you to the next file in the list and :N will bring you back. You can also add to the argslist with :argadd, or create a new args list with
:n some.py files.py you.py want.py to.py work.py with.py
or to open all *.py files recursively from some project.
:n ~/MyProjects/TimeMachine/**/*.py
The args list work well with macros too (see :help q), if you have similar changes to your files. Just record your macro on the first file, finish with :n to move to the next file, and stop recording.
qq/searchForSomethingAndDoStuffOrWhatever:nq
Then run your macro through all files (6#q), have a look to make sure everything went well, and finish with a :wall.
It kinda depends on what you want to do. If you just have one change that is exactly the same across many files (and those are the only ones you have loaded), I also like the :ba (:tabdo sp) command. It's very quick and you can see what's happening.
And if you have a bunch of buffers open, you can load up the files you want to work within, each in a window, and do a regexp on all of them.
CTRL-w v :b someFile
:sp anotherFile
...
:windo :%s/foo/bar/g
I really recommend FuzzyFinder, it makes your life a lot easier when opening files.
http://www.vim.org/scripts/script.php?script_id=1984
MMmMmmmm VIM IS NICE! SO SEXY! : )
Anytime you want to switch to another buffer try this.
:b + <any part of file in buffer> + tab
For an example. I have this in my buffer
77 "/var/www/html/TopMenuAlertAlert.vue" line 65
78 "/var/www/html/MainSidebar.vue" line 29
79 "/var/www/html/FullScreenSearch.vue" line 26
80 "/var/www/html/Menu.vue" line 93
81 "/var/www/html/layouts/RightSidebar.vue" line 195
As I want to change to another buffer, I probably remember some detail about the file like 'Alert'
So I just go
:b Alert + tab
if the file given is not the one I want, I just keep on pressing tab.
Vim will keep on giving the next file close to it.
Once you got it. Press Enter.
Here is your gospel:
https://github.com/jeetsukumaran/vim-buffersaurus
This lovely plugin shows you all the files that match your query in a separate window, from which you can choose. It support regex.
I don't believe it's possible to extend the 'n' functionanly across files or buffers. You could use
:grep blah *
And then do
:cn
To move to the next line with blah on it. That will switch between files but it's not quite as neat. It's more cumbersome to type the colon all the time, and it will only take you to the line, not the word.
What I usually do is either to open the files I want to searched in tabs and then use 'n' and 'gt' to jump to next tab when I reach the end of the file, or list the files on the command line to I can skip to the next file with ':wn' when I'm done editing it.
Hope it helps!
Another approach:
:call setqflist([]) " clear quickfix list
:silent bufdo grepadd! foo % " edit foo in command-line history window
:cw " view search results
Or mapped:
cmap bbb call setqflist([]) \| silent bufdo grepadd! %<C-F>$hha
I would open all the buffers in a new tab using the following two commands:
:tab sp
:bufdo sp
Then search through each file one by one and close its window when you are done (use :q or :close). Use CTRL+W_ to maximize each window as you are working in it. When you're finished and you close the last window, the tab page will close and you'll be dropped back wherever you were before you decided to do the search.
I'm losing all previous buffers when by mistake I'm trying to switch behind the last buffer [n:].
If for example I open couple of files in editor
:ls
1 # "/etc/moduli" line 1
2 %a "/etc/motd" line 1
:n
E163: There is only one file to edit
:p
E163: There is only one file to edit
now i can navigate between tabs just using :b [number]
Please advice how to fix this behavior. How can I prevent buffers from closing in this case?
I think you're confusing something there. A buffer is something like an open file. When you switch to the next file in the argument list using :n you close the current buffer and open the next one, so the changes must either be saved or discarded at this point.
Additionally the default behaviour of vim is to display an error message if you try to go beyond the last file in your argument list, so losing anything is not very easy in vim.
Maybe describing your actions (pressed keys) could help here, if this does not answer your question.
[edit]
Ok, now I know what the problem is: There is a difference between a buffer and the list of files to edit that you supply when starting vim. If you start vim with
vim a.txt b.txt
there are 2 files to edit. This does not mean, there are multiple buffers. You can navigate using :n and :p (meaning n(ext) file and p(revious) file). If you have the global flag :hidden set, this means that every buffer you close will become a hidden buffer. The file is still being edited, but it is not shown in any window. This value is possibly set upon startup of vim in your system. Try adding :se nohidden to your .vimrc and try the following:
:help buffer-hidden
[/edit]
:bn
will display the next file in your buffer (in your case "/etc/moduli")
:bp
will display the previous file in your buffer (also "/etc/moduli" because it does a permutation)
One thing that you'll notice is that the file you're editing is marked with
%a
whereas
#
means it's the last file you displayed.
Hope it helps you.
:n and :p doesn't switch between buffers :)
try :bufnext and :bufprev
maybe you'll like:
nmap <LEADER>k :bnext<CR>:redraw<CR>
nmap <LEADER>j :bprevious<CR>:redraw<CR>
nmap <LEADER>d :bd<CR>
nnoremap <LEADER>b :buffers<CR>:buffer<space>
Press ,j for the previous buffer, ,k for the next buffer, ,d to close the current buffer and ,b to list your buffers and select one with number keys.