SublimeText3 to keep line folded - sublimetext3

In SublimeText3, keyboard shortcut "Ctrl + Shift + [" is used to fold lines selected.
Even though the script file is saved. When the script file is closed, and opened again, the folded lines are automatically unfolded (like the lines had never been folded before).
Is there a way to keep the lines always folded?

I haven't used it myself, but it looks like the AutoFoldCode plugin is what you're looking for. It acts on all open files, which could be annoying. It also optionally remembers selections from session to session, which could also be useful.

Related

How to insert tabs on new line in Vim before a character is typed

If I write an if statement in my C program, press enter three times, then write a comment, the below is my output. Notice the two lines between the condition and the comment are completely empty.
if(my_condition) {
<Tab>// My comment here
My issue is that Vim does not insert any tab character(s) between the beginning of the line and the cursor position until a character is typed. This is very annoying for me, because I like to move my cursor up and down the block of code often. Since there isn't a real tab on the two lines, if I moved up one line my cursor would go to the beginning of the line, instead of staying on the same column. I come from Sublime Text and other editors where this has never been a problem.
Is there a plugin or setting such that I can accomplish the following?
if(my_condition) {
<Tab>
<Tab>
<Tab>// My comment here
All help is appreciated. I've looked into using Visual mode, but have had undesirable side effects of enabling it all the time. Certainly there is a simple way to automatically add the tabs when I make a new line?
This is very annoying for me, because I like to move my cursor up and down the block of code often.
Well, as you might have noticed, switching to vim means that you need to change your own editing behavior. And that might be the toughest, more than learning the new commands, because habits die hard!
I'm not saying that you should stop scrolling around your function in an useless manner, though, but that you should stop moving around with the "cursor" keys in insert mode.
When you're doing movements when in insert mode it has the side effect you're exposing as "inconvenient", but it also has the side effect of breaking the "repeat" command (.). After a cursor movement in insert mode, a . will only repeat characters typed after that movement.
So, what you should consider in your editing behavior, is to avoid empty lines, avoid trailing spaces/tabs and never move around in insert mode. Insert mode is for insertion, and the normal mode is for moving around and doing actions on the text.
There are a lot of move commands you can abuse in normal mode (j/k, <C-e>/<C-y>, {/}, …).
That being said, if you get yourself in a situation where you've fscked the indentation, you might want to keep on editing, not caring about the indent, and once you're back in normal mode issue a =i{ that will indent everything within the block following the syntax file (and your indent settings).

What's wrong with gVim copy paste between windows?

I'm new to vim and I'm using gVim on Windows. I have two windows open in the same gvim instance and I'm just trying to copy some code from one to another. For some reason when it pastes it replaces the contents of some code with literally this:
list.forEach(function(name){...}------------------------------------------
Obviously, my real code does not have ... or a ton of dashes. What the hell is happening?
The dashes (----------) give it away: The block of code has been folded. (You probably also see the line with different colors (depending on your colorscheme).) Depending on the filetype, folding can be manual or automatic. For your (JavaScript?) code, it's the latter. So when you paste a block of code, Vim automatically detects the block and folds it.
There's a whole lot of commands and options around folding. Read more about it at :help folding. If you find this too confusing (for now), turn it off via
:set nofoldenable
Just press zo on the dashes(-----)
Aside: Use zf on selected (v) lines to fold lines.
zo - Open
zo - Fold

How do I close a single buffer (out of many) in Vim?

I open several files in Vim by, for example, running
vim a/*.php
which opens 23 files.
I then make my edit and run the following twice
:q
which closes all my buffers.
How can you close only one buffer in Vim?
A word of caution: “the w in bw does not stand for write but for wipeout!”
More from manuals:
:bd
Unload buffer [N] (default: current
buffer) and delete it from
the buffer list. If the buffer was changed, this fails,
unless when [!] is specified, in which case changes are
lost.
The file remains unaffected.
If you know what you’re doing, you can also use :bw
:bw
Like |:bdelete|, but really delete the
buffer.
If this isn't made obvious by the the previous answers:
:bd will close the current buffer. If you don't want to grab the buffer list.
Check your buffer id using
:buffers
you will see list of buffers there like
1 a.php
2 b.php
3 c.php
if you want to remove b.php from buffer
:2bw
if you want to remove/close all from buffers
:1,3bw
Rather than browse the ouput of the :ls command and delete (unload, wipe..) a buffer by specifying its number, I find that using file names is often more effective.
For instance, after I opened a couple of .txt file to refresh my memories of some fine point.. copy and paste a few lines of text to use as a template of sorts.. etc. I would type the following:
:bd txt <Tab>
Note that the matching string does not have to be at the start of the file name.
The above displays the list of file names that match 'txt' at the bottom of the screen and keeps the :bd command I initially typed untouched, ready to be completed.
Here's an example:
doc1.txt doc2.txt
:bd txt
I could backspace over the 'txt' bit and type in the file name I wish to delete, but where this becomes really convenient is that I don't have to: if I hit the Tab key a second time, Vim automatically completes my command with the first match:
:bd doc1.txt
If I want to get rid of this particular buffer I just need to hit Enter.
And if the buffer I want to delete happens to be the second (third.. etc.) match, I only need to keep hitting the Tab key to make my :bd command cycle through the list of matches.
Naturally, this method can also be used to switch to a given buffer via such commands as :b.. :sb.. etc.
This approach is particularly useful when the 'hidden' Vim option is set, because the buffer list can quickly become quite large, covering several screens, and making it difficult to spot the particular buffer I am looking for.
To make the most of this feature, it's probably best to read the following Vim help file and tweak the behavior of Tab command-line completion accordingly so that it best suits your workflow:
:help wildmode
The behavior I described above results from the following setting, which I chose for consistency's sake in order to emulate bash completion:
:set wildmode=list:longest,full
As opposed to using buffer numbers, the merit of this approach is that I usually remember at least part of a given file name letting me target the buffer directly rather than having to first look up its number via the :ls command.
Use:
:ls - to list buffers
:bd#n - to close buffer where #n is the buffer number (use ls to get it)
Examples:
to delete buffer 2:
:bd2
You can map next and previous to function keys too, making cycling through buffers a breeze
map <F2> :bprevious<CR>
map <F3> :bnext<CR>
from my vimrc
Close buffer without closing the window
If you want to close a buffer without destroying your window layout (current layout based on splits), you can use a Plugin like bbye. Based on this, you can just use
:Bdelete (instead of :bdelete)
:Bwipeout (instead of :bwipeout)
Or just create a mapping in your .vimrc for easier access like
:nnoremap <Leader>q :Bdelete<CR>
Advantage over vim's :bdelete and :bwipeout
From the plugin's documentation:
Close and remove the buffer.
Show another file in that window.
Show an empty file if you've got no other files open.
Do not leave useless [no file] buffers if you decide to edit another file in that window.
Work even if a file's open in multiple windows.
Work a-okay with various buffer explorers and tabbars.
:bdelete vs :bwipeout
From the plugin's documentation:
Vim has two commands for closing a buffer: :bdelete and :bwipeout. The former removes the file from the buffer list, clears its options, variables and mappings. However, it remains in the jumplist, so Ctrl-o takes you back and reopens the file. If that's not what you want, use :bwipeout or Bbye's equivalent :Bwipeout where you would've used :bdelete.
How about
vim -O a a
That way you can edit a single file on your left and navigate the whole dir on your right...
Just a thought, not the solution...
[EDIT: this was a stupid suggestion from a time I did not know Vim well enough. Please don't use tabs instead of buffers; tabs are Vim's "window layouts"]
Maybe switch to using tabs?
vim -p a/*.php opens the same files in tabs
gt and gT switch tabs back and forth
:q closes only the current tab
:qa closes everything and exits
:tabo closes everything but the current tab
Those using a buffer or tree navigation plugin, like Buffergator or NERDTree, will need to toggle these splits before destroying the current buffer - else you'll send your splits into wonkyville
I use:
"" Buffer Navigation
" Toggle left sidebar: NERDTree and BufferGator
fu! UiToggle()
let b = bufnr("%")
execute "NERDTreeToggle | BuffergatorToggle"
execute ( bufwinnr(b) . "wincmd w" )
execute ":set number!"
endf
map <silent> <Leader>w <esc>:call UiToggle()<cr>
Where "NERDTreeToggle" in that list is the same as typing :NERDTreeToggle. You can modify this function to integrate with your own configuration.

Simple Vim commands you wish you'd known earlier [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance.
Closed 10 years ago.
I'm learning new commands in Vim all the time, but I'm sure everyone learns something new once in a while. I just recently learned about this:
zz, zt, zb - position cursor at middle, top, or bottom of screen
What are some other useful or elegant commands you wish you'd learned ages ago?
I really wish I'd known that you can use Ctrl+C instead of Esc to switch out of insert mode. That's been a real productivity boost for me.
The most recent "wow" trick that I learnt is a method of doing complicated search-and-replace. Quite often in the past, I've had a really complicated regexp to do substitutions on and it's not worked. There is a better way:
:set incsearch " I have this in .vimrc
/my complicated regexp " Highlighted as you enter characters
:%s//replace with this/ " You don't have to type it again
The "trick" here (for want of a better word) is the way that you can use the search to create the regexp (and 'incsearch' highlights it as you enter characters) and then use an empty pattern in the substitution: the empty pattern defaults to the last search pattern.
Example:
/blue\(\d\+\)
:%s//red\1/
Equivalent to:
:%s/blue\(\d\+\)/red\1/
See:
:help 'incsearch'
:help :substitute
I created this reference of my most used command for a friend of mine:
select v
select row(s) SHIFT + v
select blocks (columns) CTRL + v
indent selected text >
unindent selected text <
list buffers :ls
open buffer :bN (N = buffer number)
print :hardcopy
open a file :e /path/to/file.txt
:e C:\Path\To\File.txt
sort selected rows :sort
search for word under cursor *
open file under cursor gf
(absolute path or relative)
format selected code =
select contents of entire file ggVG
convert selected text to uppercase U
convert selected text to lowercase u
invert case of selected text ~
convert tabs to spaces :retab
start recording a macro qX (X = key to assign macro to)
stop recording a macro q
playback macro #X (X = key macro was assigned to)
replay previously played macro * ##
auto-complete a word you are typing ** CTRL + n
bookmark current place in file mX (X = key to assign bookmark to)
jump to bookmark `X (X = key bookmark was assigned to
` = back tick/tilde key)
show all bookmarks :marks
delete a bookmark :delm X (X = key bookmark to delete)
delete all bookmarks :delm!
split screen horizontally :split
split screen vertically :vsplit
navigating split screens CTRL + w + j = move down a screen
CTRL + w + k = move up a screen
CTRL + w + h = move left a screen
CTRL + w + l = move right a screen
close all other split screens :only
* - As with other commands in vi, you can playback a macro any number of times.
The following command would playback the macro assigned to the key `w' 100
times: 100#w
** - Vim uses words that exist in your current buffer and any other buffer you may have open for auto-complete suggestions.
gi switches to insertion mode, placing the cursor at the same location it was previously.
:q!
I wish I knew that before I started vi for the first time.
^X-F completes using filenames from the current directory. No more copying/pasting from the terminal or painful double checking.
^X-P completes using words in the current file
:set scrollbind forces one buffer to scroll alongside another. e.g. split your window into two vertical panes. Load one file in each (perhaps different versions of the same file). Do :set scrollbind in each. Now when you scroll in one, both panes will scroll together. Ideal for comparing files.
You can use a whole set of commands to change text inside brackets / parentheses / quotation marks/ tags. It's super useful to avoid having to find the start and finish of the group. Try ci(, ci{, ci<, ci", ci', ct depending on what kind of object you want to change. And the ca(, ca{, ... variants delete the brackets / quotation marks as well.
Easy to remember: change inside a bracketed statement / change a bracketed statement.
The asterisk key, *, will search for the word under the cursor.
[+Tab will take you to the definition of a C function that's under your cursor. (It doesn't always work, though.)
Don't press Esc ever. See this answer to learn why. As mentioned above, Ctrl + C is a better alternative. I strongly suggest mapping your Caps Lock key to escape.
If you're editing a Ctags compatible language, using a tags file and :ta, Ctrl + ], etc. is a great way to navigate the code, even across multiple files. Also, Ctrl + N and Ctrl + P completion using the tags file is a great way to cut down on keystrokes.
If you're editing a line that is wrapped because it's wider than your buffer, you can move up/down using gk and gj.
Try to focus on effective use of the motion commands before you learn bad habits. Things like using 'dt' or 'd3w' instead of pressing x a bunch of times. Basically, any time that you find yourself pressing the same key repeatedly, there's probably a better/faster/more concise way of accomplishing the same thing.
Some of my latest additions to my Vim brainstore:
^wi: Jump to the tag under the cursor by splitting the window.
cib/ciB: Change the text inside the current set of parenthesis () or braces {}, respectively.
:set listchars=tab:>-,trail:_ list: Show tabs/trailing spaces visually different from other spaces. It helps a lot with Python coding.
ZZ (works like :wq)
And about the cursor position: I found that a cursor which always stays in the middle of screen is cool -
set scrolloff=9999
gv starts visual mode and automatically selects what you previously had selected.
:shell to launch a shell console from Vim. Useful when for example you want to test a script without quitting Vim. Simply hit ^d when you done with the shell console, and then you come back to Vim and your edited file.
This always cheers me up:
:help 42
vimcryption
vim -x filename.txt
You will be asked for a passphrase, edit and save. Now whenever you open the file in vi again you will have to enter the password to view.
Build and debug your code from within Vim!
Configuration
Not much, really. You need a Makefile in the current directory.
To Compile
While you're in Vim, type :make to invoke a shell and build your program. Don't worry when the output scrolls by; just press Enter when it's finished to return to Vim.
The Magic
Back within Vim, you have the following commands at your disposal:
:cl lists the errors, warnings, and other messages.
:cc displays the current error/warning message at the bottom of the screen and jumps to the offending line in your code.
:cc n jumps to the nth message.
:cn advances to the next message.
:cp jumps to the previous message.
There are more; if you're interested, type :help :cc from within Vim.
Press % when the cursor is on a quote, parenthesis, bracket, or brace to find its match.
^P and ^N
Complete previous (^P) or next (^N) text.
^O and ^I
Go to previous (^O - "O" for old) location or to the next (^I - "I" just near to "O").
When you perform searches, edit files, etc., you can navigate through these "jumps" forward and back.
Marks
Press ma (m- mark, a - name of mark). Later to return to the position, type `a.
^r^w to paste the word under the cursor in command mode.
It is really useful when using grep or replace commands.
Until [character] (t). It is useful for any command which accepts a range. My favorite is ct; or ct) which deletes everything up to the trailing semicolon / closing parentheses and then places you in insert mode.
Also, G and gg are useful (Go to bottom and top respectively).
^y will copy the character above the cursor.
Typing a line number followed by gg will take you to that line.
I wish I'd known basic visual block mode stuff earlier. Even if you don't use Vim for anything else, it can be a big time saver to open up a file in Vim just for some block operations. I'm quite sure I wasted a ton of time doing this kind of thing manually.
Examples I've found particularly useful, when, say, refactoring lists of symbolic constant names consistently:
Enter Visual Block mode (Ctrl + Q for me on Windows instead of Ctrl + V)
Move the cursor to highlight the desired block.
Then, I whatever text and press Esc to have the text inserted in front of the block on every line.
Use A instead of I to have the text inserted after the block on every line.
Also - simply toggling the case of a visual selection with ~ can be a big time saver.
And simply deleting columns, too, with d of course.
q<letter> - records a macro.
and
#<same-letter> - plays it back.
These are by far the most useful commands in Vim since you can have the computer do a whole lot of work for you, and you don't even have to write a program or anything.
Opening multiple files using tabs
:tabe filepath
Navigating between open files
gt and gT or :tabn and :tabp
Save the open session so that you can get back to your list of open files later
:mksession session_file_name.vim
Open a created session
vim -S session_file_name.vim
Close all files at once
:qa
Another command I learned recently
autocmd
It allows you to run a command on an event, so you could for example run the command make when you save a file using something like:
:autocmd BufWritePost *.cpp :make
qx will start recording keystrokes. You can do pretty much any editing task and Vim remembers it. Hit q again when you're finished, and press #x to replay your keystrokes. This is great for repetitive edits which are too complex to write a mapping for. You can have many recordings by using a character other than x.
I would have to say that one of my favorites is putting the help window in a new tab:
:tab help <help_topic>
This opens up help in a new tab and, as somebody that loves Vim tabs, this is ridiculously useful.
:x #(Save and Quit a File)
Same as :wq or ZZ
cw
Change word - deletes the word under the cursor and puts you in insert mode to type a new one. Of course this works with other movement keys, so you can do things like c$ to change to the end of the line.
f + character
Finds the next occurrence of the character on the current line. So you can do vft to select all the text up to the next "t" on the current line. It's another movement key, so it works with other commands too.
:b [any portion of a buffer name] to switch buffers. So if you have two buffers, "somefile1.txt", and "someotherfile2.txt", you can switch to the second with simply ":b 2.t<enter>". It also supports tab completion, although it's not required.
Speaking of tab completion, the setting :set wildmode=full wildmenu is also very helpful. It enables complete tab completion for command-mode, as well as a very helpful ncurses-style menu of all the possible matches when using it.

Filename completion in reverse order

To open a file in vim, I usually type ":e " and then hit tab until the file I want appears.
However, I always get in a rhythm and inadvertently go ONE past the desired file. Without knowing how to move backwards, I end up tabbing all the way to the end and repeating the whole process.
Is there a way to perform the filename completion in reverse order?
Shift-Tab goes backwards.
You can also use set wildmenu to get a list of matching file names above the status bar when you do file name completition with Tab. Then you can select the file name with the arrow keys from this list.
Shift+Tab and Ctrl+P both go backward.
However Shift+Tab only works with the GUI [1]. Since I am using vim and not gvim, Shift+Tab would not work. Ctrl+P works perfectly.
[1] And on the Amiga and MS-DOS. See ":help cmdline-completion" for more info.
If you got here because your vim autocompletion starts with the last item when you use "Tab", cycles backward from there AND you're using the Supertab plugin, here is your solution: https://stackoverflow.com/a/17105393/332451
let g:SuperTabDefaultCompletionType = "<c-n>"

Resources