How would you do a cut and paste with this in VIM? - visual-studio

Say you had this text:
SOMETHING_XXXXXXXXXXXXXX_ELSE
SOMETHING_XXXXXXXXXXXXXX_ELSE2
SOMETHING_XXXXXXXXXXXXXX_ELSE3
SOMETHING_XXXXXXXXXXXXXX_ELSE4
And you wanted to replace all XXX..XXX with this word:
HELLOWORLD
If I go into visual mode, then yank the word, how could I then replace the XXX..XXX in the 4 lines above using cut and paste?
If I try, what happens is the X gets into my 'clipboard' and then I'm stuck to just typing it out manually.

I'm not sure if it will work in viemu, but in VIM you can do the following...
Using Yank and Paste
Yank the text to a specific register. Select the text in visual mode and use the command "ay to yank the text to the register a. Then when pasting call the command "ap, which pastes the contents of the a register.
Using Normal Command
But I would strongly prefer to use the normal command. Just select the lines
SOMETHING_XXXXXXXXXXXXXX_ELSE
SOMETHING_XXXXXXXXXXXXXX_ELSE2
SOMETHING_XXXXXXXXXXXXXX_ELSE3
SOMETHING_XXXXXXXXXXXXXX_ELSE4
using line visual mode (<C-v>) and then issue this command: :'<,'>normal fXct_HELLOWORLD. Then you'll have
SOMETHING_HELLOWORLD_ELSE
SOMETHING_HELLOWORLD_ELSE2
SOMETHING_HELLOWORLD_ELSE3
SOMETHING_HELLOWORLD_ELSE4
This means that it will run the command fXct_HELLOWORLD for each line. Let me explain the command:
fX - moves the cursor until the first X;
ct_ - deletes everything untill _ and puts you in insert mode;
HELLOWORLD - the word which will substitute XXXXXXXXXXXXXX;

One way would be to visually select all the code you want to replace and change it at once
Ctrl+v 3jt_cHELLOWORLD[Esc]
Note: it takes a couple of seconds for all lines to be updated
Another way to be by creating a macro:
record macro:
q10fXct_HELLOWORLD[esc]q
run macro on other lines:
j#1j#1j#1
q1 records a macro on character 1
#1 replays macro
But search and replace is a good alternative for your question

Highlight the four lines in visual mode, then
:'<,'>s/X\+/HELLOWORLD/g

Via this question: How do I use vim registers? I found ^R in command mode will paste from a register.
For example, with XXXX highlighted then yanked into the " register:
:s/^R"/HELLOWORLD/g

Related

vim - execute range of lines as shell, output in same buffer

Let's say I'm in a buffer like this, on line 4, I want to run line 1 to 2 and have the output in the same buffer on line 4 (where cursor is):
echo "Testing"
echo "more testing"
# and here I want the output from running lines 1 to 2
...I know I can do 1,2w !sh to run lines 1 and 2 and have the output shown in whatever that temporary buffer is. But, how do I get into my actual buffer for later editing?
(And the same thing to work with visual mode selected text, not just with line ranges given by numbers.)
You were using :w !... (:help :w_c), but you probably want :! (:help :!):
gg - go to top
Vj - select the two lines
y - yank into a buffer
4gg - go to 4th line
V - select it
p - paste over it
gv - reselect the pasted range
:!sh<CR> - execute in shell and replace
or, trusting ex commands more,
:4d
:1,2y
:3pu
:4,5!sh
NB: !sh is in most cases equivalent to !, as ! will call your default shell.
Yey! Found it. In case anyone else needs this exact same hack on a virgin/foreign vim (plugin-less or someone else's server/config):
:1,2r !sh %
(yeah, output goes after commands, or technically the commands are replaced with their echo but whatever, not at cursor position, but good enough for me to replicate my Sublime + SublimeCommand workflow in vim :) )

Stata delimit in command line

