Live data from Hacker News

Handy text manipulation tricks in Sublime Text 2

whiletruecode.com

81–90 of 99 posts

Re: Handy text manipulation tricks in Sublime Text 2

#81
post #75
post #66

Earlier quoted context omitted.

Why would that be, vim seems to be difficult to use because a good manual that actually teaches how to use vim isn't available yet. In fact it was surprising to me when I learned vim commands are actually a form of some terse language. Because I was only of an assumption that most vim commands are random string of characters you need to memorize. And when you memorize and practice enough you become productive. I woul…

> Why would that be, vim seems to be difficult to use because a good manual that actually teaches how to use vim isn't available yet. What manuals have you tried? Have you tried `vimtutor`(if vim is installed, just run vimtutor from the sell). > In fact it was surprising to me when I learned vim commands are actually a form of some terse language. Because I was only of an assumption that most vim commands are random…

I am emacs user. The problem isn't patience with vim. The problem really is deciphering the `h,j,k,l,esc,d,x,p,b,w,i,I...` sequence.

So as long I can't make sense of what random stream of characters mean, any amount of practice to put them into finger memory is going to be a exercise in frustration.

Re: Handy text manipulation tricks in Sublime Text 2

#82

I've changed some key bindings to suit my taste, like: Cmd-D: Duplicate line Cmd-L: Delete line Cmd-T: Insert HTML tag Cmd-B: Open in browser Ctrl+Up/Dn: Toggle lines Ctrl+a/e/i/o/u: Accented á é í ó ú Sublime Text 2 has become my favorite editor, fully customizable and plenty of plugins available.

Can you share with us how you made these bindings? Specifically open in browser? using

    	{ "keys": ["f8"], "command": "openInBrowser" },
Didn't work on my windows machine when I made it.

Re: Handy text manipulation tricks in Sublime Text 2

#83
post #81
post #75

Earlier quoted context omitted.

> Why would that be, vim seems to be difficult to use because a good manual that actually teaches how to use vim isn't available yet. What manuals have you tried? Have you tried `vimtutor`(if vim is installed, just run vimtutor from the sell). > In fact it was surprising to me when I learned vim commands are actually a form of some terse language. Because I was only of an assumption that most vim commands are random…

I am emacs user. The problem isn't patience with vim. The problem really is deciphering the `h,j,k,l,esc,d,x,p,b,w,i,I...` sequence. So as long I can't make sense of what random stream of characters mean, any amount of practice to put them into finger memory is going to be a exercise in frustration.

> The problem really is deciphering the `h,j,k,l,esc,d,x,p,b,w,i,I...` sequence. > So as long I can't make sense of what random stream of characters mean, any amount of practice to put them into finger memory is going to be a exercise in frustration.

That wasn't a sequence - that was basic movements and operations you need to compose sequences.

h,j,k,l is as intuitive as it gets http://wireless.ictp.trieste.it/school_2003/docs/linux/linux...

Rest of the basic movements are mostly mnemonics

w - move forward a word

b - move back a word

e - move to the end

d - delete followed by movement

You use these to formulate sequences.

d2w - delete 2 words

d2b - delete 2 words backwards

d2w2j - delete 2 words and move 2 lines up

d2w2jp - delete 2 words, move 2 lines up and paste(what was deleted)

d2w2j^p - delete 2 words, move 2 lines up, go to beginning of line(^) and paste(the deleted words)

