How do I search through MATLAB command history? - search

I would like to search for a specific command I've previously used. Is it possible to do a free text search on MATLAB command history?

Yes. Matlab stores your command history in a file called history.m in the "preferences folder," a directory containing preferences, history, and layout files. You can find the preferences folder using the prefdir command:
>> prefdir
ans =
/home/tobin/.matlab/R2010a
Then search the history.m file in that directory using the mechanism of your choice. For instance, using grep on unix:
>> chdir(prefdir)
>> !grep plot history.m
plot(f, abs(tf))
doc biplot
!grep plot history.m
You can also simply use the search function in the command history window if you just want to use the GUI.

If you want to accomplish this in a programmatic and platform-independent manner, you can first use MATLAB's Java internals to get the command history as a character array:
history = com.mathworks.mlservices.MLCommandHistoryServices.getSessionHistory;
historyText = char(history);
Then you can search through the character array however you like, using functions like STRFIND or REGEXP. You can also turn the character array into a cell array of strings (one line per cell) with the function CELLSTR, since they can sometimes be easier to work with.

Related

Replace visual selection with command output

I would like to replace parts of one line with the result of the selection being piped into a command.
For example:
echo "hello $(echo "world" | base64)" | vim -
This will open a buffer with hello d29ybGQK in it. Now press wvw to visually select d29ybGQK.
Then I attempted :!base64 -d and I expected the buffer to contain hello world, which did not happen. Indeed, the whole line was piped into the command, and the whole line was replaced.
Is it possible to replace only the visual selection, and have only that selection piped into the command?
I also attempted c<c-r>=system('base64 -d') but that did not send the visual selection to the command's stdin.
Filtering with ! is always line-wise. Your solution with c and the
expression register is an excellent way to solve this. You only forgot
to pass the input to system(), which is its second optional argument.
Since you just changed the text selected, it went into the " register
automatically. All you need to do is to grab it back and pass it to
system with getreg():
c<C-R>=system('base64 -D', getreg('"'))
Note that base64 may echo a newline at the end. If you want to remove
it, either wrap the whole thing in trim(), a new function in Vim 8, or
use [:-2]:
c<C-R>=trim(system('base64 -D', getreg('"')))
c<C-R>=system('base64 -D', getreg('"'))[:-2]
This is a shorthand for [0:-2], meaning grab everything from character
0 to second-last in the resulting string.
Consider creating a visual map if you use it often:
vnoremap <leader>d c<C-R>=system('base64 -D', getreg('"'))[:-2]<CR>
For historical reasons, the Ex commands are inherently line-based; venerable vi also didn't have visual mode yet. That limitation includes filtering through an external command with :range!; it will always filter complete lines.
manual solution
For simple input like in your example, it's probably easiest to temporarily split the line, filter, and then reassemble. For example:
:substitute/ /\r/ | execute '.!base64 -d' | -1join
plugin solution
For a universal solution, you need to use or implement a plugin that grabs the selected text, filters it (probably through system()), and then replaces the selection with the result.
My SubstituteExpression plugin has a {Visual}g= mapping that can filter through Vimscript expressions, Vim functions and commands, and external commands.
express.vim by Tom McDonald offers an almost identical implementation. It also allows on-the-fly creation of operators via :MapExpress and :MapSubpress, something for which I would use my TextTransform plugin, which you need to install as a dependency, anyway. My plugin offers more advanced (cross-mode) repeats, and the :Ex-command expression variant, but has two large dependencies you also need to install.

Cscope Syntax hightlight search within search