I am working on a .do file created by someone else. This person used a semicolon delimiter in the entire file. I am trying to go through this file and see what is going on. I like to do this by selecting a portion of the code and hitting the "Execute Selection (do)" button. However, the delimiter seems to be messing up this. Are there any workarounds for me?
Suppose your do-file looks like this:
#delimit ;
set obs
10 ;
gen x = _n ;
gen y = x^2 ;
gen z = x
^3;
Anytime you highlight a selection and press "Execute selection (do)", Stata creates a temporary, self-contained do-file, with default delimit at cr and runs that:
"When a do-file begins execution, the delimiter is automatically set to
carriage return, even if it was called from another do-file that set the
delimiter to semicolon."
It does not sequentially run those commands from the console. Therefore, if you select the first 2 commands in the do-file above, the temporary do-file includes a call to #delimit whereas if you selected the last 2 commands, the temporary do-file would not have this call and would throw a syntax error for two line commands.
One solution could be to copy-paste selections to a fresh do-file that just had the #delimit command at the beginning, and then run that.
You could also write a script to rid your do-file of semicolons. If a line does not end in a semicolon, then append the next line to the end of the current line, and check this line again. Depending on how complex the syntax is in your do-file, this would be more or less difficult.
Another option is comment out the lines you have already ran by enclosing them with /* */ and to use exit; where you want to stop. You do have to be a little careful with local macros.

How to move right or left by 'x' characters in Bash?

In bash sometimes I have very long commands where I need to edit some words. Right now I use End/Home to move end/start of the command, but what if I have to move say x characters in a line?
I need something like xb/xw of VI, but instead of words I need to move characters.
What about ditching emacs mode and switching to vi mode editing?
set -o vi
and you have all the power of vi-like command line editing, like 3l to go left three characters and 5B to go back 5 words. The Pos 1 key then becomes 0 and End becomes $.
In emacs mode, you can use Meta3Controlb to move back 3 characters, and Meta3Controlf to move forward 3 characters. For multi digit counts, you need to precede each digit with the Meta key (e.g., to move 10 characters back, Meta1Meta0Controlb).
Meta is usually the Alt key, but may be the Esc key instead (on Mac OS X, for instance).
(Yes, vi-command mode makes it easier.)
There is a command, universal-argument, that allows you to type all the digits at once, but it is unbound by default. Bind it with, say,
bind "\C-a":universal-argument
then typing Control-a will enter you into an "argument" mode, prefixing the current line (arg: 4), and allowing you to type digits to change the argument used by the next non-digit character you type. (See universal-argument in the bash man page for the full details.)
You could use the command as below
Command:
cp some_file1 some_file2 some_file3 /root/Desktop
After executing the command do the following
^some_file2^some_file4
and it will execute the command
cp some_file1 some_file4 some_file3 /root/Desktop ;
What happened is the some_file2 is replaced by some_file4 and the command is executed

Sublime - delete all lines containing specific value

I have a 900mb log file which I can open in SublimeText 3. This file is bloated with lines similar to the following.
10/08/2014 23:45:31:828,Information,,,,ExportManager: ,No records to send and/or not connected
How can I filter out all the lines which contain No records to send and/or not connected
You can do a regular expression search-and-replace:
Click Find > Replace.
Ensure that the Regular Expression button is pressed.
For the Find What field, put:
^.*No records to send and/or not connected.*\n
Leave the Replace With field empty.
Click Replace All
For people that don't want to write a regex - you can just select the search string, hit ctrl+cmd+g or pick "Quick Find All" from the menu, which will get you selections for each matching string; from there Home will move every selection cursor to the start of the line, shift+End will select every matching line, and del, del will delete all of them.
Multiple cursor editing is fun!
i could not get the regex to work so I used Alt-F3 approach from this answer:
https://superuser.com/questions/452189/how-can-i-filter-a-file-for-lines-containing-a-string-in-sublime-text-2/598999#598999
Select string of interest
Hit Alt+F3 to go into multi-cursor mode on all occurrences (Ctrl+CMD+G on Mac OS X)
Hit Ctrl+L [see comments] (Cmd+L on Mac)
Copy-paste selection to another buffer
Del
This is what i found for the windows users:
Select the string (every line containing this string is to be removed).
Press ALT+F3 .
Press Ctrl+L .
Press Delete .
Neither of the regex code suggested above worked in my case, but this did work:
.*(text in question).*
A simple way of doing it is:
1 Open Sublime Text
2 Find => Replace (Ctrl + H)
3 in Find write the desired text
4 click Find All
5 press ctrl + shift + K to remove all the lines where this search is present
This is a quick solution to remove some lines that contains some text
Above answers are the correct ways, but if you want to get rid of the rows with even a single string then do,
Find -> Replace -> put ^.*[a-zA-Z]+.*\n In the find section and keep replace with blank. Hit the replace all button this will delete all the rows with even a single string in it.
I like the manual edition solution, very good.
But.. have you tried to use cat and grep -v to filter out the lines and redirect to another file? Maybe better than learning regex.. (personally I always start with regex and end with editing the files myself).
In Windows you use findstr /v.
So you would do:
# in bash
cat my.log | grep -v "No records to send and/or not connected" > new.log
or
# in cmd
cat my.log | findstr /v "No records to send and/or not connected" > new.log
I ran into a similar problem editing a sitemap
This worked for me:
Copy the last word in the lines that you want to delete
Find all
Press delete to delete the entire line
Find -> Find all (this will mark the lines having the keyword)
Then go to Edit->Line->Delete line