df)/test$F{p

delete till ) (df)), search forward for test (/test), go to end of line where search is found($), search backward for { (F{), and paste(p)

They aren't random stream of characters if you know basic movements and operations.

As far as emacs goes, I find vim to be superior when it comes to navigation. When I am using emacs, I have a minimal configuration file for a few things I absolutely need:

    ;; General settings.
    (setq transient-mark-mode t)
    (line-number-mode t)
    (column-number-mode t)
    (global-hl-line-mode t)
    (setq-default indent-tabs-mode nil)
    (global-font-lock-mode t)
    (setq-default fill-column 120)
    (setq auto-fill-mode t)

    (define-key global-map "\C-xw" 'what-line)
    (define-key global-map "\C-z" 'undo)
    (define-key global-map "\M-g" 'goto-line)
"M-x goto-line line-number " is just too much typing for going to a specific line.

What emacs wins at is external tools integration. Vim doesn't support async command integration, and per the developers, never will. Vim developers believe vim is a text editor, and it has no business running shells; a stance I am ok with. But that means there never will be a slime for vim, at least not the way it works for Vim i.e you can't execute chunk of code, and be dropped in a debugger if an exception occurs. And there will never be a debugger which seamlessly integrates with vim. I do miss these things sometimes, but there are decent alternatives - I love my zsh, don't need it in an editor; slime, sans debugger is just configuring vim to send selection to repl etc etc.

Re: Handy text manipulation tricks in Sublime Text 2

#84
post #49
post #47

Earlier quoted context omitted.

Completely agree on the awesomeness of multiple cursors and ST2 in general. The official documentation has some more tips: http://www.sublimetext.com/docs/2/multiple_selection_with_th... Fortunately, other editors are already adopting similar features. Just today I was fiddling around with Ace, a web-based code editor, and noticed that they support multiple cursors too! Just select multiple words while pressing the C…

Fortunately, other editors are already adopting similar features. Yup, work's being done to add this to Emacs as well although it is not quite there yet: - http://emacsrocks.com/e08.html - https://github.com/emacsmirror/multiple-cursors

Are you saying Emacs is finally getting a decent editor? ;)

Re: Handy text manipulation tricks in Sublime Text 2

#85
post #35

Any article that talks about tricks in Sublime Text 2 without mentioning Multiple Cursors is... lacking (no offense to the OP). For anyone that doesn't know it, Multiple Cursors is the ONE BRILLIANT FEATURE that Sublime Text has. Don't get me wrong, tt's great in many other ways too, but Multiple Cursors is, IMO, a feature that should become part of every friggin' text editor in existence, it's that important. And it…

Having switched to Vim, multiple cursors are the only reason I ever use Sublime Text anymore. They are insanely, amazingly awesome and powerful. I can't wait to see your efforts at porting the idea to Vim.

Re: Handy text manipulation tricks in Sublime Text 2

#86
post #77
post #57

Earlier quoted context omitted.

I agree multiple cursors are awesome and largely can replace macros with real-time visible changes. But there is one use-case of macros that cannot be replaced with multiple cursors -- one where the places being affected by the macro are themselves computed by the macro.

Can you please give an example of that? You've got my curiosity piqued :) Btw, another place this can't be used is across multiple files, at least the way ST does it.

Sample

    def qux(bar, baz, qux)
        pass

    def quuux(grim, gram, grom)
        pass

    def quuuuuux(hip, hop, zoom)
        pass
Delete second argument (of 3+ arg functions):

    /def # search
    qd       # record as 'd'
    2f,dF,n  # find second arg
    q        # done recording
   
    @d       # again (or n to skip)
Example intentionally simplified/convoluted. I'm doing stuff like this on the spot routinely, and there's no way multiple cursors could do this because I apply verbs to text objects that ST has (unless I'm missing something) no way of knowing about in a visual paradigm without resorting to guessing what I mean.

What would be killer is mixing both, i.e visually selecting where to apply a vim macro.

Re: Handy text manipulation tricks in Sublime Text 2

#87
post #86
post #77

Earlier quoted context omitted.

Can you please give an example of that? You've got my curiosity piqued :) Btw, another place this can't be used is across multiple files, at least the way ST does it.

Sample def qux(bar, baz, qux) pass def quuux(grim, gram, grom) pass def quuuuuux(hip, hop, zoom) pass Delete second argument (of 3+ arg functions): /def # search qd # record as 'd' 2f,dF,n # find second arg q # done recording @d # again (or n to skip) Example intentionally simplified/convoluted. I'm doing stuff like this on the spot routinely, and there's no way multiple cursors could do this because I apply verbs to…

If you put the "/def" part in the macro too, then you can re-execute it multiple times.

I think it's actually useful, not simplified/convoluted..

Re: Handy text manipulation tricks in Sublime Text 2

#88

I've changed some key bindings to suit my taste, like: Cmd-D: Duplicate line Cmd-L: Delete line Cmd-T: Insert HTML tag Cmd-B: Open in browser Ctrl+Up/Dn: Toggle lines Ctrl+a/e/i/o/u: Accented á é í ó ú Sublime Text 2 has become my favorite editor, fully customizable and plenty of plugins available.

Can you share with us how you made these bindings? Specifically open in browser? using { "keys": ["f8"], "command": "openInBrowser" }, Didn't work on my windows machine when I made it.

Here is how I did it for Safari, change accordingly.

Go to Tools/BuildSystem/NewBuildSystem... Type this in:

    {
        "cmd": ["open", "-a", "Safari", "$file"],
        "selector": "html"
    }
Save it in your sublime/packages/user folder as 'Open in Safari.sublime-build'

That's it.

It should show up in your Tools/BuildSystem and should be selected for all html files. So when you hit Cmd+B it uses that build system to just show the file in Safari.

There are other ways like mapping Cmd+B directly to a python snippet like:

    import sublime, sublime_plugin, webbrowser

    class OpenBrowserCommand(sublime_plugin.TextCommand):
       def run(self,edit):
          url = self.view.file_name()
          webbrowser.open_new(url)
but for me the first option was simpler.

Re: Handy text manipulation tricks in Sublime Text 2

#89
post #86
post #77

Earlier quoted context omitted.

Can you please give an example of that? You've got my curiosity piqued :) Btw, another place this can't be used is across multiple files, at least the way ST does it.

Sample def qux(bar, baz, qux) pass def quuux(grim, gram, grom) pass def quuuuuux(hip, hop, zoom) pass Delete second argument (of 3+ arg functions): /def # search qd # record as 'd' 2f,dF,n # find second arg q # done recording @d # again (or n to skip) Example intentionally simplified/convoluted. I'm doing stuff like this on the spot routinely, and there's no way multiple cursors could do this because I apply verbs to…

Actually, this is a perfect example of using Multiple Cursors to get the job done more easily.

I'd select all three "def"s simultaneously, then move by word three/four times using ctrl+right, or plain old "w" if using ST's vim mode. When I'm on the second param, I'll delete forward to the next ",". I could also just ctrl+right my way until the next word, which would be the same.

Just to recap the keys I'd press:

/def (select first def) ctrl+d * 2 (select using multi-cursors the next two defs ctrl+right * 4 (to get to the second param) ctrl+shift+right (to select the entire second param) delete

And if I mess up at any point, I instantly see it. If I did something that only works for the first case but not the others, I see it immediately.

Note: Using vintage mode, Sublime Text has vim shortcuts. Without it, Multiple Cursors still rock, but are missing a LOT of their power (exactly because of situations like this).

In fact, in Sublime Text 1, for which vim mode didn't exist, I actually implemented a plugin to emulate the "f" and "t" movements because they were so good.

Re: Handy text manipulation tricks in Sublime Text 2

#90
post #89
post #86

Earlier quoted context omitted.

Sample def qux(bar, baz, qux) pass def quuux(grim, gram, grom) pass def quuuuuux(hip, hop, zoom) pass Delete second argument (of 3+ arg functions): /def # search qd # record as 'd' 2f,dF,n # find second arg q # done recording @d # again (or n to skip) Example intentionally simplified/convoluted. I'm doing stuff like this on the spot routinely, and there's no way multiple cursors could do this because I apply verbs to…

Actually, this is a perfect example of using Multiple Cursors to get the job done more easily. I'd select all three "def"s simultaneously, then move by word three/four times using ctrl+right, or plain old "w" if using ST's vim mode. When I'm on the second param, I'll delete forward to the next ",". I could also just ctrl+right my way until the next word, which would be the same. Just to recap the keys I'd press: /def…

How do you make multiple cursors that are at the exact locations you need?

That's something that a macro is useful for. His macro is less useful because for some reason he did his search outside of the macro. Maybe the search is not for "def", but for "def prefix", so ctrl+d is not applicable to form the multiple cursors.

It could be nice if you could record your movements to form the "next" cursor and immediately see all future generated cursors given that movement (similarly to the "recursive draw" web app). Then you could perhaps truncate the infinite list of cursors at some finite point somehow, and then operate your multiple cursor operations.

Post reply on HN