I am using CSCOPE for my project and I have few question.
1) Can I color the search result (mostly when I serach using
":cs f s " i.e. within the open file.
2) Is there a way to search within search? Like I search "ret_val" and it gave 1000 result, instead of going each line, can I search some more like folder name etc?
I am not sure how you use the tool, but generally, the answer is yes.
1) You could either redirect results to file, lets say out.cscope then create syntax file for file type *.csope OR, use a function to run the tool, then locally set up the buffer with adequate hi and match options
2) If you saved search results in a cscope file then you can search it normally. Otherwise you can redir output to new tab/split and again search the buffer (which now doesn't need to have externally associated file. I suggest you to use vim-foldsearch plugin to fold out anything (not) in the match. You can even delete folded sections (with or without any number of context lines) so that only stuff you are interested in finally remains in the buffer (i.e. it behaves like filter). Alternatively you can use unite plugin and it filtering source lines which filters lines as you type the keyword. Actually, I would personally go with that option as it provides most interactive way of working.

Bash Scripting: Find strings on one line of code and insert on own line

I'm trying to write a small bash script that:
-wget's an html file every [x] minutes from the web
-uses some linux utility to find differences in the file between the last two updates
-Uses sed to modify the lines on which new text was detected
The problem I am running into is that the HTML file uses in-line CSS
to format a table, but the actual code for the page is stored on one long line.
Effectively I need a Linux utility that can scan through a single line of code, find
every instance of text between each tags, and insert those instances on their own line. That should make scanning the text easier. Every tool I've tried searches on a per-line basis which can't do what I need since the entire code is stored on a single line.
You could first split the content into lines, by substituting (say) > with >\n. That will break up the document on the end of each HTML tag.
Maybe you don't even need to do that: if you use awk's RS variable to define the record separator as ">" instead of newline. See this page for an example of using RS: http://www.thegeekstuff.com/2010/01/8-powerful-awk-built-in-variables-fs-ofs-rs-ors-nr-nf-filename-fnr/

How to bookmark most using linux command line

How i can bookmark my custom command line in linux (centos 6.2)?
I'm using history | grep keyword and then !command number now.
But is there any faster solution?
just use alias:
alias lala="ls -lrt|grep a"
Yes. You can define an alias, a function, or write a script in your bin folder.
Most shell's provides more complex history expansion. For example you can use
!?str
in zsh, bash etc. to execute the most recent command that contains str.
Another possibility to save keystrokes would be to use the incremental search of the history if you want a broader control over what you want to select.
To search the history backwards this feature is usually bound to <ctrl>-r, for forward search it's <ctrl>-s (at least in bash and zsh).
Just put a comment tag on it and you can search for the tag latter.
$ my-command # tag-bookmark-1
Then later you can grep or search history for tag-bookmark-1.
Use something shorter if you would like.
(I can't say about !, because I always use export histchars="")

How can I view log files in Linux and apply custom filters while viewing?

I need to read through some gigantic log files on a Linux system. There's a lot of clutter in the logs. At the moment I'm doing something like this:
cat logfile.txt | grep -v "IgnoreThis\|IgnoreThat" | less
But it's cumbersome -- every time I want to add another filter, I need to quit less and edit the command line. Some of the filters are relatively complicated and may be multi-line.
I'd like some way to apply filters as I am reading through the log, and a way to save these filters somewhere.
Is there a tool that can do this for me? I can't install new software so hopefully it's something that would already be installed -- e.g., less, vi, something in a Python or Perl lib, etc.
Changing the code that generates the log to generate less is not an option.
Use &pattern command within less.
From the man page for less
&pattern
Display only lines which match the pattern; lines which do not
match the pattern are not displayed. If pattern is empty (if
you type & immediately followed by ENTER), any filtering is
turned off, and all lines are displayed. While filtering is in
effect, an ampersand is displayed at the beginning of the
prompt, as a reminder that some lines in the file may be hidden.
Certain characters are special as in the / command:
^N or !
Display only lines which do NOT match the pattern.
^R Don't interpret regular expression metacharacters; that
is, do a simple textual comparison.
Try the multitail tool - as well as letting you view multile logs at once, I'm pretty sure it lets you apply regex filters interactively.
Based on ghostdog74's answer and the less manpage, I came up with this:
~/.bashrc:
export LESSOPEN='|~/less-filter.sh %s'
export LESS=-R # to allow ANSI colors
~/less-filter.sh:
#!/bin/sh
case "$1" in
*logfile*.log*) ~/less-filter.sed < $1
;;
esac
~/less-filter.sed:
/deleteLinesLikeThis/d # to filter out lines
s/this/that/ # to change text on lines (useful to colorize using ANSI escapes)
Then:
less logfileFooBar.log.1 -- applies the filter applies automatically.
cat logfileFooBar.log.1 | less -- to see the log without filtering
This is adequate for now but I would still like to be able to edit the filters on the fly.
see the man page of less. there are some options you can use to search for words for example. It has line editing mode as well.
There's an application by Casstor Software Solutions called LogFilter (www.casstor.com) that can edit Windows/Mac/Linux text files and can easily perform file filtering. It supports multiple filters as well as regular expressions. I think it might be what you're looking for.

Resources