How does one align code (braces, parens etc) in vi?

How do you prettify / align / format code in vi? What is the command?
I have pasted in a hunk of code and I need to have it all formatted/aligned... obviously I am a vi neophyte.
x
These commands in my answer work in vim. Most people who think they're using vi are using vim. To find out if your 'vi' is really 'vim', open vi and type :version -- if it's vim, it will say so. Otherwise you might just see a version number without the name of the program. Also, when you open vim for the first time you will usually see a splash screen of some sort that says "VIM - VI iMproved"...
Automatic Indentation
To turn auto-indentation on, make sure vim knows the file type you're editing (it usually automatically detects this from the file name extension, but might not figure it out with some file types). You can tell it the filetype using the menus for syntax highlighting. Then, do this:
:filetype indent on
You can disable auto-indentation with
:filetype indent off
Automatically adjusting/correcting indentation
In general, ={motion} will align code to an indentation level.
== align the current line
=i{ align the inner block
=% align to the matching parenthesis/bracket under the cursor
=14j or 14== align the next 14 lines
=G align to the end of the file
vG= same thing, align to the end of the
file (but using visual mode)
vjjj= align four lines (using visual mode)
Manual indentation
If vim is not guessing the indentation level correctly, there are two ways to change it:
If you are in normal mode (where everything is a command), do << to shift a line left, or >> to shift it right by one tab. You can do this with several lines by using the same movement commands I showed above (eg, >i{ indents the current inner code block).
If you are in insert mode, you can indent the line further (without moving the cursor) by doing a Ctrl-T, or un-indent one tab with Ctrl-D
Aligning equals signs, etc
If you want to align equals signs in a list of declarations, you should consider using this vim script: http://www.vim.org/scripts/script.php?script_id=294
Adjusting indentation/tab sizes
If you want vim to use spaces instead of tabs when it indents, run this command (or consider adding it to your vimrc file)
:set expandtab
To set how many spaces equal a tab, I usually do this:
:set expandtab softtabstop=3 tabstop=3 shiftwidth=3
tabstop - how many columns a tab counts for (affects display of existing tab characters)
shiftwidth - controls reindentation size with << and >>, among other commands.
softtabstop - how much space to insert when you press the tab key
expandtab - expand tab keys to spaces
But if you have to work with different amounts of tabs a lot, you could also use this function and keybinding:
function! Ktabs(tabsize)
execute "set softtabstop=" . a:tabsize . " tabstop=" . a:tabsize . " expandtab shiftwidth=" . a:tabsize
"set softtabstop=a:tabsize tabstop=a:tabsize expandtab shiftwidth=a:tabsize
endfunction
noremap <leader><Tab> :call Ktabs(3)<Left>
If you are editing a file with a mix of tabs and spaces, you may want to use this command after setting tab size:
:retab
={motion}
:h =
P.S. You shouldn't use vi if vim is available.
If manually adjusting indents I will open a visual block with V on the first or last line I want to re-indent, move to the brace containing the block, goto the other brace with % then shift the line with > or <
If indents are off by a lot I will shift everything all the way left with < and repeat it with . and then re-indent everything.
Another solution is to use the unix fmt command as described in Your problem with Vim is that you don't grok vi., {!}fmt

Resources